1
0
mirror of synced 2026-02-17 17:47:47 +08:00

🍻 AuthUser 中新增 rawUserInfo,用来存放第三方平台返回的原始用户数据

This commit is contained in:
yadong.zhang
2020-06-07 18:46:00 +08:00
parent 8e5a679129
commit 33385fd30a
49 changed files with 295 additions and 98 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

View File

@@ -15,6 +15,7 @@
- [√ 酷家乐登录](oauth/kujiale.md)
- [√ 推特登录](oauth/twitter.md)
- [√ Coding登录](oauth/coding.md)
- [√ 阿里云登录](oauth/aliyun.md)
- [CSDN登录](oauth/csdn.md)
- [微信企业版登录](oauth/wechatEnterprise.md)
- [微信公众平台登录](oauth/wechat_mp.md)

View File

@@ -31,6 +31,8 @@
- 增加飞书授权登录
- <img src="https://avatars3.githubusercontent.com/u/20242284?s=88&u=2aeb7576bd61392e9fd94227d2b3048a9aea6f73&v=4" width="20"> · runningzyp : <a href="https://github.com/runningzyp" target="_blank">[Github]</a>
- 修改文案错误
- <img src="./_media/contributor/f4af112a.png" width="20"> · Spet : <a href="https://github.com/snippet0809" target="_blank">[Github]</a>
- 集成阿里云授权登录
- 千年等一回,我只为等你...
ps: 如有遗漏,请告知

181
docs/oauth/aliyun.md Normal file
View File

