From 13a94050822b0862eca917ccb981a997e10fca57 Mon Sep 17 00:00:00 2001 From: timfruit Date: Fri, 5 Nov 2021 23:14:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/auth/impl/SysAuthServiceImpl.java | 3 +- .../social/core/YudaoAuthRequestFactory.java | 41 ++++--------------- .../social/core/enums/AuthExtendSource.java | 6 ++- .../request/AuthWeChatMiniProgramRequest.java | 7 ++-- .../service/auth/impl/SysAuthServiceImpl.java | 4 +- 5 files changed, 21 insertions(+), 40 deletions(-) diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java index abb6a3dca..3552dd9a4 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -56,6 +56,8 @@ import static java.util.Collections.singleton; @Slf4j public class SysAuthServiceImpl implements SysAuthService { + private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.ADMIN; + @Resource @Lazy // 延迟加载,因为存在相互依赖的问题 private AuthenticationManager authenticationManager; @@ -75,7 +77,6 @@ public class SysAuthServiceImpl implements SysAuthService { @Resource private SysSocialCoreService socialService; - private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.ADMIN; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { diff --git a/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/YudaoAuthRequestFactory.java b/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/YudaoAuthRequestFactory.java index 17c36b683..26e23b9d7 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/YudaoAuthRequestFactory.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/YudaoAuthRequestFactory.java @@ -1,24 +1,21 @@ package cn.iocoder.yudao.framework.social.core; import cn.hutool.core.util.EnumUtil; +import cn.hutool.core.util.ReflectUtil; import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource; import cn.iocoder.yudao.framework.social.core.request.AuthWeChatMiniProgramRequest; -import com.xkcoding.http.config.HttpConfig; import com.xkcoding.justauth.AuthRequestFactory; import com.xkcoding.justauth.autoconfigure.JustAuthProperties; import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthSource; import me.zhyd.oauth.request.AuthRequest; -import org.springframework.util.CollectionUtils; -import java.net.InetSocketAddress; -import java.net.Proxy; -import java.util.Map; +import java.lang.reflect.Method; /** * 第三方授权拓展 request 工厂类 - * TODO @timfruit 可以说明下,为啥有了 AuthRequestFactory 类,咱还需要自定义 + * (为使得拓展配置和默认配置齐平,自定义本工厂类) * * @author timfruit * @date 2021-10-31 @@ -70,7 +67,10 @@ public class YudaoAuthRequestFactory extends AuthRequestFactory { } // 配置 http config - configureHttpConfig(authExtendSource.name(), config, properties.getHttpConfig()); + Method method = ReflectUtil.getMethod(AuthRequestFactory.class, "configureHttpConfig", + String.class, AuthConfig.class, JustAuthProperties.JustAuthHttpConfig.class); + ReflectUtil.invoke(this, method, + authExtendSource.name(), config, properties.getHttpConfig()); switch (authExtendSource) { case WECHAT_MINI_PROGRAM: @@ -80,31 +80,4 @@ public class YudaoAuthRequestFactory extends AuthRequestFactory { } } - /** - * 配置 http 相关的配置 - * - * @param authSource {@link AuthSource} - * @param authConfig {@link AuthConfig} - */ - protected void configureHttpConfig(String authSource, AuthConfig authConfig, JustAuthProperties.JustAuthHttpConfig httpConfig) { - // TODO @timfruit:可以改成反射调用父类的方法。可能有一定的损耗,但是可以忽略不计的 - if (null == httpConfig) { - return; - } - Map proxyConfigMap = httpConfig.getProxy(); - if (CollectionUtils.isEmpty(proxyConfigMap)) { - return; - } - JustAuthProperties.JustAuthProxyConfig proxyConfig = proxyConfigMap.get(authSource); - - if (null == proxyConfig) { - return; - } - - authConfig.setHttpConfig(HttpConfig.builder() - .timeout(httpConfig.getTimeout()) - .proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig.getHostname(), proxyConfig.getPort()))) - .build()); - } - } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/enums/AuthExtendSource.java b/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/enums/AuthExtendSource.java index fd6bb9cb9..16049419f 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/enums/AuthExtendSource.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/enums/AuthExtendSource.java @@ -2,7 +2,11 @@ package cn.iocoder.yudao.framework.social.core.enums; import me.zhyd.oauth.config.AuthSource; -// TODO @timfruit:类注释 +/** + * 拓展JustAuth各api需要的url, 用枚举类分平台类型管理
+ * + * 默认配置{@link me.zhyd.oauth.config.AuthDefaultSource} + */ public enum AuthExtendSource implements AuthSource { /** diff --git a/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/request/AuthWeChatMiniProgramRequest.java b/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/request/AuthWeChatMiniProgramRequest.java index ae9a8e1ec..e5bbfcaad 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/request/AuthWeChatMiniProgramRequest.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/request/AuthWeChatMiniProgramRequest.java @@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.social.core.request; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource; import cn.iocoder.yudao.framework.social.core.model.AuthExtendToken; +import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.config.AuthConfig; @@ -39,7 +40,7 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest { this.checkResponse(accessTokenObject); AuthExtendToken token = new AuthExtendToken(); - token.setMiniSessionKey(accessTokenObject.session_key); + token.setMiniSessionKey(accessTokenObject.sessionKey); token.setOpenId(accessTokenObject.openid); token.setUnionId(accessTokenObject.unionid); return token; @@ -86,12 +87,12 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest { .build(); } - // TODO @timfruit:我们要采用驼峰的命名方式。不匹配的,可以通过 jackson 的自定义注解映射 @Data private static class CodeSessionResponse { private int errcode; private String errmsg; - private String session_key; + @JsonProperty("session_key") + private String sessionKey; private String openid; private String unionid; } diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java index 6ce205107..c5c512890 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -51,6 +51,8 @@ import static cn.iocoder.yudao.userserver.modules.system.enums.SysErrorCodeConst @Slf4j public class SysAuthServiceImpl implements SysAuthService { + private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.MEMBER; + @Resource @Lazy // 延迟加载,因为存在相互依赖的问题 private AuthenticationManager authenticationManager; @@ -65,7 +67,7 @@ public class SysAuthServiceImpl implements SysAuthService { private SysUserSessionCoreService userSessionCoreService; @Resource private SysSocialCoreService socialService; - private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.MEMBER; // TODO @timfruit 挪到类的最前面。一般是 静态变量,到成员变量的顺序。 + @Override public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException {