-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
298 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# 关于OAuth | ||
|
||
请先查阅以下资料: | ||
|
||
- [The OAuth 2.0 Authorization Framework](https://tools.ietf.org/html/rfc6749) | ||
- [OAuth 2.0](https://oauth.net/2/) | ||
|
||
## OAuth 2的授权流程 | ||
|
||
### 参与的角色 | ||
|
||
- `Resource Owner` 资源所有者,即代表授权客户端访问本身资源信息的用户(User),也就是应用场景中的“**开发者A**” | ||
- `Resource Server` 资源服务器,托管受保护的**用户账号信息**,比如Github | ||
- `Authorization Server` 授权服务器,**验证用户身份**然后为客户端派发资源访问令牌,比如Github | ||
- `Resource Server`和`Authorization Server` 可以是同一台服务器,也可以是不同的服务器,视具体的授权平台而有所差异 | ||
- `Client` 客户端,即代表意图访问受限资源的**第三方应用** | ||
|
||
### 授权流程 | ||
```html | ||
+--------+ +---------------+ | ||
| |--(A)- Authorization Request ->| Resource | | ||
| | | Owner | | ||
| |<-(B)-- Authorization Grant ---| | | ||
| | +---------------+ | ||
| | | ||
| | +---------------+ | ||
| |--(C)-- Authorization Grant -->| Authorization | | ||
| Client | | Server | | ||
| |<-(D)----- Access Token -------| | | ||
| | +---------------+ | ||
| | | ||
| | +---------------+ | ||
| |--(E)----- Access Token ------>| Resource | | ||
| | | Server | | ||
| |<-(F)--- Protected Resource ---| | | ||
+--------+ +---------------+ | ||
``` | ||
|
||
上面的流程图取自[The OAuth 2.0 Authorization Framework#1.2](https://tools.ietf.org/html/rfc6749#section-1.2) | ||
|
||
- (A) 用户打开**客户端**以后,**客户端**要求**用户**给予授权。 | ||
- (B) **用户**同意给予**客户端**授权。 | ||
- (C) **客户端**使用上一步获得的授权,向**认证服务器**申请令牌。 | ||
- (D) **认证服务器**对**客户端**进行认证以后,确认无误,同意发放令牌 | ||
- (E) **客户端**使用令牌,向**资源服务器**申请获取资源。 | ||
- (F) **资源服务器**确认令牌无误,同意向**客户端**开放资源。 | ||
|
||
### 授权许可 `Authorization Grant` | ||
|
||
- Authorization Code | ||
- 结合普通服务器端应用使用(**web**端常用的授权方式) | ||
- Implicit | ||
- 结合移动应用或 Web App 使用 | ||
- Resource Owner Password Credentials | ||
- 适用于受信任客户端应用,例如同个组织的内部或外部应用 | ||
- Client Credentials | ||
- 适用于客户端调用主服务API型应用(比如百度API Store) | ||
|
||
在`JustAuth`中是使用的`Authorization Code`授权方式,下面将主要讲解`Authorization Code`的授权流程 | ||
|
||
(未完待续) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 致谢 | ||
|
||
在项目立项初期,也对当前开源圈的一些相同类型的项目作过调研,同时本项目也参考过这些项目,再次感谢开源圈内的朋友。 | ||
|
||
- [YurunOAuthLogin](https://gitee.com/yurunsoft/YurunOAuthLogin): PHP 第三方登录授权 SDK | ||
- [阿里妈妈MUX倾力打造的矢量图标库-iconfont](https://www.iconfont.cn/search/index): 本文档中的图标大部分取自该平台 | ||
- [mica](https://github.com/lets-mica/mica):Spring Cloud 微服务开发核心包,支持 `web `和 `webflux`。注:JustAuth项目中的[UuidUtils](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/utils/UuidUtils.java)就是直接使用的mica提供的高性能的uuid创建工具类源码[StringUtil.java](https://github.com/lets-mica/mica/blob/master/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java#L335) | ||
|
||
|
||
**感谢 JetBrains 提供的免费开源 License** | ||
|
||
<img src="https://github.com/lets-mica/mica/raw/c251e176b81518a6a570bf4eb21f525c4f582a81/docs/img/jetbrains.png" alt="图片引用自lets-mica" style="float:left;"> | ||
|
||
<div style="clear: both;"></div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,53 @@ | ||
# 使用State | ||
|
||
待补充 | ||
## state使用的流程 | ||
|
||
在JustAuth中`state`参数的使用流程如下: | ||
|
||
1. 获取`authorizeUrl`时创建`state`(开发者创建,如果不创建则系统默认生成) | ||
2. 缓存`state`(JustAuth执行) | ||
3. 内置的缓存调度器自动清除已过期的`state`(JustAuth执行) | ||
|
||
## 创建state(开发者) | ||
`state`在OAuth授权流程中是一个**非必要但很重要**的参数,就如[名词解释](https://docs.justauth.whnb.wang/#/explain?id=justauth中的关键词)中描述的:`state`是用来保持授权会话流程完整性,防止CSRF攻击的安全的随机的参数,**由开发者生成**。 | ||
|
||
在JustAuth中提供了一个默认的创建state的方法,使用方式: | ||
|
||
```java | ||
String state = AuthStateUtils.createState() | ||
``` | ||
|
||
`createState`的内部实现其实就是生成了一个UUID(采用 jdk 9 的形式,优化性能),该工具是直接copy自[mica](https://github.com/lets-mica/mica/blob/master/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java)(`mica`是一个SpringBoot微服务高效开发工具集,开源地址:[https://github.com/lets-mica/mica](https://github.com/lets-mica/mica)),关于mica uuid生成方式的压测结果,可以参考:https://github.com/lets-mica/mica-jmh/wiki/uuid。 | ||
|
||
除此之外,开发者还可以自己生成特定的`state`参数。 | ||
|
||
## 缓存state(JustAuth) | ||
|
||
在JustAuth中,内置了一个基于map的state缓存器,默认缓存有效期为3分钟(缓存配置见`AuthCacheConfig.java`)。`AuthCacheConfig`中包含两个配置参数: | ||
|
||
- `timeout` 缓存过期时间,默认3分钟 | ||
- `schedulePrune` 是否开启定时清理过期state的任务,默认开启。如果不开启,则需要开发者自己对state做处理,防止map存入过多内容 | ||
|
||
缓存state的操作是在`getRealState`中触发的,不需要开发者自己处理 | ||
```java | ||
/** | ||
* 获取state,如果为空, 则默认取当前日期的时间戳 | ||
* | ||
* @param state 原始的state | ||
* @return 返回不为null的state | ||
*/ | ||
protected String getRealState(String state) { | ||
if (StringUtils.isEmpty(state)) { | ||
state = UuidUtils.getUUID(); | ||
} | ||
// 缓存state | ||
authStateCache.cache(state, state); | ||
return state; | ||
} | ||
``` | ||
|
||
注:关于自定义缓存,请参考下节内容。 | ||
|
||
## 清理state(JustAuth) | ||
|
||
JustAuth内置了一个缓存调度器,默认3分钟清理一次过期的`state`,缓存清理时间可以通过`AuthCacheConfig.timeout`进行修改,不建议修改太大。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.