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

🔖 1.15.5-alpha

上级 571c3438
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
</p> </p>
<p align="center"> <p align="center">
<a target="_blank" href="https://search.maven.org/search?q=JustAuth"> <a target="_blank" href="https://search.maven.org/search?q=JustAuth">
<img src="https://img.shields.io/badge/Maven%20Central--1.15.4-alpha-blue" ></img> <img src="https://img.shields.io/badge/Maven%20Central--1.15.5-alpha-blue" ></img>
</a> </a>
<a target="_blank" href="https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE"> <a target="_blank" href="https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE">
<img src="https://img.shields.io/apm/l/vim-mode.svg?color=yellow" ></img> <img src="https://img.shields.io/apm/l/vim-mode.svg?color=yellow" ></img>
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<img src="https://img.shields.io/badge/JDK-1.8+-green.svg" ></img> <img src="https://img.shields.io/badge/JDK-1.8+-green.svg" ></img>
</a> </a>
<a target="_blank" href="https://apidoc.gitee.com/yadong.zhang/JustAuth/" title="API文档"> <a target="_blank" href="https://apidoc.gitee.com/yadong.zhang/JustAuth/" title="API文档">
<img src="https://img.shields.io/badge/Api%20Docs--1.15.4-alpha-latest-orange" ></img> <img src="https://img.shields.io/badge/Api%20Docs--1.15.5-alpha-latest-orange" ></img>
</a> </a>
<a target="_blank" href="https://docs.justauth.whnb.wang" title="参考文档"> <a target="_blank" href="https://docs.justauth.whnb.wang" title="参考文档">
<img src="https://img.shields.io/badge/Docs-latest-blueviolet.svg" ></img> <img src="https://img.shields.io/badge/Docs-latest-blueviolet.svg" ></img>
...@@ -97,7 +97,7 @@ These artifacts are available from Maven Central: ...@@ -97,7 +97,7 @@ These artifacts are available from Maven Central:
<dependency> <dependency>
<groupId>me.zhyd.oauth</groupId> <groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId> <artifactId>JustAuth</artifactId>
<version>1.15.4-alpha</version> <version>1.15.5-alpha</version>
</dependency> </dependency>
``` ```
- Using JustAuth - Using JustAuth
...@@ -182,7 +182,7 @@ Note, that since [v1.14.0](https://gitee.com/yadong.zhang/JustAuth/releases/v1.1 ...@@ -182,7 +182,7 @@ Note, that since [v1.14.0](https://gitee.com/yadong.zhang/JustAuth/releases/v1.1
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/facebook.png" width="20"> | [AuthFacebookRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java) | <a href="https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow" target="_blank">参考文档</a> | | <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/facebook.png" width="20"> | [AuthFacebookRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java) | <a href="https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow" target="_blank">参考文档</a> |
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/douyin.png" width="20"> | [AuthDouyinRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java) | <a href="https://www.douyin.com/platform/doc/m-2-1-1" target="_blank">参考文档</a> | | <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/douyin.png" width="20"> | [AuthDouyinRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java) | <a href="https://www.douyin.com/platform/doc/m-2-1-1" target="_blank">参考文档</a> |
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/linkedin.png" width="20"> | [AuthLinkedinRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java) | <a href="https://docs.microsoft.com/zh-cn/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context" target="_blank">参考文档</a> | | <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/linkedin.png" width="20"> | [AuthLinkedinRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthLinkedinRequest.java) | <a href="https://docs.microsoft.com/zh-cn/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context" target="_blank">参考文档</a> |
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/microsoft.png" width="20"> | [AuthMicrosoftRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java) | <a href="https://docs.microsoft.com/zh-cn/graph/auth/" target="_blank">参考文档</a> | | <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/microsoft.png" width="20"> | [AuthMicrosoftRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthMicrosoftRequest.java) | <a href="https://docs.microsoft.com/zh-cn/graph/auth-v2-user" target="_blank">参考文档</a> |
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/mi.png" width="20"> | [AuthMiRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java) | <a href="https://dev.mi.com/console/doc/detail?pId=711" target="_blank">参考文档</a> | | <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/mi.png" width="20"> | [AuthMiRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthMiRequest.java) | <a href="https://dev.mi.com/console/doc/detail?pId=711" target="_blank">参考文档</a> |
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/toutiao.png" width="20"> | [AuthToutiaoRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthToutiaoRequest.java) | <a href="https://open.mp.toutiao.com/#/resource?_k=y7mfgk" target="_blank">参考文档</a> | | <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/toutiao.png" width="20"> | [AuthToutiaoRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthToutiaoRequest.java) | <a href="https://open.mp.toutiao.com/#/resource?_k=y7mfgk" target="_blank">参考文档</a> |
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/teambition.png" width="20"> | [AuthTeambitionRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTeambitionRequest.java) | <a href="https://docs.teambition.com/" target="_blank">参考文档</a> | | <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/teambition.png" width="20"> | [AuthTeambitionRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTeambitionRequest.java) | <a href="https://docs.teambition.com/" target="_blank">参考文档</a> |
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
</p> </p>
<p align="center"> <p align="center">
<a target="_blank" href="https://search.maven.org/search?q=JustAuth"> <a target="_blank" href="https://search.maven.org/search?q=JustAuth">
<img src="https://img.shields.io/badge/Maven%20Central--1.15.4-alpha-blue" ></img> <img src="https://img.shields.io/badge/Maven%20Central--1.15.5-alpha-blue" ></img>
</a> </a>
<a target="_blank" href="https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE"> <a target="_blank" href="https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE">
<img src="https://img.shields.io/apm/l/vim-mode.svg?color=yellow" ></img> <img src="https://img.shields.io/apm/l/vim-mode.svg?color=yellow" ></img>
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<img src="https://img.shields.io/badge/JDK-1.8+-green.svg" ></img> <img src="https://img.shields.io/badge/JDK-1.8+-green.svg" ></img>
</a> </a>
<a target="_blank" href="https://apidoc.gitee.com/yadong.zhang/JustAuth/" title="API文档"> <a target="_blank" href="https://apidoc.gitee.com/yadong.zhang/JustAuth/" title="API文档">
<img src="https://img.shields.io/badge/Api%20Docs--1.15.4-alpha-latest-orange" ></img> <img src="https://img.shields.io/badge/Api%20Docs--1.15.5-alpha-latest-orange" ></img>
</a> </a>
<a target="_blank" href="https://docs.justauth.whnb.wang" title="参考文档"> <a target="_blank" href="https://docs.justauth.whnb.wang" title="参考文档">
<img src="https://img.shields.io/badge/Docs-latest-blueviolet.svg" ></img> <img src="https://img.shields.io/badge/Docs-latest-blueviolet.svg" ></img>
...@@ -96,7 +96,7 @@ JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具 ...@@ -96,7 +96,7 @@ JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具
<dependency> <dependency>
<groupId>me.zhyd.oauth</groupId> <groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId> <artifactId>JustAuth</artifactId>
<version>1.15.4-alpha</version> <version>1.15.5-alpha</version>
</dependency> </dependency>
``` ```
- 调用api - 调用api
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
</p> </p>
<p align="center"> <p align="center">
<a target="_blank" href="https://search.maven.org/search?q=JustAuth"> <a target="_blank" href="https://search.maven.org/search?q=JustAuth">
<img src="https://img.shields.io/badge/Maven%20Central--1.15.4-alpha-blue" ></img> <img src="https://img.shields.io/badge/Maven%20Central--1.15.5-alpha-blue" ></img>
</a> </a>
<a target="_blank" href="https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE"> <a target="_blank" href="https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE">
<img src="https://img.shields.io/apm/l/vim-mode.svg?color=yellow" ></img> <img src="https://img.shields.io/apm/l/vim-mode.svg?color=yellow" ></img>
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
<img src="https://img.shields.io/badge/JDK-1.8+-green.svg" ></img> <img src="https://img.shields.io/badge/JDK-1.8+-green.svg" ></img>
</a> </a>
<a target="_blank" href="https://apidoc.gitee.com/yadong.zhang/JustAuth/" title="API文档"> <a target="_blank" href="https://apidoc.gitee.com/yadong.zhang/JustAuth/" title="API文档">
<img src="https://img.shields.io/badge/Api%20Docs--1.15.4-alpha-latest-orange" ></img> <img src="https://img.shields.io/badge/Api%20Docs--1.15.5-alpha-latest-orange" ></img>
</a> </a>
<a target="_blank" href="https://docs.justauth.whnb.wang" title="参考文档"> <a target="_blank" href="https://docs.justauth.whnb.wang" title="参考文档">
<img src="https://img.shields.io/badge/Docs-latest-blueviolet.svg" ></img> <img src="https://img.shields.io/badge/Docs-latest-blueviolet.svg" ></img>
......
![](_media/justauth@0,25x.png) ![](_media/justauth@0,25x.png)
# JustAuth <small>1.15.4-alpha</small> # JustAuth <small>1.15.5-alpha</small>
<strong>史上最全的整合第三方登录的开源库</strong> <strong>史上最全的整合第三方登录的开源库</strong>
......
...@@ -14,15 +14,14 @@ ...@@ -14,15 +14,14 @@
- [√ 支付宝登录](oauth/alipay.md) - [√ 支付宝登录](oauth/alipay.md)
- [√ 酷家乐登录](oauth/kujiale.md) - [√ 酷家乐登录](oauth/kujiale.md)
- [√ 推特登录](oauth/twitter.md) - [√ 推特登录](oauth/twitter.md)
- [√ Coding登录](oauth/coding.md)
- [CSDN登录](oauth/csdn.md) - [CSDN登录](oauth/csdn.md)
- [微信企业版登录](oauth/wechatEnterprise.md) - [微信企业版登录](oauth/wechatEnterprise.md)
- [微信公众平台登录](oauth/wechat_mp.md) - [微信公众平台登录](oauth/wechat_mp.md)
- [百度登录](oauth/baidu.md) - [百度登录](oauth/baidu.md)
- [Coding登录](oauth/coding.md)
- [钉钉登录](oauth/dingtalk.md) - [钉钉登录](oauth/dingtalk.md)
- [开源中国登录](oauth/oschina.md) - [开源中国登录](oauth/oschina.md)
- [淘宝登录](oauth/taobao.md) - [淘宝登录](oauth/taobao.md)
- [腾讯云登录](oauth/tencentCloud.md)
- [Google登录](oauth/google.md) - [Google登录](oauth/google.md)
- [Facebook登录](oauth/facebook.md) - [Facebook登录](oauth/facebook.md)
- [抖音登录](oauth/douyin.md) - [抖音登录](oauth/douyin.md)
......
...@@ -95,6 +95,14 @@ c. 回到应用详情页面,点击【接口加签方式】后面的【设置 ...@@ -95,6 +95,14 @@ c. 回到应用详情页面,点击【接口加签方式】后面的【设置
</dependency> </dependency>
``` ```
```
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.4.ALL</version>
</dependency>
```
`${latest.version}`表示当前最新的版本,可以在[这儿](https://github.com/justauth/JustAuth/releases)获取最新的版本信息。 `${latest.version}`表示当前最新的版本,可以在[这儿](https://github.com/justauth/JustAuth/releases)获取最新的版本信息。
### 2.2 创建Request ### 2.2 创建Request
......
## 1. 申请应用
## 创建 CODING 应用
1. 点击【个人设置】->【开放生态】->【创建应用】,注册需要接入 CODING 的应用。
![](../_media/oauth/7fded4b4.png)
2. 在创建应用页面,填写接入的应用信息:
- 应用名称、应用主页及回调地址为必填项;
- 其中回调地址是用户授权后,CODING 回调到应用,并且回传授权码的地址;
- 如果没有上传图标将自动生成应用图标。
填写完毕后,点击【创建应用】。
![](../_media/oauth/ca78b40f.png)
注意:回调地址不支持 `127.0.0.1``localhost` 等配置
3. 应用创建完成后,在应用详情页,会生成 Client ID 和 Client Secret ,保存以便后续认证使用。
![](../_media/oauth/ac549f1a.png)
## 2. 集成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 AuthTencentCloudRequest(AuthConfig.builder()
.clientId("App Key")
.clientSecret("App Secret")
.redirectUri("授权回调页")
.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.AuthWeiboRequest;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
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);
}
@RequestMapping("/revoke/{token}")
public Object revokeAuth(@PathVariable("token") String token) throws IOException {
AuthRequest authRequest = getAuthRequest();
return authRequest.revoke(AuthToken.builder().accessToken(token).build());
}
private AuthRequest getAuthRequest() {
return new AuthTencentCloudRequest(AuthConfig.builder()
.clientId("App Key")
.clientSecret("App Secret")
.redirectUri("授权回调页")
// 比如以“ https://justauth.coding.net/ ”为例,codingGroupName = justauth
.codingGroupName("团队名")
.build());
}
}
```
授权链接访问成功后会看到以下页面内容:
![](../_media/oauth/2f1b9c2c.png)
## 3. 授权结果
```json
{
"code": 2000,
"msg": null,
"data": {
"uuid": "xxx",
"username": "yadong.zhang",
"nickname": "yadong.zhang",
"avatar": "https://coding.net/static/fruit_avatar/Fruit-6.png",
"blog": "https://coding.net/u/HNMxPTOFRd",
"company": "",
"location": "",
"email": null,
"remark": "",
"gender": "FEMALE",
"source": "CODING",
"token": {
"accessToken": "xx",
"expireIn": 858292,
"refreshToken": "xx",
"uid": null,
"openId": null,
"accessCode": null,
"unionId": null,
"scope": null,
"tokenType": null,
"idToken": null,
"macAlgorithm": null,
"macKey": null,
"code": null,
"oauthToken": null,
"oauthTokenSecret": null,
"userId": null,
"screenName": null,
"oauthCallbackConfirmed": null
}
}
}
```
\ No newline at end of file
## 1.15.5-alpha
### 2020/06/07
- 修复
- 解决 `Microsoft` 授权失败的 BUG
- 解决 `Coding` 个人账号授权失败的 BUG(目前只能使用团队模式进行授权,需要传入团队名,参考`AuthConfig#codingGroupName`
- 新增
- 支持 Http 级的代理配置,使用方式:
```java
new AuthGoogleRequest(AuthConfig.builder()
.clientId("")
.clientSecret("")
.redirectUri("http://127.0.0.1:8443/oauth/callback/google")
// 针对国外平台配置代理
.httpConfig(HttpConfig.builder()
.timeout(15000)
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
.build())
.build());
```
- 删除
- 删除**腾讯云登录**。coding 已并入 腾讯云,因此只保留 coding 登录
- 文档
- 新增 [Coding登录](oauth/coding.md)文档
- 完善 [支付宝登录](oauth/alipay.md)文档
- PR
- 合并 [Gitee!17](https://gitee.com/yadong.zhang/JustAuth/pulls/17)
- 合并 [Gitee!15](https://gitee.com/yadong.zhang/JustAuth/pulls/15)
## 1.15.4-alpha ## 1.15.4-alpha
### 2020/05/13 ### 2020/05/13
- 修复 - 修复
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<groupId>me.zhyd.oauth</groupId> <groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId> <artifactId>JustAuth</artifactId>
<version>1.15.4-alpha</version> <version>1.15.5-alpha</version>
<name>JustAuth</name> <name>JustAuth</name>
<url>https://gitee.com/yadong.zhang/JustAuth</url> <url>https://gitee.com/yadong.zhang/JustAuth</url>
...@@ -60,7 +60,7 @@ ...@@ -60,7 +60,7 @@
<maven-surefire-version>2.20</maven-surefire-version> <maven-surefire-version>2.20</maven-surefire-version>
<maven-gpg-version>1.6</maven-gpg-version> <maven-gpg-version>1.6</maven-gpg-version>
<maven.test.skip>false</maven.test.skip> <maven.test.skip>false</maven.test.skip>
<simple-http.version>1.0.1</simple-http.version> <simple-http.version>1.0.2-SNAPSHOT</simple-http.version>
<lombok-version>1.18.10</lombok-version> <lombok-version>1.18.10</lombok-version>
<junit-version>4.11</junit-version> <junit-version>4.11</junit-version>
<fastjson-version>1.2.69</fastjson-version> <fastjson-version>1.2.69</fastjson-version>
......
package me.zhyd.oauth.config; package me.zhyd.oauth.config;
import com.xkcoding.http.config.HttpConfig;
import lombok.*; import lombok.*;
/** /**
...@@ -59,4 +60,23 @@ public class AuthConfig { ...@@ -59,4 +60,23 @@ public class AuthConfig {
* @since 1.10.0 * @since 1.10.0
*/ */
private String agentId; private String agentId;
/**
* 使用 Coding 登录时,需要传该值。
*
* 团队域名前缀,比如以“ https://justauth.coding.net/ ”为例,{@code codingGroupName} = justauth
*
* @since 1.15.5
*/
private String codingGroupName;
/**
* 针对国外服务可以单独设置代理
* HttpConfig config = new HttpConfig();
* config.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)));
* config.setTimeout(15000);
*
* @since 1.15.5
*/
private HttpConfig httpConfig;
} }
...@@ -140,41 +140,25 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -140,41 +140,25 @@ public enum AuthDefaultSource implements AuthSource {
} }
}, },
/** /**
* Coding * Coding,
*
* 参考 https://help.coding.net/docs/project/open/oauth.html#%E7%94%A8%E6%88%B7%E6%8E%88%E6%9D%83 中的说明,
* 新版的 coding API 地址需要传入用户团队名,这儿使用动态参数,方便在 request 中使用
*/ */
CODING { CODING {
@Override @Override
public String authorize() { public String authorize() {
return "https://coding.net/oauth_authorize.html"; return "https://%s.coding.net/oauth_authorize.html";
}
@Override
public String accessToken() {
return "https://coding.net/api/oauth/access_token";
}
@Override
public String userInfo() {
return "https://coding.net/api/account/current_user";
}
},
/**
* 腾讯云开发者平台(coding升级后就变成腾讯云开发者平台了)
*/
TENCENT_CLOUD {
@Override
public String authorize() {
return "https://dev.tencent.com/oauth_authorize.html";
} }
@Override @Override
public String accessToken() { public String accessToken() {
return "https://dev.tencent.com/api/oauth/access_token"; return "https://%s.coding.net/api/oauth/access_token";
} }
@Override @Override
public String userInfo() { public String userInfo() {
return "https://dev.tencent.com/api/account/current_user"; return "https://%s.coding.net/api/account/current_user";
} }
}, },
/** /**
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -37,13 +37,20 @@ public class AuthBaiduRequest extends AuthDefaultRequest { ...@@ -37,13 +37,20 @@ public class AuthBaiduRequest extends AuthDefaultRequest {
return getAuthToken(response); return getAuthToken(response);
} }
/**
* https://openapi.baidu.com/rest/2.0/passport/users/getInfo?access_token=121.c86e87cc0828cc1dabb8faee540531d4.YsUIAWvYbgqVni1VhkgKgyLh8nEyELbDOEZs_OA.OgDgmA
* https://openapi.baidu.com/rest/2.0/passport/users/getInfo?access_token=121.2907d9facf9fb97adf7287fa75496eda.Y3NSjR3-3HKt1RgT0HEl7GgxRXT5gOOVdngXezY.OcC_7g
* 新旧应用返回的用户信息不一致
* @param authToken token信息
* @return AuthUser
*/
@Override @Override
protected AuthUser getUserInfo(AuthToken authToken) { protected AuthUser getUserInfo(AuthToken authToken) {
String userInfo = doGetUserInfo(authToken); String userInfo = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(userInfo); JSONObject object = JSONObject.parseObject(userInfo);
this.checkResponse(object); this.checkResponse(object);
return AuthUser.builder() return AuthUser.builder()
.uuid(object.getString("userid")) .uuid(object.containsKey("userid") ? object.getString("userid") : object.getString("openid"))
.username(object.getString("username")) .username(object.getString("username"))
.nickname(object.getString("username")) .nickname(object.getString("username"))
.avatar(getAvatar(object)) .avatar(getAvatar(object))
...@@ -77,7 +84,7 @@ public class AuthBaiduRequest extends AuthDefaultRequest { ...@@ -77,7 +84,7 @@ public class AuthBaiduRequest extends AuthDefaultRequest {
.queryParam("client_id", this.config.getClientId()) .queryParam("client_id", this.config.getClientId())
.queryParam("client_secret", this.config.getClientSecret()) .queryParam("client_secret", this.config.getClientSecret())
.build(); .build();
String response = HttpUtil.get(refreshUrl); String response = new HttpUtils(config.getHttpConfig()).get(refreshUrl);
return AuthResponse.builder() return AuthResponse.builder()
.code(AuthResponseStatus.SUCCESS.getCode()) .code(AuthResponseStatus.SUCCESS.getCode())
.data(this.getAuthToken(response)) .data(this.getAuthToken(response))
......
...@@ -49,8 +49,8 @@ public class AuthCodingRequest extends AuthDefaultRequest { ...@@ -49,8 +49,8 @@ public class AuthCodingRequest extends AuthDefaultRequest {
return AuthUser.builder() return AuthUser.builder()
.uuid(object.getString("id")) .uuid(object.getString("id"))
.username(object.getString("name")) .username(object.getString("name"))
.avatar("https://coding.net/" + object.getString("avatar")) .avatar("https://coding.net" + object.getString("avatar"))
.blog("https://coding.net/" + object.getString("path")) .blog("https://coding.net" + object.getString("path"))
.nickname(object.getString("name")) .nickname(object.getString("name"))
.company(object.getString("company")) .company(object.getString("company"))
.location(object.getString("location")) .location(object.getString("location"))
...@@ -82,7 +82,7 @@ public class AuthCodingRequest extends AuthDefaultRequest { ...@@ -82,7 +82,7 @@ public class AuthCodingRequest extends AuthDefaultRequest {
*/ */
@Override @Override
public String authorize(String state) { public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize()) return UrlBuilder.fromBaseUrl(String.format(source.authorize(), config.getCodingGroupName()))
.queryParam("response_type", "code") .queryParam("response_type", "code")
.queryParam("client_id", config.getClientId()) .queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri()) .queryParam("redirect_uri", config.getRedirectUri())
...@@ -90,4 +90,33 @@ public class AuthCodingRequest extends AuthDefaultRequest { ...@@ -90,4 +90,33 @@ public class AuthCodingRequest extends AuthDefaultRequest {
.queryParam("state", getRealState(state)) .queryParam("state", getRealState(state))
.build(); .build();
} }
/**
* 返回获取accessToken的url
*
* @param code 授权码
* @return 返回获取accessToken的url
*/
@Override
public String accessTokenUrl(String code) {
return UrlBuilder.fromBaseUrl(String.format(source.accessToken(), config.getCodingGroupName()))
.queryParam("code", code)
.queryParam("client_id", config.getClientId())
.queryParam("client_secret", config.getClientSecret())
.queryParam("grant_type", "authorization_code")
.queryParam("redirect_uri", config.getRedirectUri())
.build();
}
/**
* 返回获取userInfo的url
*
* @param authToken token
* @return 返回获取userInfo的url
*/
@Override
public String userInfoUrl(AuthToken authToken) {
return UrlBuilder.fromBaseUrl(String.format(source.userInfo(), config.getCodingGroupName()))
.queryParam("access_token", authToken.getAccessToken())
.build();
}
} }
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthDefaultStateCache; import me.zhyd.oauth.cache.AuthDefaultStateCache;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
...@@ -210,7 +210,7 @@ public abstract class AuthDefaultRequest implements AuthRequest { ...@@ -210,7 +210,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
* @return Response * @return Response
*/ */
protected String doPostAuthorizationCode(String code) { protected String doPostAuthorizationCode(String code) {
return HttpUtil.post(accessTokenUrl(code)); return new HttpUtils(config.getHttpConfig()).post(accessTokenUrl(code));
} }
/** /**
...@@ -220,7 +220,7 @@ public abstract class AuthDefaultRequest implements AuthRequest { ...@@ -220,7 +220,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
* @return Response * @return Response
*/ */
protected String doGetAuthorizationCode(String code) { protected String doGetAuthorizationCode(String code) {
return HttpUtil.get(accessTokenUrl(code)); return new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(code));
} }
/** /**
...@@ -231,7 +231,7 @@ public abstract class AuthDefaultRequest implements AuthRequest { ...@@ -231,7 +231,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
*/ */
@Deprecated @Deprecated
protected String doPostUserInfo(AuthToken authToken) { protected String doPostUserInfo(AuthToken authToken) {
return HttpUtil.post(userInfoUrl(authToken)); return new HttpUtils(config.getHttpConfig()).post(userInfoUrl(authToken));
} }
/** /**
...@@ -241,7 +241,7 @@ public abstract class AuthDefaultRequest implements AuthRequest { ...@@ -241,7 +241,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
* @return Response * @return Response
*/ */
protected String doGetUserInfo(AuthToken authToken) { protected String doGetUserInfo(AuthToken authToken) {
return HttpUtil.get(userInfoUrl(authToken)); return new HttpUtils(config.getHttpConfig()).get(userInfoUrl(authToken));
} }
/** /**
...@@ -252,7 +252,7 @@ public abstract class AuthDefaultRequest implements AuthRequest { ...@@ -252,7 +252,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
*/ */
@Deprecated @Deprecated
protected String doPostRevoke(AuthToken authToken) { protected String doPostRevoke(AuthToken authToken) {
return HttpUtil.post(revokeUrl(authToken)); return new HttpUtils(config.getHttpConfig()).post(revokeUrl(authToken));
} }
/** /**
...@@ -262,7 +262,7 @@ public abstract class AuthDefaultRequest implements AuthRequest { ...@@ -262,7 +262,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
* @return Response * @return Response
*/ */
protected String doGetRevoke(AuthToken authToken) { protected String doGetRevoke(AuthToken authToken) {
return HttpUtil.get(revokeUrl(authToken)); return new HttpUtils(config.getHttpConfig()).get(revokeUrl(authToken));
} }
} }
...@@ -2,7 +2,7 @@ package me.zhyd.oauth.request; ...@@ -2,7 +2,7 @@ package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -40,7 +40,7 @@ public class AuthDingTalkRequest extends AuthDefaultRequest { ...@@ -40,7 +40,7 @@ public class AuthDingTalkRequest extends AuthDefaultRequest {
String code = authToken.getAccessCode(); String code = authToken.getAccessCode();
JSONObject param = new JSONObject(); JSONObject param = new JSONObject();
param.put("tmp_auth_code", code); param.put("tmp_auth_code", code);
String response = HttpUtil.post(userInfoUrl(authToken), param.toJSONString()); String response = new HttpUtils(config.getHttpConfig()).post(userInfoUrl(authToken), param.toJSONString());
JSONObject object = JSON.parseObject(response); JSONObject object = JSON.parseObject(response);
if (object.getIntValue("errcode") != 0) { if (object.getIntValue("errcode") != 0) {
throw new AuthException(object.getString("errmsg")); throw new AuthException(object.getString("errmsg"));
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -84,7 +84,7 @@ public class AuthDouyinRequest extends AuthDefaultRequest { ...@@ -84,7 +84,7 @@ public class AuthDouyinRequest extends AuthDefaultRequest {
* @return token对象 * @return token对象
*/ */
private AuthToken getToken(String accessTokenUrl) { private AuthToken getToken(String accessTokenUrl) {
String response = HttpUtil.post(accessTokenUrl); String response = new HttpUtils(config.getHttpConfig()).post(accessTokenUrl);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
JSONObject dataObj = object.getJSONObject("data"); JSONObject dataObj = object.getJSONObject("data");
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.constants.Constants; import com.xkcoding.http.constants.Constants;
import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.support.HttpHeader;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
...@@ -52,7 +52,7 @@ public class AuthElemeRequest extends AuthDefaultRequest { ...@@ -52,7 +52,7 @@ public class AuthElemeRequest extends AuthDefaultRequest {
form.put("grant_type", "authorization_code"); form.put("grant_type", "authorization_code");
HttpHeader httpHeader = this.buildHeader(CONTENT_TYPE_FORM, this.getRequestId(), true); HttpHeader httpHeader = this.buildHeader(CONTENT_TYPE_FORM, this.getRequestId(), true);
String response = HttpUtil.post(source.accessToken(), form, httpHeader, false); String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), form, httpHeader, false);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
...@@ -91,7 +91,7 @@ public class AuthElemeRequest extends AuthDefaultRequest { ...@@ -91,7 +91,7 @@ public class AuthElemeRequest extends AuthDefaultRequest {
paramsMap.put("signature", signature); paramsMap.put("signature", signature);
HttpHeader httpHeader = this.buildHeader(CONTENT_TYPE_JSON, requestId, false); HttpHeader httpHeader = this.buildHeader(CONTENT_TYPE_JSON, requestId, false);
String response = HttpUtil.post(source.userInfo(), JSONObject.toJSONString(paramsMap), httpHeader); String response = new HttpUtils(config.getHttpConfig()).post(source.userInfo(), JSONObject.toJSONString(paramsMap), httpHeader);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
...@@ -122,7 +122,7 @@ public class AuthElemeRequest extends AuthDefaultRequest { ...@@ -122,7 +122,7 @@ public class AuthElemeRequest extends AuthDefaultRequest {
form.put("grant_type", "refresh_token"); form.put("grant_type", "refresh_token");
HttpHeader httpHeader = this.buildHeader(CONTENT_TYPE_FORM, this.getRequestId(), true); HttpHeader httpHeader = this.buildHeader(CONTENT_TYPE_FORM, this.getRequestId(), true);
String response = HttpUtil.post(source.refresh(), form, httpHeader, false); String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, httpHeader, false);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
......
...@@ -2,7 +2,6 @@ package me.zhyd.oauth.request; ...@@ -2,7 +2,6 @@ package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil;
import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.support.HttpHeader;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -13,6 +12,7 @@ import me.zhyd.oauth.model.AuthResponse; ...@@ -13,6 +12,7 @@ import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.GlobalAuthUtils; import me.zhyd.oauth.utils.GlobalAuthUtils;
import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.utils.UrlBuilder; import me.zhyd.oauth.utils.UrlBuilder;
/** /**
...@@ -36,7 +36,7 @@ public class AuthFeishuRequest extends AuthDefaultRequest { ...@@ -36,7 +36,7 @@ public class AuthFeishuRequest extends AuthDefaultRequest {
requestObject.put("app_secret", config.getClientSecret()); requestObject.put("app_secret", config.getClientSecret());
requestObject.put("grant_type", "authorization_code"); requestObject.put("grant_type", "authorization_code");
requestObject.put("code", authCallback.getCode()); requestObject.put("code", authCallback.getCode());
String response = HttpUtil.post(source.accessToken(), requestObject.toJSONString(), new HttpHeader() String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), requestObject.toJSONString(), new HttpHeader()
.add("Content-Type", "application/json")); .add("Content-Type", "application/json"));
JSONObject jsonObject = JSON.parseObject(response); JSONObject jsonObject = JSON.parseObject(response);
this.checkResponse(jsonObject); this.checkResponse(jsonObject);
...@@ -53,7 +53,7 @@ public class AuthFeishuRequest extends AuthDefaultRequest { ...@@ -53,7 +53,7 @@ public class AuthFeishuRequest extends AuthDefaultRequest {
@Override @Override
protected AuthUser getUserInfo(AuthToken authToken) { protected AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken(); String accessToken = authToken.getAccessToken();
String response = HttpUtil.get(source.userInfo(), null, new HttpHeader() String response = new HttpUtils(config.getHttpConfig()).get(source.userInfo(), null, new HttpHeader()
.add("Content-Type", "application/json") .add("Content-Type", "application/json")
.add("Authorization", "Bearer " + accessToken), false); .add("Authorization", "Bearer " + accessToken), false);
JSONObject jsonObject = JSON.parseObject(response); JSONObject jsonObject = JSON.parseObject(response);
...@@ -72,7 +72,7 @@ public class AuthFeishuRequest extends AuthDefaultRequest { ...@@ -72,7 +72,7 @@ public class AuthFeishuRequest extends AuthDefaultRequest {
requestObject.put("app_secret", config.getClientSecret()); requestObject.put("app_secret", config.getClientSecret());
requestObject.put("grant_type", "refresh_token"); requestObject.put("grant_type", "refresh_token");
requestObject.put("refresh_token", authToken.getRefreshToken()); requestObject.put("refresh_token", authToken.getRefreshToken());
String response = HttpUtil.post(source.refresh(), requestObject.toJSONString(), new HttpHeader() String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), requestObject.toJSONString(), new HttpHeader()
.add("Content-Type", "application/json")); .add("Content-Type", "application/json"));
JSONObject jsonObject = JSON.parseObject(response); JSONObject jsonObject = JSON.parseObject(response);
this.checkResponse(jsonObject); this.checkResponse(jsonObject);
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.support.HttpHeader;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
...@@ -47,7 +47,7 @@ public class AuthGoogleRequest extends AuthDefaultRequest { ...@@ -47,7 +47,7 @@ public class AuthGoogleRequest extends AuthDefaultRequest {
protected AuthUser getUserInfo(AuthToken authToken) { protected AuthUser getUserInfo(AuthToken authToken) {
HttpHeader httpHeader = new HttpHeader(); HttpHeader httpHeader = new HttpHeader();
httpHeader.add("Authorization", "Bearer " + authToken.getAccessToken()); httpHeader.add("Authorization", "Bearer " + authToken.getAccessToken());
String userInfo = HttpUtil.post(userInfoUrl(authToken), null, httpHeader); String userInfo = new HttpUtils(config.getHttpConfig()).post(userInfoUrl(authToken), null, httpHeader);
JSONObject object = JSONObject.parseObject(userInfo); JSONObject object = JSONObject.parseObject(userInfo);
this.checkResponse(object); this.checkResponse(object);
return AuthUser.builder() return AuthUser.builder()
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -52,7 +52,7 @@ public class AuthHuaweiRequest extends AuthDefaultRequest { ...@@ -52,7 +52,7 @@ public class AuthHuaweiRequest extends AuthDefaultRequest {
form.put("client_secret", config.getClientSecret()); form.put("client_secret", config.getClientSecret());
form.put("redirect_uri", config.getRedirectUri()); form.put("redirect_uri", config.getRedirectUri());
String response = HttpUtil.post(source.accessToken(), form, false); String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), form, false);
return getAuthToken(response); return getAuthToken(response);
} }
...@@ -71,7 +71,7 @@ public class AuthHuaweiRequest extends AuthDefaultRequest { ...@@ -71,7 +71,7 @@ public class AuthHuaweiRequest extends AuthDefaultRequest {
form.put("nsp_fmt", "JS"); form.put("nsp_fmt", "JS");
form.put("nsp_svc", "OpenUP.User.getInfo"); form.put("nsp_svc", "OpenUP.User.getInfo");
String response = HttpUtil.post(source.userInfo(), form, false); String response = new HttpUtils(config.getHttpConfig()).post(source.userInfo(), form, false);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
...@@ -103,7 +103,7 @@ public class AuthHuaweiRequest extends AuthDefaultRequest { ...@@ -103,7 +103,7 @@ public class AuthHuaweiRequest extends AuthDefaultRequest {
form.put("refresh_token", authToken.getRefreshToken()); form.put("refresh_token", authToken.getRefreshToken());
form.put("grant_type", "refresh_token"); form.put("grant_type", "refresh_token");
String response = HttpUtil.post(source.refresh(), form, false); String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false);
return AuthResponse.builder().code(SUCCESS.getCode()).data(getAuthToken(response)).build(); return AuthResponse.builder().code(SUCCESS.getCode()).data(getAuthToken(response)).build();
} }
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -44,7 +44,7 @@ public class AuthJdRequest extends AuthDefaultRequest { ...@@ -44,7 +44,7 @@ public class AuthJdRequest extends AuthDefaultRequest {
params.put("app_secret", config.getClientSecret()); params.put("app_secret", config.getClientSecret());
params.put("grant_type", "authorization_code"); params.put("grant_type", "authorization_code");
params.put("code", authCallback.getCode()); params.put("code", authCallback.getCode());
String response = HttpUtil.post(source.accessToken(), params, false); String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), params, false);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
...@@ -68,7 +68,7 @@ public class AuthJdRequest extends AuthDefaultRequest { ...@@ -68,7 +68,7 @@ public class AuthJdRequest extends AuthDefaultRequest {
.queryParam("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) .queryParam("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
.queryParam("v", "2.0"); .queryParam("v", "2.0");
urlBuilder.queryParam("sign", GlobalAuthUtils.generateJdSignature(config.getClientSecret(), urlBuilder.getReadOnlyParams())); urlBuilder.queryParam("sign", GlobalAuthUtils.generateJdSignature(config.getClientSecret(), urlBuilder.getReadOnlyParams()));
String response = HttpUtil.post(urlBuilder.build(true)); String response = new HttpUtils(config.getHttpConfig()).post(urlBuilder.build(true));
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
...@@ -106,7 +106,7 @@ public class AuthJdRequest extends AuthDefaultRequest { ...@@ -106,7 +106,7 @@ public class AuthJdRequest extends AuthDefaultRequest {
params.put("app_secret", config.getClientSecret()); params.put("app_secret", config.getClientSecret());
params.put("grant_type", "refresh_token"); params.put("grant_type", "refresh_token");
params.put("refresh_token", oldToken.getRefreshToken()); params.put("refresh_token", oldToken.getRefreshToken());
String response = HttpUtil.post(source.refresh(), params, false); String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), params, false);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -90,7 +90,7 @@ public class AuthKujialeRequest extends AuthDefaultRequest { ...@@ -90,7 +90,7 @@ public class AuthKujialeRequest extends AuthDefaultRequest {
@Override @Override
public AuthUser getUserInfo(AuthToken authToken) { public AuthUser getUserInfo(AuthToken authToken) {
String openId = this.getOpenId(authToken); String openId = this.getOpenId(authToken);
String response = HttpUtil.get(UrlBuilder.fromBaseUrl(source.userInfo()) String response = new HttpUtils(config.getHttpConfig()).get(UrlBuilder.fromBaseUrl(source.userInfo())
.queryParam("access_token", authToken.getAccessToken()) .queryParam("access_token", authToken.getAccessToken())
.queryParam("open_id", openId) .queryParam("open_id", openId)
.build()); .build());
...@@ -117,7 +117,7 @@ public class AuthKujialeRequest extends AuthDefaultRequest { ...@@ -117,7 +117,7 @@ public class AuthKujialeRequest extends AuthDefaultRequest {
* @return openId * @return openId
*/ */
private String getOpenId(AuthToken authToken) { private String getOpenId(AuthToken authToken) {
String response = HttpUtil.get(UrlBuilder.fromBaseUrl("https://oauth.kujiale.com/oauth2/auth/user") String response = new HttpUtils(config.getHttpConfig()).get(UrlBuilder.fromBaseUrl("https://oauth.kujiale.com/oauth2/auth/user")
.queryParam("access_token", authToken.getAccessToken()) .queryParam("access_token", authToken.getAccessToken())
.build()); .build());
JSONObject accessTokenObject = checkResponse(response); JSONObject accessTokenObject = checkResponse(response);
...@@ -126,7 +126,7 @@ public class AuthKujialeRequest extends AuthDefaultRequest { ...@@ -126,7 +126,7 @@ public class AuthKujialeRequest extends AuthDefaultRequest {
@Override @Override
public AuthResponse refresh(AuthToken authToken) { public AuthResponse refresh(AuthToken authToken) {
String response = HttpUtil.post(refreshTokenUrl(authToken.getRefreshToken())); String response = new HttpUtils(config.getHttpConfig()).post(refreshTokenUrl(authToken.getRefreshToken()));
return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(getAuthToken(response)).build(); return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(getAuthToken(response)).build();
} }
} }
...@@ -3,7 +3,7 @@ package me.zhyd.oauth.request; ...@@ -3,7 +3,7 @@ package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath; import com.alibaba.fastjson.JSONPath;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.constants.Constants; import com.xkcoding.http.constants.Constants;
import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.support.HttpHeader;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
...@@ -49,7 +49,7 @@ public class AuthLinkedinRequest extends AuthDefaultRequest { ...@@ -49,7 +49,7 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
httpHeader.add("Connection", "Keep-Alive"); httpHeader.add("Connection", "Keep-Alive");
httpHeader.add("Authorization", "Bearer " + accessToken); httpHeader.add("Authorization", "Bearer " + accessToken);
String response = HttpUtil.get(userInfoUrl(authToken), null, httpHeader, false); String response = new HttpUtils(config.getHttpConfig()).get(userInfoUrl(authToken), null, httpHeader, false);
JSONObject userInfoObject = JSONObject.parseObject(response); JSONObject userInfoObject = JSONObject.parseObject(response);
this.checkResponse(userInfoObject); this.checkResponse(userInfoObject);
...@@ -128,7 +128,7 @@ public class AuthLinkedinRequest extends AuthDefaultRequest { ...@@ -128,7 +128,7 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
httpHeader.add("Connection", "Keep-Alive"); httpHeader.add("Connection", "Keep-Alive");
httpHeader.add("Authorization", "Bearer " + accessToken); httpHeader.add("Authorization", "Bearer " + accessToken);
String emailResponse = HttpUtil.get("https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))", null, httpHeader, false); String emailResponse = new HttpUtils(config.getHttpConfig()).get("https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))", null, httpHeader, false);
JSONObject emailObj = JSONObject.parseObject(emailResponse); JSONObject emailObj = JSONObject.parseObject(emailResponse);
this.checkResponse(emailObj); this.checkResponse(emailObj);
...@@ -181,7 +181,7 @@ public class AuthLinkedinRequest extends AuthDefaultRequest { ...@@ -181,7 +181,7 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
httpHeader.add("Host", "www.linkedin.com"); httpHeader.add("Host", "www.linkedin.com");
httpHeader.add(Constants.CONTENT_TYPE, "application/x-www-form-urlencoded"); httpHeader.add(Constants.CONTENT_TYPE, "application/x-www-form-urlencoded");
String response = HttpUtil.post(accessTokenUrl, null, httpHeader); String response = new HttpUtils(config.getHttpConfig()).post(accessTokenUrl, null, httpHeader);
JSONObject accessTokenObject = JSONObject.parseObject(response); JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject); this.checkResponse(accessTokenObject);
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -41,7 +41,7 @@ public class AuthMeituanRequest extends AuthDefaultRequest { ...@@ -41,7 +41,7 @@ public class AuthMeituanRequest extends AuthDefaultRequest {
form.put("code", authCallback.getCode()); form.put("code", authCallback.getCode());
form.put("grant_type", "authorization_code"); form.put("grant_type", "authorization_code");
String response = HttpUtil.post(source.accessToken(), form, false); String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), form, false);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
...@@ -60,7 +60,7 @@ public class AuthMeituanRequest extends AuthDefaultRequest { ...@@ -60,7 +60,7 @@ public class AuthMeituanRequest extends AuthDefaultRequest {
form.put("secret", config.getClientSecret()); form.put("secret", config.getClientSecret());
form.put("access_token", authToken.getAccessToken()); form.put("access_token", authToken.getAccessToken());
String response = HttpUtil.post(source.userInfo(), form, false); String response = new HttpUtils(config.getHttpConfig()).post(source.userInfo(), form, false);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
...@@ -84,7 +84,7 @@ public class AuthMeituanRequest extends AuthDefaultRequest { ...@@ -84,7 +84,7 @@ public class AuthMeituanRequest extends AuthDefaultRequest {
form.put("refresh_token", oldToken.getRefreshToken()); form.put("refresh_token", oldToken.getRefreshToken());
form.put("grant_type", "refresh_token"); form.put("grant_type", "refresh_token");
String response = HttpUtil.post(source.refresh(), form, false); String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.constants.Constants; import com.xkcoding.http.constants.Constants;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
...@@ -41,7 +41,7 @@ public class AuthMiRequest extends AuthDefaultRequest { ...@@ -41,7 +41,7 @@ public class AuthMiRequest extends AuthDefaultRequest {
} }
private AuthToken getToken(String accessTokenUrl) { private AuthToken getToken(String accessTokenUrl) {
String response = HttpUtil.get(accessTokenUrl); String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl);
String jsonStr = response.replace(PREFIX, Constants.EMPTY); String jsonStr = response.replace(PREFIX, Constants.EMPTY);
JSONObject accessTokenObject = JSONObject.parseObject(jsonStr); JSONObject accessTokenObject = JSONObject.parseObject(jsonStr);
...@@ -88,7 +88,7 @@ public class AuthMiRequest extends AuthDefaultRequest { ...@@ -88,7 +88,7 @@ public class AuthMiRequest extends AuthDefaultRequest {
String emailPhoneUrl = MessageFormat.format("{0}?clientId={1}&token={2}", "https://open.account.xiaomi.com/user/phoneAndEmail", config String emailPhoneUrl = MessageFormat.format("{0}?clientId={1}&token={2}", "https://open.account.xiaomi.com/user/phoneAndEmail", config
.getClientId(), authToken.getAccessToken()); .getClientId(), authToken.getAccessToken());
String emailResponse = HttpUtil.get(emailPhoneUrl); String emailResponse = new HttpUtils(config.getHttpConfig()).get(emailPhoneUrl);
JSONObject userEmailPhone = JSONObject.parseObject(emailResponse); JSONObject userEmailPhone = JSONObject.parseObject(emailResponse);
if (!"error".equalsIgnoreCase(userEmailPhone.getString("result"))) { if (!"error".equalsIgnoreCase(userEmailPhone.getString("result"))) {
JSONObject emailPhone = userEmailPhone.getJSONObject("data"); JSONObject emailPhone = userEmailPhone.getJSONObject("data");
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.constants.Constants; import com.xkcoding.http.constants.Constants;
import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.support.HttpHeader;
import com.xkcoding.http.util.MapUtil; import com.xkcoding.http.util.MapUtil;
...@@ -47,12 +47,10 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest { ...@@ -47,12 +47,10 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
*/ */
private AuthToken getToken(String accessTokenUrl) { private AuthToken getToken(String accessTokenUrl) {
HttpHeader httpHeader = new HttpHeader(); HttpHeader httpHeader = new HttpHeader();
httpHeader.add("Host", "https://login.microsoftonline.com");
httpHeader.add(Constants.CONTENT_TYPE, "application/x-www-form-urlencoded");
Map<String, String> form = MapUtil.parseStringToMap(accessTokenUrl, false); Map<String, String> form = MapUtil.parseStringToMap(accessTokenUrl, false);
String response = HttpUtil.post(accessTokenUrl, form, httpHeader, false); String response = new HttpUtils(config.getHttpConfig()).post(accessTokenUrl, form, httpHeader, false);
JSONObject accessTokenObject = JSONObject.parseObject(response); JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject); this.checkResponse(accessTokenObject);
...@@ -86,7 +84,7 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest { ...@@ -86,7 +84,7 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
HttpHeader httpHeader = new HttpHeader(); HttpHeader httpHeader = new HttpHeader();
httpHeader.add("Authorization", jwt); httpHeader.add("Authorization", jwt);
String userInfo = HttpUtil.get(userInfoUrl(authToken), null, httpHeader, false); String userInfo = new HttpUtils(config.getHttpConfig()).get(userInfoUrl(authToken), null, httpHeader, false);
JSONObject object = JSONObject.parseObject(userInfo); JSONObject object = JSONObject.parseObject(userInfo);
this.checkResponse(object); this.checkResponse(object);
return AuthUser.builder() return AuthUser.builder()
...@@ -129,7 +127,7 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest { ...@@ -129,7 +127,7 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
.queryParam("client_id", config.getClientId()) .queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri()) .queryParam("redirect_uri", config.getRedirectUri())
.queryParam("response_mode", "query") .queryParam("response_mode", "query")
.queryParam("scope", "offline_access%20user.read%20mail.read") .queryParam("scope", "offline_access user.read mail.read")
.queryParam("state", getRealState(state)) .queryParam("state", getRealState(state))
.build(); .build();
} }
...@@ -147,7 +145,7 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest { ...@@ -147,7 +145,7 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
.queryParam("client_id", config.getClientId()) .queryParam("client_id", config.getClientId())
.queryParam("client_secret", config.getClientSecret()) .queryParam("client_secret", config.getClientSecret())
.queryParam("grant_type", "authorization_code") .queryParam("grant_type", "authorization_code")
.queryParam("scope", "user.read%20mail.read") .queryParam("scope", "offline_access user.read mail.read")
.queryParam("redirect_uri", config.getRedirectUri()) .queryParam("redirect_uri", config.getRedirectUri())
.build(); .build();
} }
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.enums.AuthUserGender; import me.zhyd.oauth.enums.AuthUserGender;
...@@ -48,7 +48,7 @@ public class AuthPinterestRequest extends AuthDefaultRequest { ...@@ -48,7 +48,7 @@ public class AuthPinterestRequest extends AuthDefaultRequest {
protected AuthUser getUserInfo(AuthToken authToken) { protected AuthUser getUserInfo(AuthToken authToken) {
String userinfoUrl = userInfoUrl(authToken); String userinfoUrl = userInfoUrl(authToken);
// TODO: 是否需要 .setFollowRedirects(true) // TODO: 是否需要 .setFollowRedirects(true)
String response = HttpUtil.get(userinfoUrl); String response = new HttpUtils(config.getHttpConfig()).get(userinfoUrl);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
JSONObject userObj = object.getJSONObject("data"); JSONObject userObj = object.getJSONObject("data");
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -42,7 +42,7 @@ public class AuthQqRequest extends AuthDefaultRequest { ...@@ -42,7 +42,7 @@ public class AuthQqRequest extends AuthDefaultRequest {
@Override @Override
public AuthResponse refresh(AuthToken authToken) { public AuthResponse refresh(AuthToken authToken) {
String response = HttpUtil.get(refreshTokenUrl(authToken.getRefreshToken())); String response = new HttpUtils(config.getHttpConfig()).get(refreshTokenUrl(authToken.getRefreshToken()));
return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(getAuthToken(response)).build(); return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(getAuthToken(response)).build();
} }
...@@ -80,7 +80,7 @@ public class AuthQqRequest extends AuthDefaultRequest { ...@@ -80,7 +80,7 @@ public class AuthQqRequest extends AuthDefaultRequest {
* @return openId * @return openId
*/ */
private String getOpenId(AuthToken authToken) { private String getOpenId(AuthToken authToken) {
String response = HttpUtil.get(UrlBuilder.fromBaseUrl("https://graph.qq.com/oauth2.0/me") String response = new HttpUtils(config.getHttpConfig()).get(UrlBuilder.fromBaseUrl("https://graph.qq.com/oauth2.0/me")
.queryParam("access_token", authToken.getAccessToken()) .queryParam("access_token", authToken.getAccessToken())
.queryParam("unionid", config.isUnionId() ? 1 : 0) .queryParam("unionid", config.isUnionId() ? 1 : 0)
.build()); .build());
......
...@@ -2,7 +2,7 @@ package me.zhyd.oauth.request; ...@@ -2,7 +2,7 @@ package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.enums.AuthUserGender; import me.zhyd.oauth.enums.AuthUserGender;
...@@ -64,7 +64,7 @@ public class AuthRenrenRequest extends AuthDefaultRequest { ...@@ -64,7 +64,7 @@ public class AuthRenrenRequest extends AuthDefaultRequest {
} }
private AuthToken getToken(String url) { private AuthToken getToken(String url) {
String response = HttpUtil.post(url); String response = new HttpUtils(config.getHttpConfig()).post(url);
JSONObject jsonObject = JSONObject.parseObject(response); JSONObject jsonObject = JSONObject.parseObject(response);
if (jsonObject.containsKey("error")) { if (jsonObject.containsKey("error")) {
throw new AuthException("Failed to get token from Renren: " + jsonObject); throw new AuthException("Failed to get token from Renren: " + jsonObject);
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.constants.Constants; import com.xkcoding.http.constants.Constants;
import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.support.HttpHeader;
import com.xkcoding.http.util.MapUtil; import com.xkcoding.http.util.MapUtil;
...@@ -40,7 +40,7 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest { ...@@ -40,7 +40,7 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest {
Map<String, String> form = MapUtil.parseStringToMap(accessTokenUrl, false); Map<String, String> form = MapUtil.parseStringToMap(accessTokenUrl, false);
HttpHeader httpHeader = new HttpHeader(); HttpHeader httpHeader = new HttpHeader();
httpHeader.add(Constants.CONTENT_TYPE, "application/x-www-form-urlencoded"); httpHeader.add(Constants.CONTENT_TYPE, "application/x-www-form-urlencoded");
String response = HttpUtil.post(accessTokenUrl, form, httpHeader, false); String response = new HttpUtils(config.getHttpConfig()).post(accessTokenUrl, form, httpHeader, false);
JSONObject accessTokenObject = JSONObject.parseObject(response); JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject); this.checkResponse(accessTokenObject);
...@@ -58,7 +58,7 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest { ...@@ -58,7 +58,7 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest {
.queryParam("site", "stackoverflow") .queryParam("site", "stackoverflow")
.queryParam("key", this.config.getStackOverflowKey()) .queryParam("key", this.config.getStackOverflowKey())
.build(); .build();
String response = HttpUtil.get(userInfoUrl); String response = new HttpUtils(config.getHttpConfig()).get(userInfoUrl);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
JSONObject userObj = object.getJSONArray("items").getJSONObject(0); JSONObject userObj = object.getJSONArray("items").getJSONObject(0);
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.support.HttpHeader;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
...@@ -45,7 +45,7 @@ public class AuthTeambitionRequest extends AuthDefaultRequest { ...@@ -45,7 +45,7 @@ public class AuthTeambitionRequest extends AuthDefaultRequest {
form.put("code", authCallback.getCode()); form.put("code", authCallback.getCode());
form.put("grant_type", "code"); form.put("grant_type", "code");
String response = HttpUtil.post(source.accessToken(), form, false); String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), form, false);
JSONObject accessTokenObject = JSONObject.parseObject(response); JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject); this.checkResponse(accessTokenObject);
...@@ -63,7 +63,7 @@ public class AuthTeambitionRequest extends AuthDefaultRequest { ...@@ -63,7 +63,7 @@ public class AuthTeambitionRequest extends AuthDefaultRequest {
HttpHeader httpHeader = new HttpHeader(); HttpHeader httpHeader = new HttpHeader();
httpHeader.add("Authorization", "OAuth2 " + accessToken); httpHeader.add("Authorization", "OAuth2 " + accessToken);
String response = HttpUtil.get(source.userInfo(), null, httpHeader, false); String response = new HttpUtils(config.getHttpConfig()).get(source.userInfo(), null, httpHeader, false);
JSONObject object = JSONObject.parseObject(response); JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object); this.checkResponse(object);
...@@ -92,7 +92,7 @@ public class AuthTeambitionRequest extends AuthDefaultRequest { ...@@ -92,7 +92,7 @@ public class AuthTeambitionRequest extends AuthDefaultRequest {
Map<String, String> form = new HashMap<>(2); Map<String, String> form = new HashMap<>(2);
form.put("_userId", uid); form.put("_userId", uid);
form.put("refresh_token", refreshToken); form.put("refresh_token", refreshToken);
String response = HttpUtil.post(source.refresh(), form, false); String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false);
JSONObject refreshTokenObject = JSONObject.parseObject(response); JSONObject refreshTokenObject = JSONObject.parseObject(response);
this.checkResponse(refreshTokenObject); this.checkResponse(refreshTokenObject);
......
package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 腾讯云登录
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @since 1.0.0
*/
public class AuthTencentCloudRequest extends AuthDefaultRequest {
public AuthTencentCloudRequest(AuthConfig config) {
super(config, AuthDefaultSource.TENCENT_CLOUD);
}
public AuthTencentCloudRequest(AuthConfig config, AuthStateCache authStateCache) {
super(config, AuthDefaultSource.TENCENT_CLOUD, authStateCache);
}
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
String response = doGetAuthorizationCode(authCallback.getCode());
JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject);
return AuthToken.builder()
.accessToken(accessTokenObject.getString("access_token"))
.expireIn(accessTokenObject.getIntValue("expires_in"))
.refreshToken(accessTokenObject.getString("refresh_token"))
.build();
}
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
String response = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object);
object = object.getJSONObject("data");
return AuthUser.builder()
.uuid(object.getString("id"))
.username(object.getString("name"))
.avatar("https://dev.tencent.com/" + object.getString("avatar"))
.blog("https://dev.tencent.com/" + object.getString("path"))
.nickname(object.getString("name"))
.company(object.getString("company"))
.location(object.getString("location"))
.gender(AuthUserGender.getRealGender(object.getString("sex")))
.email(object.getString("email"))
.remark(object.getString("slogan"))
.token(authToken)
.source(source.toString())
.build();
}
/**
* 检查响应内容是否正确
*
* @param object 请求响应内容
*/
private void checkResponse(JSONObject object) {
if (object.getIntValue("code") != 0) {
throw new AuthException(object.getString("msg"));
}
}
/**
* 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state}
*
* @param state state 验证授权流程的参数,可以防止csrf
* @return 返回授权地址
* @since 1.9.3
*/
@Override
public String authorize(String state) {
return UrlBuilder.fromBaseUrl(source.authorize())
.queryParam("response_type", "code")
.queryParam("client_id", config.getClientId())
.queryParam("redirect_uri", config.getRedirectUri())
.queryParam("scope", "user")
.queryParam("state", getRealState(state))
.build();
}
}
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.constants.Constants; import com.xkcoding.http.constants.Constants;
import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.support.HttpHeader;
import com.xkcoding.http.util.MapUtil; import com.xkcoding.http.util.MapUtil;
...@@ -72,7 +72,7 @@ public class AuthTwitterRequest extends AuthDefaultRequest { ...@@ -72,7 +72,7 @@ public class AuthTwitterRequest extends AuthDefaultRequest {
httpHeader.add("User-Agent", "themattharris' HTTP Client"); httpHeader.add("User-Agent", "themattharris' HTTP Client");
httpHeader.add("Host", "api.twitter.com"); httpHeader.add("Host", "api.twitter.com");
httpHeader.add("Accept", "*/*"); httpHeader.add("Accept", "*/*");
String requestToken = HttpUtil.post(baseUrl, null, httpHeader); String requestToken = new HttpUtils(config.getHttpConfig()).post(baseUrl, null, httpHeader);
Map<String, String> res = MapUtil.parseStringToMap(requestToken, false); Map<String, String> res = MapUtil.parseStringToMap(requestToken, false);
...@@ -104,7 +104,7 @@ public class AuthTwitterRequest extends AuthDefaultRequest { ...@@ -104,7 +104,7 @@ public class AuthTwitterRequest extends AuthDefaultRequest {
Map<String, String> form = new HashMap<>(1); Map<String, String> form = new HashMap<>(1);
form.put("oauth_verifier", authCallback.getOauth_verifier()); form.put("oauth_verifier", authCallback.getOauth_verifier());
String response = HttpUtil.post(source.accessToken(), form, httpHeader, false); String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), form, httpHeader, false);
Map<String, String> requestToken = MapUtil.parseStringToMap(response, false); Map<String, String> requestToken = MapUtil.parseStringToMap(response, false);
...@@ -134,7 +134,7 @@ public class AuthTwitterRequest extends AuthDefaultRequest { ...@@ -134,7 +134,7 @@ public class AuthTwitterRequest extends AuthDefaultRequest {
HttpHeader httpHeader = new HttpHeader(); HttpHeader httpHeader = new HttpHeader();
httpHeader.add("Authorization", header); httpHeader.add("Authorization", header);
String response = HttpUtil.get(userInfoUrl(authToken), null, httpHeader, false); String response = new HttpUtils(config.getHttpConfig()).get(userInfoUrl(authToken), null, httpHeader, false);
JSONObject userInfo = JSONObject.parseObject(response); JSONObject userInfo = JSONObject.parseObject(response);
return AuthUser.builder() return AuthUser.builder()
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -148,7 +148,7 @@ public class AuthWeChatEnterpriseRequest extends AuthDefaultRequest { ...@@ -148,7 +148,7 @@ public class AuthWeChatEnterpriseRequest extends AuthDefaultRequest {
.queryParam("access_token", accessToken) .queryParam("access_token", accessToken)
.queryParam("userid", userId) .queryParam("userid", userId)
.build(); .build();
return HttpUtil.get(userDetailUrl); return new HttpUtils(config.getHttpConfig()).get(userDetailUrl);
} }
} }
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -94,7 +94,7 @@ public class AuthWeChatMpRequest extends AuthDefaultRequest { ...@@ -94,7 +94,7 @@ public class AuthWeChatMpRequest extends AuthDefaultRequest {
* @return token对象 * @return token对象
*/ */
private AuthToken getToken(String accessTokenUrl) { private AuthToken getToken(String accessTokenUrl) {
String response = HttpUtil.get(accessTokenUrl); String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl);
JSONObject accessTokenObject = JSONObject.parseObject(response); JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject); this.checkResponse(accessTokenObject);
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
...@@ -93,7 +93,7 @@ public class AuthWeChatOpenRequest extends AuthDefaultRequest { ...@@ -93,7 +93,7 @@ public class AuthWeChatOpenRequest extends AuthDefaultRequest {
* @return token对象 * @return token对象
*/ */
private AuthToken getToken(String accessTokenUrl) { private AuthToken getToken(String accessTokenUrl) {
String response = HttpUtil.get(accessTokenUrl); String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl);
JSONObject accessTokenObject = JSONObject.parseObject(response); JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject); this.checkResponse(accessTokenObject);
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil; import me.zhyd.oauth.utils.HttpUtils;
import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.support.HttpHeader;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
...@@ -58,7 +58,7 @@ public class AuthWeiboRequest extends AuthDefaultRequest { ...@@ -58,7 +58,7 @@ public class AuthWeiboRequest extends AuthDefaultRequest {
HttpHeader httpHeader = new HttpHeader(); HttpHeader httpHeader = new HttpHeader();
httpHeader.add("Authorization", "OAuth2 " + oauthParam); httpHeader.add("Authorization", "OAuth2 " + oauthParam);
httpHeader.add("API-RemoteIP", IpUtils.getLocalIp()); httpHeader.add("API-RemoteIP", IpUtils.getLocalIp());
String userInfo = HttpUtil.get(userInfoUrl(authToken), null, httpHeader, false); String userInfo = new HttpUtils(config.getHttpConfig()).get(userInfoUrl(authToken), null, httpHeader, false);
JSONObject object = JSONObject.parseObject(userInfo); JSONObject object = JSONObject.parseObject(userInfo);
if (object.containsKey("error")) { if (object.containsKey("error")) {
throw new AuthException(object.getString("error")); throw new AuthException(object.getString("error"));
......
...@@ -35,6 +35,9 @@ public class AuthChecker { ...@@ -35,6 +35,9 @@ public class AuthChecker {
if (isSupported && AuthDefaultSource.WECHAT_ENTERPRISE == source) { if (isSupported && AuthDefaultSource.WECHAT_ENTERPRISE == source) {
isSupported = StringUtils.isNotEmpty(config.getAgentId()); isSupported = StringUtils.isNotEmpty(config.getAgentId());
} }
if (isSupported && AuthDefaultSource.CODING == source) {
isSupported = StringUtils.isNotEmpty(config.getCodingGroupName());
}
return isSupported; return isSupported;
} }
......
package me.zhyd.oauth.utils;
import com.xkcoding.http.HttpUtil;
import com.xkcoding.http.config.HttpConfig;
import com.xkcoding.http.support.HttpHeader;
import java.util.Map;
/**
* HttpUtil 工具,统一处理 http 请求,方便对 simple-http 做定制
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class HttpUtils {
public HttpUtils(HttpConfig config) {
HttpUtil.setConfig(config);
}
public HttpUtils() {
}
/**
* GET 请求
*
* @param url URL
* @return 结果
*/
public String get(String url) {
return HttpUtil.get(url);
}
/**
* GET 请求
*
* @param url URL
* @param params 参数
* @param header 请求头
* @param encode 是否需要 url encode
* @return 结果
*/
public String get(String url, Map<String, String> params, HttpHeader header, boolean encode) {
return HttpUtil.get(url, params, header, encode);
}
/**
* POST 请求
*
* @param url URL
* @return 结果
*/
public String post(String url) {
return HttpUtil.post(url);
}
/**
* POST 请求
*
* @param url URL
* @param data JSON 参数
* @return 结果
*/
public String post(String url, String data) {
return HttpUtil.post(url, data);
}
/**
* POST 请求
*
* @param url URL
* @param data JSON 参数
* @param header 请求头
* @return 结果
*/
public String post(String url, String data, HttpHeader header) {
return HttpUtil.post(url, data, header);
}
/**
* POST 请求
*
* @param url URL
* @param params form 参数
* @param encode 是否需要 url encode
* @return 结果
*/
public String post(String url, Map<String, String> params, boolean encode) {
return HttpUtil.post(url, params, encode);
}
/**
* POST 请求
*
* @param url URL
* @param params form 参数
* @param header 请求头
* @param encode 是否需要 url encode
* @return 结果
*/
public String post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
return HttpUtil.post(url, params, header, encode);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册