@@ -0,0 +1,181 @@
## 1. 授权流程
![](../_media/oauth/7bc9ea47.png)
1. 用户通过浏览器登录Web应用。
2. Web应用重定向到阿里云OAuth 2.0服务并将URL返回给浏览器。
注:如果用户还未登录,则会进一步重定向到阿里云登录服务。
3. 用户通过浏览器登录阿里云OAuth 2.0服务并申请授权码。
4. 阿里云OAuth 2.0服务重定向到Web应用并返回授权码给浏览器。
5. 浏览器通过Web应用使用授权码向阿里云OAuth 2.0服务申请代表用户身份的令牌。
6. 阿里云OAuth 2.0服务向Web应用返回令牌。
7. Web应用通过获取的令牌向阿里云发起访问API的请求。
## 2. 配置应用
### 2.1 创建应用
1. 云账号登录[RAM控制台](https://ram.console.aliyun.com/?spm=a2c4g.11186623.2.8.46a779b5dxuzTy)。
2. 在左侧导航栏单击OAuth应用管理。
3. 单击创建应用。
4. 输入应用名称和应用显示名称。
5. 选择应用类型。(这儿选择 WebApp 即可)
- WebApp 指基于浏览器交互的网络应用。
- NativeApp指操作系统中运行的本地应用主要为运行在桌面操作系统或移动操作系统中的应用。
- ServerApp指直接访问阿里云服务而无需依赖用户登录的应用目前仅支持基于SCIM协议的用户同步应用。示例请参见[通过SCIM协议将企业内部账号同步到阿里云RAM](https://help.aliyun.com/document_detail/162674.html?spm=a2c4g.11186623.2.9.46a779b5dxuzTy#task-2471389)。
6. 根据需要修改访问令牌有效期时长。
有效期可设置范围为15分钟至3小时默认为3600秒。
7. 根据需要修改刷新令牌有效期时长。
有效期可设置范围为2小时至1年默认为2592000秒。
8. 输入回调地址。![](../_media/oauth/4f3da199.png)
9. 单击确定完成应用创建。
### 2.2 配置应用 OAuth 范围
![](../_media/oauth/f44fb011.png)
如上,除了默认的 openid 外,再单独增加两条 scope
- aliuid阿里云颁发的唯一用户标志符
- profile用户的名称等个人信息
scope 对应获取的用户信息关系表如下:
![](../_media/oauth/673628f8.png)
### 2.3 配置密钥
应用创建完成后,进入应用详情:
![](../_media/oauth/2055a056.png)
切换到 “应用密钥” 选项页,创建密钥
![](../_media/oauth/930e0825.png)
注:密钥创建完成后记得要保存,因为密钥只能在创建完成后明文显示一次,如丢失或者忘记,请重新创建。
## 3. 集成JustAuth
### 2.1 引入依赖
```xml
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>${latest.version}</version>
</dependency>
```
`${latest.version}`表示当前最新的版本,可以在[这儿](https://github.com/justauth/JustAuth/releases)获取最新的版本信息。
### 2.2 创建Request
```java
AuthRequest authRequest = new AuthAliyunRequest(AuthConfig.builder()
.clientId("APPID")
.clientSecret("应用私钥")
.alipayPublicKey("支付宝公钥")
.redirectUri("https://www.zhyd.me/oauth/callback/alipay")
.build());
```
### 2.3 生成授权地址
我们可以直接使用以下方式生成第三方平台的授权链接:
```java
String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
```
这个链接我们可以直接后台重定向跳转也可以返回到前端后前端控制跳转。前端控制的好处就是可以将第三方的授权页嵌入到iframe中适配网站设计。
### 2.4 以上完整代码如下
```java
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthAliyunRequest;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@RequestMapping("/oauth")
public class RestAuthController {
@RequestMapping("/render")
public void renderAuth(HttpServletResponse response) throws IOException {
AuthRequest authRequest = getAuthRequest();
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
}
@RequestMapping("/callback")
public Object login(AuthCallback callback) {
AuthRequest authRequest = getAuthRequest();
return authRequest.login(callback);
}
private AuthRequest getAuthRequest() {
return new AuthAliyunRequest(AuthConfig.builder()
.clientId("APPID")
.clientSecret("应用私钥")
.redirectUri("回调地址")
.build());
}
}
```
## 3. 授权结果
```json
{
"code": 2000,
"msg": null,
"data": {
"uuid": "lmwxxxw==",
"username": "yadong.zhang0415@gmail.com",
"nickname": "root",
"avatar": null,
"blog": null,
"company": null,
"location": null,
"email": null,
"remark": null,
"gender": "UNKNOWN",
"source": "ALIYUN",
"token": {
"accessToken": "xxxx",
"expireIn": 3599,
"refreshToken": null,
"uid": null,
"openId": null,
"accessCode": null,
"unionId": null,
"scope": null,
"tokenType": "Bearer",
"idToken": "xxxxx",
"macAlgorithm": null,
"macKey": null,
"code": null,
"oauthToken": null,
"oauthTokenSecret": null,
"userId": null,
"screenName": null,
"oauthCallbackConfirmed": null
},
"rawUserInfo": {
"sub": "lmwtxxxZ9ezw==",
"uid": "193xxx",
"login_name": "yadong.zhang0415@gmail.com",
"requestid": "544effc2-f7ba-4ac7-8af4-da2739e50e04",
"name": "root",
"bid": "2xxx42",
"aid": "193xxx"
}
}
}
```

View File

@@ -58,7 +58,7 @@ String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
```java
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthWeiboRequest;
import me.zhyd.oauth.request.AuthCodingRequest;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.request.AuthRequest;
@@ -94,7 +94,7 @@ public class RestAuthController {
}
private AuthRequest getAuthRequest() {
return new AuthTencentCloudRequest(AuthConfig.builder()
return new AuthCodingRequest(AuthConfig.builder()
.clientId("App Key")
.clientSecret("App Secret")
.redirectUri("授权回调页")

View File

@@ -25,7 +25,7 @@
- <a href="https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow" target="_blank">Facebook</a>
- <a href="https://www.douyin.com/platform/doc/m-2-1-1" target="_blank">抖音</a>
- <a href="https://docs.microsoft.com/zh-cn/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context" target="_blank">领英</a>
- <a href="https://docs.microsoft.com/zh-cn/graph/auth/" target="_blank">微软</a>
- <a href="https://docs.microsoft.com/zh-cn/graph/auth-v2-user" target="_blank">微软</a>
- <a href="https://dev.mi.com/console/doc/detail?pId=711" target="_blank">小米</a>
- <a href="https://open.mp.toutiao.com/#/resource?_k=y7mfgk" target="_blank">头条</a>
- <a href="https://docs.teambition.com/" target="_blank">Teambition</a>
@@ -41,3 +41,6 @@
- <a href="https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object" target="_blank">用户信息</a>
- <a href="https://developer.twitter.com/en/docs/basics/response-codes" target="_blank">响应码</a>
- <a href="https://developer.twitter.com/en/docs/basics/apps/guides/callback-urls" target="_blank">Callback Urls规则</a>
- <a href="https://help.aliyun.com/document_detail/93696.html?spm=a2c4g.11186623.6.656.1a764138vhg4gr" target="_blank">阿里云</a>
- [Web应用登录阿里云](https://help.aliyun.com/document_detail/93696.html?spm=a2c4g.11186623.6.656.1a764138vhg4gr)
- [通过OIDC获取用户信息](https://help.aliyun.com/document_detail/93698.html?spm=a2c4g.11186623.6.658.537dd19eE1eEJh)

View File

@@ -1,9 +1,11 @@
## 1.15.5-alpha
### 2020/06/07
- 修复
- BUG
- 解决 `Microsoft` 授权失败的 BUG
- 解决 `Coding` 个人账号授权失败的 BUG目前只能使用团队模式进行授权需要传入团队名参考`AuthConfig#codingGroupName`
- 解决 `AuthLinkedinRequest#getAvatar` NPE 的问题。(领英用户没有头像时,原代码会报 NPE
- 新增
- AuthUser 中新增 `rawUserInfo`,用来存放第三方平台返回的原始用户数据。注:淘宝平台的`rawUserInfo`为一个空 JSON
- 支持 Http 级的代理配置,使用方式:
```java
new AuthGoogleRequest(AuthConfig.builder()
@@ -17,15 +19,23 @@ new AuthGoogleRequest(AuthConfig.builder()
.build())
.build());
```
- 修改
- 删除
- 删除**腾讯云登录**。coding 已并入 腾讯云,因此只保留 coding 登录
- 文档
- 新增 [Coding登录](oauth/coding.md)文档
- 完善 [支付宝登录](oauth/alipay.md)文档
- 完善 [贡献者名单](contributors.md)文档
- 完善 [参考文档](references.md)文档
- PR
- 合并 [Gitee!17](https://gitee.com/yadong.zhang/JustAuth/pulls/17)
- 合并 [Gitee!15](https://gitee.com/yadong.zhang/JustAuth/pulls/15)
- 合并 [Github#81](https://github.com/justauth/JustAuth/pull/81)
- Issues
- 解决 [Github#80](https://github.com/justauth/JustAuth/issues/80)
- 解决 [Github#75](https://github.com/justauth/JustAuth/issues/75)
## 1.15.4-alpha
### 2020/05/13
- 修复