柯桥增值式服务
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

89 lignes
3.0KB

  1. package com.ningdatech.kqapi.sso.manage;
  2. import java.util.Map;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.http.HttpEntity;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.util.MultiValueMap;
  8. import org.springframework.web.client.RestTemplate;
  9. import com.ningdatech.kqapi.common.exception.BizException;
  10. import com.ningdatech.kqapi.sso.model.dto.ReqSsoDTO;
  11. import com.ningdatech.kqapi.sso.model.vo.SsoTokenVO;
  12. import com.ningdatech.kqapi.sso.model.vo.SsoUserInfoVO;
  13. import com.ningdatech.kqapi.sso.utils.HmacAuthUtil;
  14. import lombok.RequiredArgsConstructor;
  15. import lombok.extern.slf4j.Slf4j;
  16. /**
  17. * @author CMM
  18. * @since 2024/04/07 11:17
  19. */
  20. @Component
  21. @Slf4j
  22. @RequiredArgsConstructor
  23. public class SsoManage {
  24. //@Value("${sso.token.internet-url}")
  25. @Value("${sso.token.zww-url}")
  26. private String tokenUrl;
  27. //@Value("${sso.user-info.internet-url}")
  28. @Value("${sso.user-info.zww-url}")
  29. private String userInfoUrl;
  30. @Value("${sso.access-key}")
  31. private String accessKey;
  32. @Value("${sso.secret-key}")
  33. private String secretKey;
  34. @Value("${sso.app-id}")
  35. private String appId;
  36. private final RestTemplate restTemplate;
  37. /**
  38. * 基于单点登录票据换取请求 token
  39. *
  40. * @param reqSsoDTO \
  41. * @return SsoTokenVO
  42. */
  43. public SsoTokenVO getToken(ReqSsoDTO reqSsoDTO) {
  44. String ticketId = reqSsoDTO.getTicketId();
  45. if (StringUtils.isBlank(ticketId)){
  46. throw new BizException("登录票据不能为空!");
  47. }
  48. Map<String, String> header = HmacAuthUtil.generateHeader(tokenUrl, "POST", accessKey, secretKey);
  49. MultiValueMap<String, String> multiValueMap = HmacAuthUtil.convertToMultiValueMap(header);
  50. reqSsoDTO.setAppId(appId);
  51. HttpEntity<Object> httpEntity = new HttpEntity<>(reqSsoDTO, multiValueMap);
  52. SsoTokenVO res = restTemplate.postForObject(tokenUrl, httpEntity, SsoTokenVO.class);
  53. log.info("[RestTemplateTest-基于单点登录票据换取请求token] http request :{}", res);
  54. return res;
  55. }
  56. /**
  57. * 基于 token 获取用户信息
  58. *
  59. * @param reqSsoDTO \
  60. * @return SsoUserInfoVO
  61. */
  62. public SsoUserInfoVO getUserInfo(ReqSsoDTO reqSsoDTO) {
  63. String token = reqSsoDTO.getToken();
  64. if (StringUtils.isBlank(token)){
  65. throw new BizException("token不能为空!");
  66. }
  67. Map<String, String> header = HmacAuthUtil.generateHeader(userInfoUrl, "POST", accessKey, secretKey);
  68. MultiValueMap<String, String> multiValueMap = HmacAuthUtil.convertToMultiValueMap(header);
  69. HttpEntity<Object> httpEntity = new HttpEntity<>(reqSsoDTO,multiValueMap);
  70. SsoUserInfoVO res = restTemplate.postForObject(userInfoUrl, httpEntity, SsoUserInfoVO.class);
  71. log.info("[RestTemplateTest-基于token获取用户信息] http request :{}", res);
  72. return res;
  73. }
  74. }