Commit 13a94050822b0862eca917ccb981a997e10fca57

Authored by timfruit
1 parent 65abc667

增加注释,优化代码

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;
56 56 @Slf4j
57 57 public class SysAuthServiceImpl implements SysAuthService {
58 58  
  59 + private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.ADMIN;
  60 +
59 61 @Resource
60 62 @Lazy // 延迟加载,因为存在相互依赖的问题
61 63 private AuthenticationManager authenticationManager;
... ... @@ -75,7 +77,6 @@ public class SysAuthServiceImpl implements SysAuthService {
75 77 @Resource
76 78 private SysSocialCoreService socialService;
77 79  
78   - private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.ADMIN;
79 80  
80 81 @Override
81 82 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
... ...
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/YudaoAuthRequestFactory.java
1 1 package cn.iocoder.yudao.framework.social.core;
2 2  
3 3 import cn.hutool.core.util.EnumUtil;
  4 +import cn.hutool.core.util.ReflectUtil;
4 5 import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource;
5 6 import cn.iocoder.yudao.framework.social.core.request.AuthWeChatMiniProgramRequest;
6   -import com.xkcoding.http.config.HttpConfig;
7 7 import com.xkcoding.justauth.AuthRequestFactory;
8 8 import com.xkcoding.justauth.autoconfigure.JustAuthProperties;
9 9 import me.zhyd.oauth.cache.AuthStateCache;
10 10 import me.zhyd.oauth.config.AuthConfig;
11 11 import me.zhyd.oauth.config.AuthSource;
12 12 import me.zhyd.oauth.request.AuthRequest;
13   -import org.springframework.util.CollectionUtils;
14 13  
15   -import java.net.InetSocketAddress;
16   -import java.net.Proxy;
17   -import java.util.Map;
  14 +import java.lang.reflect.Method;
18 15  
19 16 /**
20 17 * 第三方授权拓展 request 工厂类
21   - * TODO @timfruit 可以说明下,为啥有了 AuthRequestFactory 类,咱还需要自定义
  18 + * (为使得拓展配置和默认配置齐平,自定义本工厂类)
22 19 *
23 20 * @author timfruit
24 21 * @date 2021-10-31
... ... @@ -70,7 +67,10 @@ public class YudaoAuthRequestFactory extends AuthRequestFactory {
70 67 }
71 68  
72 69 // 配置 http config
73   - configureHttpConfig(authExtendSource.name(), config, properties.getHttpConfig());
  70 + Method method = ReflectUtil.getMethod(AuthRequestFactory.class, "configureHttpConfig",
  71 + String.class, AuthConfig.class, JustAuthProperties.JustAuthHttpConfig.class);
  72 + ReflectUtil.invoke(this, method,
  73 + authExtendSource.name(), config, properties.getHttpConfig());
74 74  
75 75 switch (authExtendSource) {
76 76 case WECHAT_MINI_PROGRAM:
... ... @@ -80,31 +80,4 @@ public class YudaoAuthRequestFactory extends AuthRequestFactory {
80 80 }
81 81 }
82 82  
83   - /**
84   - * 配置 http 相关的配置
85   - *
86   - * @param authSource {@link AuthSource}
87   - * @param authConfig {@link AuthConfig}
88   - */
89   - protected void configureHttpConfig(String authSource, AuthConfig authConfig, JustAuthProperties.JustAuthHttpConfig httpConfig) {
90   - // TODO @timfruit:可以改成反射调用父类的方法。可能有一定的损耗,但是可以忽略不计的
91   - if (null == httpConfig) {
92   - return;
93   - }
94   - Map<String, JustAuthProperties.JustAuthProxyConfig> proxyConfigMap = httpConfig.getProxy();
95   - if (CollectionUtils.isEmpty(proxyConfigMap)) {
96   - return;
97   - }
98   - JustAuthProperties.JustAuthProxyConfig proxyConfig = proxyConfigMap.get(authSource);
99   -
100   - if (null == proxyConfig) {
101   - return;
102   - }
103   -
104   - authConfig.setHttpConfig(HttpConfig.builder()
105   - .timeout(httpConfig.getTimeout())
106   - .proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig.getHostname(), proxyConfig.getPort())))
107   - .build());
108   - }
109   -
110 83 }
... ...
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;
2 2  
3 3 import me.zhyd.oauth.config.AuthSource;
4 4  
5   -// TODO @timfruit:类注释
  5 +/**
  6 + * 拓展JustAuth各api需要的url, 用枚举类分平台类型管理<br>
  7 + *
  8 + * 默认配置{@link me.zhyd.oauth.config.AuthDefaultSource}
  9 + */
6 10 public enum AuthExtendSource implements AuthSource {
7 11  
8 12 /**
... ...
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;
3 3 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
4 4 import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource;
5 5 import cn.iocoder.yudao.framework.social.core.model.AuthExtendToken;
  6 +import com.fasterxml.jackson.annotation.JsonProperty;
6 7 import lombok.Data;
7 8 import me.zhyd.oauth.cache.AuthStateCache;
8 9 import me.zhyd.oauth.config.AuthConfig;
... ... @@ -39,7 +40,7 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
39 40 this.checkResponse(accessTokenObject);
40 41  
41 42 AuthExtendToken token = new AuthExtendToken();
42   - token.setMiniSessionKey(accessTokenObject.session_key);
  43 + token.setMiniSessionKey(accessTokenObject.sessionKey);
43 44 token.setOpenId(accessTokenObject.openid);
44 45 token.setUnionId(accessTokenObject.unionid);
45 46 return token;
... ... @@ -86,12 +87,12 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
86 87 .build();
87 88 }
88 89  
89   - // TODO @timfruit:我们要采用驼峰的命名方式。不匹配的,可以通过 jackson 的自定义注解映射
90 90 @Data
91 91 private static class CodeSessionResponse {
92 92 private int errcode;
93 93 private String errmsg;
94   - private String session_key;
  94 + @JsonProperty("session_key")
  95 + private String sessionKey;
95 96 private String openid;
96 97 private String unionid;
97 98 }
... ...
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
51 51 @Slf4j
52 52 public class SysAuthServiceImpl implements SysAuthService {
53 53  
  54 + private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.MEMBER;
  55 +
54 56 @Resource
55 57 @Lazy // 延迟加载,因为存在相互依赖的问题
56 58 private AuthenticationManager authenticationManager;
... ... @@ -65,7 +67,7 @@ public class SysAuthServiceImpl implements SysAuthService {
65 67 private SysUserSessionCoreService userSessionCoreService;
66 68 @Resource
67 69 private SysSocialCoreService socialService;
68   - private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.MEMBER; // TODO @timfruit 挪到类的最前面。一般是 静态变量,到成员变量的顺序。
  70 +
69 71  
70 72 @Override
71 73 public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException {
... ...