增加注释,优化代码

This commit is contained in:
timfruit 2021-11-05 23:14:51 +08:00
parent 65abc667b0
commit 13a9405082
5 changed files with 21 additions and 40 deletions

View File

@ -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 {

View File

@ -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<String, JustAuthProperties.JustAuthProxyConfig> 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());
}
}

View File

@ -2,7 +2,11 @@ package cn.iocoder.yudao.framework.social.core.enums;
import me.zhyd.oauth.config.AuthSource;
// TODO @timfruit类注释
/**
* 拓展JustAuth各api需要的url 用枚举类分平台类型管理<br>
*
* 默认配置{@link me.zhyd.oauth.config.AuthDefaultSource}
*/
public enum AuthExtendSource implements AuthSource {
/**

View File

@ -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;
}

View File

@ -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 {