ImplicitLoginUtil.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package jnpf.implicit.utils;
  2. import jnpf.implicit.request.ImplicitDingTalkRequest;
  3. import jnpf.implicit.request.ImplicitWeChatEnterpriseWWQrcodeRequest;
  4. import jnpf.socials.config.CustomAuthConfig;
  5. import jnpf.socials.config.SocialsConfig;
  6. import jnpf.socials.model.AuthCallbackNew;
  7. import jnpf.config.ConfigValueUtil;
  8. import jnpf.constant.MsgCode;
  9. import jnpf.exception.LoginException;
  10. import jnpf.util.StringUtil;
  11. import me.zhyd.oauth.enums.AuthResponseStatus;
  12. import me.zhyd.oauth.exception.AuthException;
  13. import me.zhyd.oauth.model.AuthResponse;
  14. import me.zhyd.oauth.model.AuthUser;
  15. import me.zhyd.oauth.request.AuthRequest;
  16. import me.zhyd.oauth.request.AuthWeChatEnterpriseWebRequest;
  17. import org.springframework.beans.BeanUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Component;
  20. import java.net.URLEncoder;
  21. /**
  22. * 免登工具类
  23. *
  24. * @author JNPF开发平台组
  25. * @version v5.0.0
  26. * @copyright 引迈信息技术有限公司
  27. * @date 2024/5/23 11:09:26
  28. */
  29. @Component
  30. public class ImplicitLoginUtil {
  31. @Autowired
  32. private SocialsConfig socialsConfig;
  33. @Autowired
  34. private ConfigValueUtil configValueUtil;
  35. public String getAuthLink(String source) {
  36. AuthRequest authRequest = this.getAuthRequest(source, null, null);
  37. return authRequest.authorize(null);
  38. }
  39. public String loginByCode(String source, String code, String state) {
  40. //获取第三方请求
  41. AuthCallbackNew callback = new AuthCallbackNew();
  42. callback.setAuthCode(code);
  43. callback.setAuth_code(code);
  44. callback.setAuthorization_code(code);
  45. callback.setCode(code);
  46. callback.setState(state);
  47. AuthRequest authRequest = this.getAuthRequest(source, null, null);
  48. AuthResponse<AuthUser> res = authRequest.login(callback);
  49. if (AuthResponseStatus.FAILURE.getCode() == res.getCode()) {
  50. throw new LoginException("连接失败!");
  51. } else if (AuthResponseStatus.SUCCESS.getCode() != res.getCode()) {
  52. throw new LoginException("授权失败:" + res.getMsg());
  53. }
  54. //登录用户第三方id
  55. return getSocialUuid(res);
  56. }
  57. private String getSocialUuid(AuthResponse<AuthUser> res) {
  58. String uuid = res.getData().getUuid();
  59. if (res.getData().getToken() != null && StringUtil.isNotEmpty(res.getData().getToken().getUnionId())) {
  60. uuid = res.getData().getToken().getUnionId();
  61. }
  62. return uuid;
  63. }
  64. /**
  65. * 根据配置信息获取请求对象
  66. *
  67. * @param
  68. * @return
  69. * @copyright 引迈信息技术有限公司
  70. * @date 2022/7/21
  71. */
  72. public AuthRequest getAuthRequest(String source, String userId, String tenantId) {
  73. AuthRequest authRequest = null;
  74. String url = configValueUtil.getApiDomain() + "/api/oauth/Login/implicit?source=" + source;
  75. String platform = source.toLowerCase();
  76. CustomAuthConfig socialConfig = socialsConfig.getSocialMap().get(platform);
  77. CustomAuthConfig newSocialConfig = new CustomAuthConfig();
  78. BeanUtils.copyProperties(socialConfig, newSocialConfig);
  79. newSocialConfig.setRedirectUri(url);
  80. switch (source.toLowerCase()) {
  81. case "dingtalk":
  82. newSocialConfig.setRedirectUri(URLEncoder.encode(newSocialConfig.getRedirectUri()));
  83. newSocialConfig.setIgnoreCheckState(true);
  84. authRequest = new ImplicitDingTalkRequest(newSocialConfig);
  85. break;
  86. case "wechat_enterprise":
  87. newSocialConfig.setRedirectUri(URLEncoder.encode(newSocialConfig.getRedirectUri()));
  88. authRequest = new AuthWeChatEnterpriseWebRequest(newSocialConfig);
  89. break;
  90. case "wechat_enterprise_ww":
  91. newSocialConfig.setRedirectUri(URLEncoder.encode(newSocialConfig.getRedirectUri()));
  92. authRequest = new ImplicitWeChatEnterpriseWWQrcodeRequest(newSocialConfig);
  93. break;
  94. default:
  95. break;
  96. }
  97. if (null == authRequest) {
  98. throw new AuthException(MsgCode.OA024.get());
  99. }
  100. return authRequest;
  101. }
  102. }