继续进行优化,去掉多余的改动
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,8 @@
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user