Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
justauth
JustAuth
提交
ebf39627
J
JustAuth
项目概览
justauth
/
JustAuth
1 年多 前同步成功
通知
391
Star
15212
Fork
2708
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JustAuth
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
ebf39627
编写于
8月 11, 2021
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
添加 AuthRequestBuilder 可以便捷的创建 AuthRequest
上级
74ee17b2
变更
8
展开全部
隐藏空白更改
内联
并排
Showing
8 changed file
with
550 addition
and
142 deletion
+550
-142
CHANGELOGS.md
CHANGELOGS.md
+11
-0
README.md
README.md
+74
-19
src/main/java/me/zhyd/oauth/AuthRequestBuilder.java
src/main/java/me/zhyd/oauth/AuthRequestBuilder.java
+98
-0
src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java
src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java
+250
-86
src/main/java/me/zhyd/oauth/config/AuthSource.java
src/main/java/me/zhyd/oauth/config/AuthSource.java
+8
-0
src/test/java/me/zhyd/oauth/AuthRequestBuilderTest.java
src/test/java/me/zhyd/oauth/AuthRequestBuilderTest.java
+105
-0
src/test/java/me/zhyd/oauth/config/AuthExtendSource.java
src/test/java/me/zhyd/oauth/config/AuthExtendSource.java
+4
-24
src/test/java/me/zhyd/oauth/request/AuthWeChatMpRequestTest.java
...t/java/me/zhyd/oauth/request/AuthWeChatMpRequestTest.java
+0
-13
未找到文件。
CHANGELOGS.md
浏览文件 @
ebf39627
## 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
### 2021/7/28
...
...
README.md
浏览文件 @
ebf39627
...
...
@@ -61,7 +61,7 @@ JustAuth 集成了诸如:Github、Gitee、支付宝、新浪微博、微信、
## 快速开始
-
引入依赖
###
引入依赖
```
xml
<dependency>
<groupId>
me.zhyd.oauth
</groupId>
...
...
@@ -69,22 +69,8 @@ JustAuth 集成了诸如:Github、Gitee、支付宝、新浪微博、微信、
<version>
1.16.2
</version>
</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
...
...
@@ -92,7 +78,7 @@ authRequest.login(callback);
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-http
</artifactId>
<version>
5.
2.5
</version>
<version>
5.
7.7
</version>
</dependency>
```
...
...
@@ -102,7 +88,7 @@ authRequest.login(callback);
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.5.1
2
</version>
<version>
4.5.1
3
</version>
</dependency>
```
...
...
@@ -112,9 +98,78 @@ authRequest.login(callback);
<dependency>
<groupId>
com.squareup.okhttp3
</groupId>
<artifactId>
okhttp
</artifactId>
<version>
4.
4
.1
</version>
<version>
4.
9
.1
</version>
</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
();
```
## 赞助和支持
...
...
src/main/java/me/zhyd/oauth/AuthRequestBuilder.java
0 → 100644
浏览文件 @
ebf39627
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
;
}
}
src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java
浏览文件 @
ebf39627
此差异已折叠。
点击以展开。
src/main/java/me/zhyd/oauth/config/AuthSource.java
浏览文件 @
ebf39627
...
...
@@ -3,6 +3,7 @@ package me.zhyd.oauth.config;
import
me.zhyd.oauth.enums.AuthResponseStatus
;
import
me.zhyd.oauth.exception.AuthException
;
import
me.zhyd.oauth.model.AuthCallback
;
import
me.zhyd.oauth.request.AuthDefaultRequest
;
/**
* OAuth平台的API地址的统一接口,提供以下方法:
...
...
@@ -73,4 +74,11 @@ public interface AuthSource {
}
return
this
.
getClass
().
getSimpleName
();
}
/**
* 平台对应的 AuthRequest 实现类,必须继承自 {@link AuthDefaultRequest}
*
* @return class
*/
Class
<?
extends
AuthDefaultRequest
>
getTargetClass
();
}
src/test/java/me/zhyd/oauth/AuthRequestBuilderTest.java
0 → 100644
浏览文件 @
ebf39627
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
()));
}
}
}
src/test/java/me/zhyd/oauth/config/AuthExtendSource.java
浏览文件 @
ebf39627
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
;
/**
* 测试自定义实现{@link AuthSource}接口后的枚举类
...
...
@@ -15,7 +12,7 @@ import me.zhyd.oauth.request.AuthRequest;
*/
public
enum
AuthExtendSource
implements
AuthSource
{
OTHER
(
AuthExtendRequest
.
class
)
{
OTHER
{
/**
* 授权的api
*
...
...
@@ -65,27 +62,10 @@ 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配置"
);
@Override
public
Class
<?
extends
AuthDefaultRequest
>
getTargetClass
()
{
return
AuthExtendRequest
.
class
;
}
}
...
...
src/test/java/me/zhyd/oauth/request/AuthWeChatMpRequestTest.java
浏览文件 @
ebf39627
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
{
...
...
@@ -18,15 +16,4 @@ public class AuthWeChatMpRequestTest {
.
build
());
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录