提交 7cdc7191 编写于 作者: cly_0's avatar cly_0

继续进行优化,去掉多余的改动

上级 5073f828
package me.zhyd.oauth.config;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthRequest;
/**
* OAuth平台的API地址的统一接口,提供以下方法:
......@@ -46,21 +44,6 @@ public interface AuthSource {
*/
String userInfo();
/**
* 根据配置获取对应的实例
* @param authConfig 配置
* @return AuthRequest
*/
AuthRequest getAuthRequestInstance(AuthConfig authConfig);
/**
* 根据配置和缓存获取对应的实例
* @param authConfig 配置
* @param authStateCache 缓存
* @return AuthRequest
*/
AuthRequest getAuthRequestInstance(AuthConfig authConfig, AuthStateCache authStateCache);
/**
* 取消授权的api
*
......
......@@ -2,11 +2,10 @@ package me.zhyd.oauth.config;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.AuthDefaultRequest;
import me.zhyd.oauth.request.AuthExtendRequest;
import me.zhyd.oauth.request.AuthRequest;
import java.lang.reflect.Constructor;
/**
* 测试自定义实现{@link AuthSource}接口后的枚举类
*
......@@ -16,7 +15,7 @@ import java.lang.reflect.Constructor;
*/
public enum AuthExtendSource implements AuthSource {
OTHER {
OTHER (AuthExtendRequest.class){
/**
* 授权的api
*
......@@ -47,32 +46,6 @@ public enum AuthExtendSource implements AuthSource {
return null;
}
@Override
public AuthRequest getAuthRequestInstance(AuthConfig authConfig) {
return getAuthRequestInstance(authConfig,null);
}
@Override
public AuthRequest getAuthRequestInstance(AuthConfig authConfig, AuthStateCache authStateCache) {
try {
AuthRequest request;
Class<?> clazz = Class.forName(AuthExtendRequest.class.getName());
Constructor constructor;
if(authStateCache==null){
constructor = clazz.getDeclaredConstructor(AuthConfig.class);
constructor.setAccessible(true);
request = (AuthRequest) constructor.newInstance(authConfig);
}else{
constructor = clazz.getDeclaredConstructor(AuthConfig.class, AuthStateCache.class);
constructor.setAccessible(true);
request = (AuthRequest) constructor.newInstance(authConfig, authStateCache);
}
return request;
} catch (Exception e) {
throw new AuthException("未获取到有效的Auth配置");
}
}
/**
* 取消授权的api
*
......@@ -92,5 +65,28 @@ public enum AuthExtendSource implements AuthSource {
public String refresh() {
return null;
}
};
private Class<? extends AuthDefaultRequest> targetClass;
AuthExtendSource(Class<? extends AuthDefaultRequest> targetClass) {
this.targetClass = targetClass;
}
public AuthRequest getAuthRequestInstance(AuthConfig authConfig) {
return this.getAuthRequestInstance(authConfig,null);
}
public AuthRequest getAuthRequestInstance(AuthConfig authConfig, AuthStateCache authStateCache) {
try {
if(authStateCache==null){
return this.targetClass.getDeclaredConstructor(AuthConfig.class).newInstance(authConfig);
}else{
return this.targetClass.getDeclaredConstructor(AuthConfig.class, AuthStateCache.class).newInstance(authConfig, authStateCache);
}
} catch (Exception e) {
throw new AuthException("未获取到有效的Auth配置");
}
}
}
......@@ -3,6 +3,7 @@ package me.zhyd.oauth.request;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.junit.Assert;
import org.junit.Test;
public class AuthWeChatMpRequestTest {
......@@ -25,6 +26,7 @@ public class AuthWeChatMpRequestTest {
.clientSecret("a")
.redirectUri("https://www.justauth.cn")
.build());
Assert.assertTrue(request instanceof AuthWeChatMpRequest);
System.out.println(request.authorize(AuthStateUtils.createState()));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册