提交 ebf39627 编写于 作者: 智布道's avatar 智布道 👁

添加 AuthRequestBuilder 可以便捷的创建 AuthRequest

上级 74ee17b2
## 1.16.3
### 2021/8/**
- 发布 v1.16.3
- PR
- 合并 Gitee PR ([#27](https://gitee.com/yadong.zhang/JustAuth/pulls/27))
- 修改
在 Gitee PR ([#27](https://gitee.com/yadong.zhang/JustAuth/pulls/27)) 的基础上重构代码,增加 Builder 方式创建 AuthRequest
## 1.16.2 ## 1.16.2
### 2021/7/28 ### 2021/7/28
......
...@@ -61,7 +61,7 @@ JustAuth 集成了诸如:Github、Gitee、支付宝、新浪微博、微信、 ...@@ -61,7 +61,7 @@ JustAuth 集成了诸如:Github、Gitee、支付宝、新浪微博、微信、
## 快速开始 ## 快速开始
- 引入依赖 ### 引入依赖
```xml ```xml
<dependency> <dependency>
<groupId>me.zhyd.oauth</groupId> <groupId>me.zhyd.oauth</groupId>
...@@ -69,22 +69,8 @@ JustAuth 集成了诸如:Github、Gitee、支付宝、新浪微博、微信、 ...@@ -69,22 +69,8 @@ JustAuth 集成了诸如:Github、Gitee、支付宝、新浪微博、微信、
<version>1.16.2</version> <version>1.16.2</version>
</dependency> </dependency>
``` ```
- 调用api
```java
// 创建授权request
AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
.clientId("clientId")
.clientSecret("clientSecret")
.redirectUri("redirectUri")
.build());
// 生成授权页面
authRequest.authorize("state");
// 授权登录后会返回code(auth_code(仅限支付宝))、state,1.8.0版本后,可以用AuthCallback类作为回调接口的参数
// 注:JustAuth默认保存state的时效为3分钟,3分钟内未使用则会自动清除过期的state
authRequest.login(callback);
```
如下**任选一种** HTTP 工具 依赖,_项目内如果已有,请忽略。另外需要特别注意,如果项目中已经引入了低版本的依赖,请先排除低版本以后来,引入高版本或者最新版本的依赖_ 如下**任选一种** HTTP 工具 依赖,_项目内如果已有,请忽略。另外需要特别注意,如果项目中已经引入了低版本的依赖,请先排除低版本依赖后,引入高版本或者最新版本的依赖_
- hutool-http - hutool-http
...@@ -92,7 +78,7 @@ authRequest.login(callback); ...@@ -92,7 +78,7 @@ authRequest.login(callback);
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId> <artifactId>hutool-http</artifactId>
<version>5.2.5</version> <version>5.7.7</version>
</dependency> </dependency>
``` ```
...@@ -102,7 +88,7 @@ authRequest.login(callback); ...@@ -102,7 +88,7 @@ authRequest.login(callback);
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>4.5.12</version> <version>4.5.13</version>
</dependency> </dependency>
``` ```
...@@ -112,9 +98,78 @@ authRequest.login(callback); ...@@ -112,9 +98,78 @@ authRequest.login(callback);
<dependency> <dependency>
<groupId>com.squareup.okhttp3</groupId> <groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId> <artifactId>okhttp</artifactId>
<version>4.4.1</version> <version>4.9.1</version>
</dependency> </dependency>
``` ```
### 调用api
#### 普通方式
```java
// 创建授权request
AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
.clientId("clientId")
.clientSecret("clientSecret")
.redirectUri("redirectUri")
.build());
// 生成授权页面
authRequest.authorize("state");
// 授权登录后会返回code(auth_code(仅限支付宝))、state,1.8.0版本后,可以用AuthCallback类作为回调接口的参数
// 注:JustAuth默认保存state的时效为3分钟,3分钟内未使用则会自动清除过期的state
authRequest.login(callback);
```
#### Builder 方式一
静态配置 `AuthConfig`
```java
AuthRequest authRequest = AuthRequestBuilder.builder()
.source("github")
.authConfig(AuthConfig.builder()
.clientId("clientId")
.clientSecret("clientSecret")
.redirectUri("redirectUri")
.build())
.build();
// 生成授权页面
authRequest.authorize("state");
// 授权登录后会返回code(auth_code(仅限支付宝))、state,1.8.0版本后,可以用AuthCallback类作为回调接口的参数
// 注:JustAuth默认保存state的时效为3分钟,3分钟内未使用则会自动清除过期的state
authRequest.login(callback);
```
#### Builder 方式二
静态获取并配置 `AuthConfig`
```java
AuthRequest authRequest = AuthRequestBuilder.builder()
.source("gitee")
.authConfig((source) -> {
// 通过 source 动态获取 AuthConfig
// 此处可以灵活的从 sql 中取配置也可以从配置文件中取配置
return AuthConfig.builder()
.clientId("clientId")
.clientSecret("clientSecret")
.redirectUri("redirectUri")
.build();
})
.build();
Assert.assertTrue(authRequest instanceof AuthGiteeRequest);
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
```
#### Builder 方式支持自定义的平台
```java
AuthRequest authRequest = AuthRequestBuilder.builder()
// 关键点:将自定义实现的 AuthSource 配置上
.extendSource(AuthExtendSource.values())
// ... 其他内容不变,参考上面的示例
.build();
```
## 赞助和支持 ## 赞助和支持
......
package me.zhyd.oauth;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.AuthDefaultRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.StringUtils;
import java.util.Arrays;
import java.util.function.Function;
/**
* 快捷的构建 AuthRequest
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.16.3
*/
public class AuthRequestBuilder {
private String source;
private AuthConfig authConfig;
private AuthStateCache authStateCache;
private AuthSource[] extendSource;
private AuthRequestBuilder() {
}
public static AuthRequestBuilder builder() {
return new AuthRequestBuilder();
}
public AuthRequestBuilder source(String source) {
this.source = source;
return this;
}
public AuthRequestBuilder authConfig(AuthConfig authConfig) {
this.authConfig = authConfig;
return this;
}
public AuthRequestBuilder authConfig(Function<String, AuthConfig> authConfig) {
this.authConfig = authConfig.apply(this.source);
return this;
}
public AuthRequestBuilder authStateCache(AuthStateCache authStateCache) {
this.authStateCache = authStateCache;
return this;
}
public AuthRequestBuilder extendSource(AuthSource... extendSource) {
this.extendSource = extendSource;
return this;
}
public AuthRequest build() {
if (StringUtils.isEmpty(this.source)) {
throw new AuthException("未配置 source");
}
if (null == this.authConfig) {
throw new AuthException("未配置 AuthConfig");
}
// 合并 JustAuth 默认的 AuthDefaultSource 和 开发者自定义的 AuthSource
AuthSource[] sources = this.concat(AuthDefaultSource.values(), extendSource);
// 筛选符合条件的 AuthSource
AuthSource source = Arrays.stream(sources).distinct()
.filter(authSource -> authSource.getName().equalsIgnoreCase(this.source))
.findAny()
.orElseThrow(() -> new AuthException("未获取到有效的 AuthSource 配置"));
Class<? extends AuthDefaultRequest> targetClass = source.getTargetClass();
try {
if (this.authStateCache == null) {
return targetClass.getDeclaredConstructor(AuthConfig.class).newInstance(this.authConfig);
} else {
return targetClass.getDeclaredConstructor(AuthConfig.class, AuthStateCache.class).newInstance(this.authConfig, this.authStateCache);
}
} catch (Exception e) {
e.printStackTrace();
throw new AuthException("未获取到有效的 Auth 配置");
}
}
private AuthSource[] concat(AuthSource[] first, AuthSource[] second) {
if (null == second || second.length == 0) {
return first;
}
AuthSource[] result = new AuthSource[first.length + second.length];
System.arraycopy(first, 0, result, 0, first.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
}
...@@ -3,6 +3,7 @@ package me.zhyd.oauth.config; ...@@ -3,6 +3,7 @@ package me.zhyd.oauth.config;
import me.zhyd.oauth.enums.AuthResponseStatus; import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthDefaultRequest;
/** /**
* OAuth平台的API地址的统一接口,提供以下方法: * OAuth平台的API地址的统一接口,提供以下方法:
...@@ -73,4 +74,11 @@ public interface AuthSource { ...@@ -73,4 +74,11 @@ public interface AuthSource {
} }
return this.getClass().getSimpleName(); return this.getClass().getSimpleName();
} }
/**
* 平台对应的 AuthRequest 实现类,必须继承自 {@link AuthDefaultRequest}
*
* @return class
*/
Class<? extends AuthDefaultRequest> getTargetClass();
} }
package me.zhyd.oauth;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.config.AuthExtendSource;
import me.zhyd.oauth.request.AuthExtendRequest;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.junit.Assert;
import org.junit.Test;
public class AuthRequestBuilderTest {
/**
* 示例:一般场景下通过 AuthRequestBuilder 构建 AuthRequest
*/
@Test
public void build2() {
AuthRequest authRequest = AuthRequestBuilder.builder()
.source("github")
.authConfig(AuthConfig.builder()
.clientId("a")
.clientSecret("a")
.redirectUri("https://www.justauth.cn")
.build())
.build();
Assert.assertTrue(authRequest instanceof AuthGithubRequest);
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
}
/**
* 示例:AuthConfig 需要动态获取的场景下通过 AuthRequestBuilder 构建 AuthRequest
*/
@Test
public void build() {
AuthRequest authRequest = AuthRequestBuilder.builder()
.source("gitee")
.authConfig((source) -> {
// 通过 source 动态获取 AuthConfig
// 此处可以灵活的从 sql 中取配置也可以从配置文件中取配置
return AuthConfig.builder()
.clientId(source)
.clientSecret(source)
.redirectUri("https://www.justauth.cn/" + source)
.build();
})
.build();
Assert.assertTrue(authRequest instanceof AuthGiteeRequest);
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
}
/**
* 示例:自定义实现的 AuthRequest,通过 AuthRequestBuilder 构建 AuthRequest
*/
@Test
public void build3() {
AuthRequest authRequest = AuthRequestBuilder.builder()
// 关键点:将自定义的 AuthSource 配置上
.extendSource(AuthExtendSource.values())
.source("other")
.authConfig(AuthConfig.builder()
.clientId("a")
.clientSecret("a")
.redirectUri("https://www.justauth.cn")
.build())
.build();
Assert.assertTrue(authRequest instanceof AuthExtendRequest);
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
}
/**
* 测试不同平台
*/
@Test
public void build4() {
for (AuthDefaultSource value : AuthDefaultSource.values()) {
if (value == AuthDefaultSource.TWITTER) {
System.out.println(value.getTargetClass());
System.out.println("忽略 twitter");
continue;
}
AuthRequest authRequest = AuthRequestBuilder.builder()
.source(value.getName())
.authConfig(AuthConfig.builder()
.clientId("a")
.clientSecret("a")
.redirectUri("https://www.justauth.cn")
.alipayPublicKey("asd")
.authServerId("asd")
.agentId("asd")
.domainPrefix("asd")
.stackOverflowKey("asd")
.deviceId("asd")
.clientOsType(3)
.build())
.build();
System.out.println(value.getTargetClass());
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
}
}
}
package me.zhyd.oauth.config; 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.AuthDefaultRequest;
import me.zhyd.oauth.request.AuthExtendRequest; import me.zhyd.oauth.request.AuthExtendRequest;
import me.zhyd.oauth.request.AuthRequest;
/** /**
* 测试自定义实现{@link AuthSource}接口后的枚举类 * 测试自定义实现{@link AuthSource}接口后的枚举类
...@@ -15,7 +12,7 @@ import me.zhyd.oauth.request.AuthRequest; ...@@ -15,7 +12,7 @@ import me.zhyd.oauth.request.AuthRequest;
*/ */
public enum AuthExtendSource implements AuthSource { public enum AuthExtendSource implements AuthSource {
OTHER (AuthExtendRequest.class){ OTHER {
/** /**
* 授权的api * 授权的api
* *
...@@ -65,27 +62,10 @@ public enum AuthExtendSource implements AuthSource { ...@@ -65,27 +62,10 @@ public enum AuthExtendSource implements AuthSource {
public String refresh() { public String refresh() {
return null; return null;
} }
};
private Class<? extends AuthDefaultRequest> targetClass; @Override
public Class<? extends AuthDefaultRequest> getTargetClass() {
AuthExtendSource(Class<? extends AuthDefaultRequest> targetClass) { return AuthExtendRequest.class;
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配置");
} }
} }
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.utils.AuthStateUtils; import me.zhyd.oauth.utils.AuthStateUtils;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class AuthWeChatMpRequestTest { public class AuthWeChatMpRequestTest {
...@@ -18,15 +16,4 @@ public class AuthWeChatMpRequestTest { ...@@ -18,15 +16,4 @@ public class AuthWeChatMpRequestTest {
.build()); .build());
System.out.println(request.authorize(AuthStateUtils.createState())); System.out.println(request.authorize(AuthStateUtils.createState()));
} }
@Test
public void authorize1() {
AuthRequest request = AuthDefaultSource.getAuthSource("wechat_mp").getAuthRequestInstance(AuthConfig.builder()
.clientId("a")
.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.
先完成此消息的编辑!
想要评论请 注册