Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3cf8beab83 | |||
| 3824bc0887 | |||
| b8c3caea73 | |||
| 4fb81436e2 | |||
| a52d34ed75 | |||
| 83856c8350 | |||
| 79574a66e5 | |||
| 6186236a13 | |||
| 8abe74db9d | |||
| 188c52ca6b | |||
| e04d2abe3a | |||
| 144decd932 | |||
| 87c2a1b2cd | |||
| 2e0262ed7e | |||
| 8520d0b24c | |||
| 40b48a9f2a | |||
| d176b6089c | |||
| 9b1bf7bb47 | |||
| 0ed1814deb | |||
| 755b72de99 | |||
| 0e24f06022 |
@@ -21,3 +21,8 @@
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
# exclude idea files
|
||||
.idea
|
||||
*.iml
|
||||
*.sh
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<img src="https://img.shields.io/badge/Maven Central-1.0.0-blue.svg" ></img>
|
||||
</a>
|
||||
<a target="_blank" href="https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE">
|
||||
<img src="https://img.shields.io/badge/License-GPL%20v3-yellow.svg" ></img>
|
||||
<img src="https://img.shields.io/apm/l/vim-mode.svg?color=yellow" ></img>
|
||||
</a>
|
||||
<a target="_blank" href="https://www.oracle.com/technetwork/java/javase/downloads/index.html">
|
||||
<img src="https://img.shields.io/badge/JDK-1.8+-green.svg" ></img>
|
||||
@@ -20,18 +20,19 @@
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<td align="center" width="200"><a href="https://gitee.com/"><img src="https://gitee.com/logo_icon.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://github.com"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/github.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://weibo.com"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/weibo.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.dingtalk.com"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/dingding.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://developer.baidu.com/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/baidu.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.csdn.net/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/csdn.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://coding.net"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/coding.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://dev.tencent.com/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/tencent_cloud.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.oschina.net"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/oschinas.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.alipay.com"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/alipay.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://connect.qq.com/devuser.html#/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/qq.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://gitee.com/"><img src="https://gitee.com/logo_icon.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://github.com"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/github.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://weibo.com"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/weibo.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.dingtalk.com"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/dingding.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://developer.baidu.com/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/baidu.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.csdn.net/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/csdn.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://coding.net"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/coding.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://dev.tencent.com/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/tencent_cloud.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.oschina.net"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/oschinas.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.alipay.com"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/alipay.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://connect.qq.com/devuser.html#/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/qq.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png" width="20"></a></td>
|
||||
<td align="center" width="200"><a href="https://open.taobao.com/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/taobao.png" width="20"></a></td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -48,6 +49,7 @@
|
||||
<td align="center" width="200"><a href="#授权支付宝">支付宝</a></td>
|
||||
<td align="center" width="200"><a href="#授权qq">QQ</a></td>
|
||||
<td align="center" width="200"><a href="#授权微信">微信</a></td>
|
||||
<td align="center" width="200"><a href="#授权淘宝">淘宝</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -67,7 +69,7 @@ JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具
|
||||
<dependency>
|
||||
<groupId>me.zhyd.oauth</groupId>
|
||||
<artifactId>JustAuth</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
```
|
||||
- 调用api
|
||||
@@ -94,16 +96,17 @@ authRequest.login("code");
|
||||
|:------:|:-------:|:-------:|
|
||||
| <img src="https://gitee.com/logo_icon.png" width="20"> | [AuthGiteeRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java) | <a href="https://gitee.com/api/v5/oauth_doc#list_1" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/github.png" width="20"> | [AuthGithubRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java) | <a href="https://github.com/settings/developers" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/weibo.png" width="20"> | [AuthWeiboRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java) | <a href="https://open.weibo.com/apps?_blank" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/weibo.png" width="20"> | [AuthWeiboRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java) | <a href="https://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/dingding.png" width="20"> | [AuthDingTalkRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java) | <a href="https://open-doc.dingtalk.com/microapp/serverapi2/kymkv6" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/baidu.png" width="20"> | [AuthBaiduRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java) | <a href="https://developer.baidu.com/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/coding.png" width="25"> | [AuthCodingRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java) | <a href="https://open.coding.net/references/oauth/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/tencent_cloud.png" width="25"> | [AuthTencentCloudRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java) | <a href="https://dev.tencent.com/help/doc/faq/b4e5b7aee786/oauth" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/oschinas.png" width="20"> | [AuthOschinaRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java) | <a href="https://www.oschina.net/openapi/docs/openapi_user" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/alipay.png" width="20"> | [AuthAlipayRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java) | <a href="https://alipay.open.taobao.com/docs/doc.htm?spm=a219a.7629140.0.0.336d4b70GUKXOl&treeId=193&articleId=105809&docType=1" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/qq.png" width="20"> | [AuthQqRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthQqRequest.java) | <a href="http://wiki.connect.qq.com/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/csdn.png" width="20"> | [AuthCsdnRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java) | 待续 |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/qq.png" width="20"> | AuthQqRequest | <a href="https://connect.qq.com/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png" width="20"> | AuthWechatRequest | 待续 |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png" width="20"> | [AuthWeChatRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java) | <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/taobao.png" width="20"> | [AuthTaobaoRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTaobaoRequest.java) | <a href="https://open.taobao.com/doc.htm?spm=a219a.7386797.0.0.4e00669acnkQy6&source=search&docId=105590&docType=1" target="_blank">参考文档</a> |
|
||||
|
||||
## 后续开发计划
|
||||
|
||||
@@ -156,11 +159,11 @@ authRequest.login("code");
|
||||
|
||||

|
||||
|
||||
#### 授权csdn
|
||||
#### 授权qq
|
||||
|
||||
待续
|
||||
|
||||
#### 授权qq
|
||||
#### 授权csdn
|
||||
|
||||
待续
|
||||
|
||||
@@ -168,6 +171,21 @@ authRequest.login("code");
|
||||
|
||||
待续
|
||||
|
||||
#### 授权淘宝
|
||||
|
||||
待续
|
||||
|
||||
# 交流
|
||||
|
||||
| 微信(备注:加群) | 公众号 |
|
||||
| :------------: | :------------: |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/wx/wx.png" width="170"/> | <img src="https://gitee.com/yadong.zhang/static/raw/master/wx/wechat_account.jpg" width="200" /> |
|
||||
|
||||
**QQ群**
|
||||
|
||||
[](https://shang.qq.com/wpa/qunwpa?idkey=3571c554a143eff1e15807de033a240196c6b493b25b903d1d37571cfb6040aa)
|
||||
|
||||
|
||||
## 请喝咖啡
|
||||
|
||||
| 支付宝 | 微信 |
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.zhyd.oauth</groupId>
|
||||
<artifactId>JustAuth</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.2.0</version>
|
||||
|
||||
<name>JustAuth</name>
|
||||
<url>https://gitee.com/yadong.zhang/JustAuth</url>
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<name>MIT</name>
|
||||
<url>https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE</url>
|
||||
</license>
|
||||
</licenses>
|
||||
@@ -29,6 +29,12 @@
|
||||
<developer>
|
||||
<name>yadong.zhang</name>
|
||||
<email>yadong.zhang0415@gmail.com</email>
|
||||
<url>https://www.zhyd.me</url>
|
||||
</developer>
|
||||
<developer>
|
||||
<name>Yangkai.Shen</name>
|
||||
<email>shenyangkai1994@gmail.com</email>
|
||||
<url>https://xkcoding.com</url>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
@@ -45,6 +51,7 @@
|
||||
<junit-version>4.11</junit-version>
|
||||
<fastjson-version>1.2.44</fastjson-version>
|
||||
<google-api-version>1.28.0</google-api-version>
|
||||
<guava-version>27.1-jre</guava-version>
|
||||
<alipay-sdk-version>3.7.4.ALL</alipay-sdk-version>
|
||||
</properties>
|
||||
|
||||
@@ -172,14 +179,14 @@
|
||||
</build>
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
<id>sonatype-oss</id>
|
||||
<id>sonatype-nexus-snapshots</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
|
||||
</snapshotRepository>
|
||||
<repository>
|
||||
<id>sonatype-oss</id>
|
||||
<id>sonatype-nexus-staging</id>
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
</project>
|
||||
@@ -6,10 +6,10 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* JustAuth配置类
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 14:03
|
||||
* @since 1.8
|
||||
*/
|
||||
@Getter
|
||||
@@ -17,8 +17,17 @@ import lombok.NoArgsConstructor;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AuthConfig {
|
||||
|
||||
/**
|
||||
* 客户端id:对应个平台的appKey
|
||||
*/
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* 客户端Secret:对应个平台的appSecret
|
||||
*/
|
||||
private String clientSecret;
|
||||
|
||||
/**
|
||||
* 登录成功后的回调地址
|
||||
*/
|
||||
|
||||
@@ -8,8 +8,6 @@ import me.zhyd.oauth.request.ResponseStatus;
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/24 21:20
|
||||
* @since 1.0
|
||||
*/
|
||||
public enum ApiUrl {
|
||||
@@ -298,20 +296,132 @@ public enum ApiUrl {
|
||||
throw new AuthException(ResponseStatus.UNSUPPORTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String refresh() {
|
||||
throw new AuthException(ResponseStatus.UNSUPPORTED);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* QQ
|
||||
*/
|
||||
QQ {
|
||||
@Override
|
||||
public String authorize() {
|
||||
return "https://graph.qq.com/oauth2.0/authorize";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String accessToken() {
|
||||
return "https://graph.qq.com/oauth2.0/token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String userInfo() {
|
||||
return "https://graph.qq.com/user/get_user_info";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String revoke() {
|
||||
throw new AuthException(ResponseStatus.UNSUPPORTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String refresh() {
|
||||
throw new AuthException(ResponseStatus.UNSUPPORTED);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 微信
|
||||
*/
|
||||
WECHAT {
|
||||
@Override
|
||||
public String authorize() {
|
||||
return "https://open.weixin.qq.com/connect/qrconnect";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String accessToken() {
|
||||
return "https://api.weixin.qq.com/sns/oauth2/access_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String userInfo() {
|
||||
return "https://api.weixin.qq.com/sns/userinfo";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String revoke() {
|
||||
throw new AuthException(ResponseStatus.UNSUPPORTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String refresh() {
|
||||
return "https://api.weixin.qq.com/sns/oauth2/refresh_token";
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 淘宝
|
||||
*/
|
||||
TAOBAO {
|
||||
@Override
|
||||
public String authorize() {
|
||||
return "https://oauth.taobao.com/authorize";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String accessToken() {
|
||||
return "https://oauth.taobao.com/token";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String userInfo() {
|
||||
throw new AuthException(ResponseStatus.UNSUPPORTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String revoke() {
|
||||
throw new AuthException(ResponseStatus.UNSUPPORTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String refresh() {
|
||||
throw new AuthException(ResponseStatus.UNSUPPORTED);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 授权的api
|
||||
*
|
||||
* @return url
|
||||
*/
|
||||
public abstract String authorize();
|
||||
|
||||
/**
|
||||
* 获取accessToken的api
|
||||
*
|
||||
* @return url
|
||||
*/
|
||||
public abstract String accessToken();
|
||||
|
||||
/**
|
||||
* 获取用户信息的api
|
||||
*
|
||||
* @return url
|
||||
*/
|
||||
public abstract String userInfo();
|
||||
|
||||
/**
|
||||
* 取消授权的api
|
||||
*
|
||||
* @return url
|
||||
*/
|
||||
public abstract String revoke();
|
||||
|
||||
/**
|
||||
* 刷新授权的api
|
||||
*
|
||||
* @return url
|
||||
*/
|
||||
public abstract String refresh();
|
||||
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@ import me.zhyd.oauth.request.ResponseStatus;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 16:05
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthException extends RuntimeException {
|
||||
|
||||
@@ -5,8 +5,6 @@ import me.zhyd.oauth.utils.StringUtils;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/19 19:13
|
||||
* @since 1.8
|
||||
*/
|
||||
public enum AuthBaiduErrorCode {
|
||||
|
||||
@@ -3,8 +3,6 @@ package me.zhyd.oauth.model;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/19 9:42
|
||||
* @since 1.8
|
||||
*/
|
||||
public enum AuthDingTalkErrorCode {
|
||||
|
||||
@@ -7,8 +7,6 @@ import me.zhyd.oauth.request.ResponseStatus;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 15:43
|
||||
* @since 1.8
|
||||
*/
|
||||
@Builder
|
||||
|
||||
@@ -3,8 +3,6 @@ package me.zhyd.oauth.model;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 13:52
|
||||
* @since 1.8
|
||||
*/
|
||||
public enum AuthSource {
|
||||
@@ -18,7 +16,7 @@ public enum AuthSource {
|
||||
OSCHINA,
|
||||
TENCEN_CLOUD,
|
||||
ALIPAY,
|
||||
|
||||
TAOBAO,
|
||||
QQ,
|
||||
WECHAT,
|
||||
GOOGLE,
|
||||
|
||||
@@ -6,15 +6,15 @@ import lombok.Data;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 15:43
|
||||
* @since 1.8
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class AuthToken {
|
||||
private String accessToken;
|
||||
private String expireIn;
|
||||
private int expireIn;
|
||||
private String refreshToken;
|
||||
private String uid;
|
||||
private String openId;
|
||||
private String accessCode;
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ import lombok.Data;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 13:47
|
||||
* @since 1.8
|
||||
*/
|
||||
@Builder
|
||||
@@ -23,5 +21,5 @@ public class AuthUser {
|
||||
private String remark;
|
||||
private AuthUserGender gender;
|
||||
private AuthSource source;
|
||||
private String accessToken;
|
||||
private AuthToken token;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package me.zhyd.oauth.model;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 17:24
|
||||
* @since 1.8
|
||||
*/
|
||||
public enum AuthUserGender {
|
||||
@@ -21,10 +21,12 @@ public enum AuthUserGender {
|
||||
if (code == null) {
|
||||
return UNKNOW;
|
||||
}
|
||||
if ("m".equals(code) || "男".equals(code) || "1".equals(code) || "male".equalsIgnoreCase(code) || "F".equalsIgnoreCase(code)) {
|
||||
String[] males = {"m", "男", "1", "male", "F"};
|
||||
if (Arrays.asList(males).contains(code)) {
|
||||
return MALE;
|
||||
}
|
||||
if ("f".equals(code) || "女".equals(code) || "0".equals(code) || "female".equalsIgnoreCase(code)) {
|
||||
String[] females = {"f", "女", "0", "female"};
|
||||
if (Arrays.asList(females).contains(code)) {
|
||||
return FEMALE;
|
||||
}
|
||||
return UNKNOW;
|
||||
|
||||
@@ -11,15 +11,16 @@ import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.consts.ApiUrl;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.model.AuthUserGender;
|
||||
import me.zhyd.oauth.utils.StringUtils;
|
||||
|
||||
/**
|
||||
* 支付宝登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/3/25 22:48
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthAlipayRequest extends BaseAuthRequest {
|
||||
@@ -32,7 +33,7 @@ public class AuthAlipayRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
|
||||
request.setGrantType("authorization_code");
|
||||
request.setCode(code);
|
||||
@@ -45,11 +46,14 @@ public class AuthAlipayRequest extends BaseAuthRequest {
|
||||
if (!response.isSuccess()) {
|
||||
throw new AuthException(response.getSubMsg());
|
||||
}
|
||||
return response.getAccessToken();
|
||||
return AuthToken.builder()
|
||||
.accessToken(response.getAccessToken())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
|
||||
AlipayUserInfoShareResponse response = null;
|
||||
try {
|
||||
@@ -68,7 +72,7 @@ public class AuthAlipayRequest extends BaseAuthRequest {
|
||||
.avatar(response.getAvatar())
|
||||
.location(String.format("%s %s", StringUtils.isEmpty(province) ? "" : province, StringUtils.isEmpty(city) ? "" : city))
|
||||
.gender(AuthUserGender.getRealGender(response.getGender()))
|
||||
.accessToken(accessToken)
|
||||
.token(authToken)
|
||||
.source(AuthSource.ALIPAY)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -3,16 +3,17 @@ package me.zhyd.oauth.request;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.*;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* 百度账号登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/19 18:43
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthBaiduRequest extends BaseAuthRequest {
|
||||
@@ -22,7 +23,7 @@ public class AuthBaiduRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getBaiduAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
|
||||
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
||||
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
||||
@@ -30,11 +31,14 @@ public class AuthBaiduRequest extends BaseAuthRequest {
|
||||
if (!AuthBaiduErrorCode.OK.equals(errorCode)) {
|
||||
throw new AuthException(errorCode.getDesc());
|
||||
}
|
||||
return accessTokenObject.getString("access_token");
|
||||
return AuthToken.builder()
|
||||
.accessToken(accessTokenObject.getString("access_token"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getBaiduUserInfoUrl(accessToken)).execute();
|
||||
String userInfo = response.body();
|
||||
JSONObject object = JSONObject.parseObject(userInfo);
|
||||
@@ -46,16 +50,23 @@ public class AuthBaiduRequest extends BaseAuthRequest {
|
||||
.username(object.getString("username"))
|
||||
.nickname(object.getString("username"))
|
||||
.gender(AuthUserGender.getRealGender(object.getString("sex")))
|
||||
.accessToken(accessToken)
|
||||
.token(authToken)
|
||||
.source(AuthSource.BAIDU)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthResponse revoke(String accessToken) {
|
||||
public AuthResponse revoke(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getBaiduRevokeUrl(accessToken)).execute();
|
||||
String userInfo = response.body();
|
||||
JSONObject object = JSONObject.parseObject(userInfo);
|
||||
if(object.containsKey("error_code")) {
|
||||
return AuthResponse.builder()
|
||||
.code(ResponseStatus.FAILURE.getCode())
|
||||
.msg(object.getString("error_msg"))
|
||||
.build();
|
||||
}
|
||||
ResponseStatus status = object.getIntValue("result") == 1 ? ResponseStatus.SUCCESS : ResponseStatus.FAILURE;
|
||||
return AuthResponse.builder().code(status.getCode()).msg(status.getMsg()).build();
|
||||
}
|
||||
|
||||
@@ -6,15 +6,16 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.model.AuthUserGender;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* Cooding登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/23 15:48
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthCodingRequest extends BaseAuthRequest {
|
||||
@@ -24,18 +25,21 @@ public class AuthCodingRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getCodingAccessTokenUrl(config.getClientId(), config.getClientSecret(), code);
|
||||
HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
|
||||
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
||||
if (accessTokenObject.getIntValue("code") != 0) {
|
||||
throw new AuthException("Unable to get token from coding using code [" + code + "]");
|
||||
}
|
||||
return accessTokenObject.getString("access_token");
|
||||
return AuthToken.builder()
|
||||
.accessToken(accessTokenObject.getString("access_token"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getCodingUserInfoUrl(accessToken)).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.getIntValue("code") != 0) {
|
||||
@@ -52,7 +56,7 @@ public class AuthCodingRequest extends BaseAuthRequest {
|
||||
.gender(AuthUserGender.getRealGender(object.getString("sex")))
|
||||
.email(object.getString("email"))
|
||||
.remark(object.getString("slogan"))
|
||||
.accessToken(accessToken)
|
||||
.token(authToken)
|
||||
.source(AuthSource.CODING)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -6,14 +6,15 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* CSDN登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/23 15:00
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthCsdnRequest extends BaseAuthRequest {
|
||||
@@ -23,18 +24,21 @@ public class AuthCsdnRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getCsdnAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
|
||||
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
||||
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
||||
if (accessTokenObject.containsKey("error_code")) {
|
||||
throw new AuthException("Unable to get token from csdn using code [" + code + "]");
|
||||
}
|
||||
return accessTokenObject.getString("access_token");
|
||||
return AuthToken.builder()
|
||||
.accessToken(accessTokenObject.getString("access_token"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getCsdnUserInfoUrl(accessToken)).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.containsKey("error_code")) {
|
||||
@@ -42,7 +46,7 @@ public class AuthCsdnRequest extends BaseAuthRequest {
|
||||
}
|
||||
return AuthUser.builder()
|
||||
.username(object.getString("username"))
|
||||
.accessToken(accessToken)
|
||||
.token(authToken)
|
||||
.source(AuthSource.CSDN)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import cn.hutool.json.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthDingTalkErrorCode;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.utils.GlobalAuthUtil;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
@@ -15,10 +15,10 @@ import me.zhyd.oauth.utils.UrlBuilder;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 钉钉登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/18 18:43
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthDingTalkRequest extends BaseAuthRequest {
|
||||
@@ -28,12 +28,15 @@ public class AuthDingTalkRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
return AuthToken.builder()
|
||||
.accessCode(code)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String code) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String code = authToken.getAccessCode();
|
||||
// 根据timestamp, appSecret计算签名值
|
||||
String stringToSign = System.currentTimeMillis() + "";
|
||||
String urlEncodeSignature = GlobalAuthUtil.generateDingTalkSignature(config.getClientSecret(), stringToSign);
|
||||
@@ -49,14 +52,8 @@ public class AuthDingTalkRequest extends BaseAuthRequest {
|
||||
object = object.getJSONObject("user_info");
|
||||
return AuthUser.builder()
|
||||
.nickname(object.getStr("nick"))
|
||||
.username(object.getStr("nick"))
|
||||
.source(AuthSource.DINGTALK)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthResponse login(String code) {
|
||||
return AuthResponse.builder()
|
||||
.data(this.getUserInfo(code))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,14 +6,15 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* Gitee登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 16:31
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthGiteeRequest extends BaseAuthRequest {
|
||||
@@ -23,18 +24,21 @@ public class AuthGiteeRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getGiteeAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
|
||||
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
||||
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
||||
if (accessTokenObject.containsKey("error")) {
|
||||
throw new AuthException("Unable to get token from gitee using code [" + code + "]");
|
||||
}
|
||||
return accessTokenObject.getString("access_token");
|
||||
return AuthToken.builder()
|
||||
.accessToken(accessTokenObject.getString("access_token"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getGiteeUserInfoUrl(accessToken)).execute();
|
||||
String userInfo = response.body();
|
||||
JSONObject object = JSONObject.parseObject(userInfo);
|
||||
@@ -47,7 +51,7 @@ public class AuthGiteeRequest extends BaseAuthRequest {
|
||||
.location(object.getString("address"))
|
||||
.email(object.getString("email"))
|
||||
.remark(object.getString("bio"))
|
||||
.accessToken(accessToken)
|
||||
.token(authToken)
|
||||
.source(AuthSource.GITEE)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.utils.GlobalAuthUtil;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
@@ -13,10 +14,10 @@ import me.zhyd.oauth.utils.UrlBuilder;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Github登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 16:31
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthGithubRequest extends BaseAuthRequest {
|
||||
@@ -26,18 +27,21 @@ public class AuthGithubRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getGithubAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
|
||||
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
||||
Map<String, String> res = GlobalAuthUtil.parseStringToMap(response.body());
|
||||
if (res.containsKey("error")) {
|
||||
throw new AuthException(res.get("error") + ":" + res.get("error_description"));
|
||||
}
|
||||
return res.get("access_token");
|
||||
return AuthToken.builder()
|
||||
.accessToken(res.get("access_token"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getGithubUserInfoUrl(accessToken)).execute();
|
||||
String userInfo = response.body();
|
||||
JSONObject object = JSONObject.parseObject(userInfo);
|
||||
@@ -50,7 +54,7 @@ public class AuthGithubRequest extends BaseAuthRequest {
|
||||
.location(object.getString("location"))
|
||||
.email(object.getString("email"))
|
||||
.remark(object.getString("bio"))
|
||||
.accessToken(accessToken)
|
||||
.token(authToken)
|
||||
.source(AuthSource.GITHUB)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -6,15 +6,16 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.model.AuthUserGender;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* oschina登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/23 15:48
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthOschinaRequest extends BaseAuthRequest {
|
||||
@@ -24,18 +25,21 @@ public class AuthOschinaRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getOschinaAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
|
||||
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
||||
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
||||
if (accessTokenObject.containsKey("error")) {
|
||||
throw new AuthException("Unable to get token from oschina using code [" + code + "]");
|
||||
}
|
||||
return accessTokenObject.getString("access_token");
|
||||
return AuthToken.builder()
|
||||
.accessToken(accessTokenObject.getString("access_token"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getOschinaUserInfoUrl(accessToken)).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.containsKey("error")) {
|
||||
@@ -49,7 +53,7 @@ public class AuthOschinaRequest extends BaseAuthRequest {
|
||||
.location(object.getString("location"))
|
||||
.gender(AuthUserGender.getRealGender(object.getString("gender")))
|
||||
.email(object.getString("email"))
|
||||
.accessToken(accessToken)
|
||||
.token(authToken)
|
||||
.source(AuthSource.OSCHINA)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package me.zhyd.oauth.request;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.model.AuthUserGender;
|
||||
import me.zhyd.oauth.utils.StringUtils;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* qq登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthQqRequest extends BaseAuthRequest {
|
||||
public AuthQqRequest(AuthConfig config) {
|
||||
super(config, AuthSource.QQ);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getQqAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
|
||||
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
||||
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
||||
if (!accessTokenObject.containsKey("access_token")) {
|
||||
throw new AuthException("Unable to get token from qq using code [" + code + "]");
|
||||
}
|
||||
return AuthToken.builder()
|
||||
.accessToken(accessTokenObject.getString("access_token"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
String openId = this.getOpenId(accessToken);
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getQqUserInfoUrl(accessToken, openId)).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.getIntValue("ret") != 0) {
|
||||
throw new AuthException(object.getString("msg"));
|
||||
}
|
||||
String avatar = object.getString("figureurl_qq_2");
|
||||
if (StringUtils.isEmpty(avatar)) {
|
||||
avatar = object.getString("figureurl_qq_1");
|
||||
}
|
||||
return AuthUser.builder()
|
||||
.username(object.getString("nickname"))
|
||||
.nickname(object.getString("nickname"))
|
||||
.avatar(avatar)
|
||||
.gender(AuthUserGender.getRealGender(object.getString("gender")))
|
||||
.token(authToken)
|
||||
.source(AuthSource.QQ)
|
||||
.build();
|
||||
}
|
||||
|
||||
private String getOpenId(String accessToken) {
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getQqOpenidUrl("https://graph.qq.com/oauth2.0/me", accessToken)).execute();
|
||||
if (response.isOk()) {
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.containsKey("openid")) {
|
||||
return object.getString("openid");
|
||||
}
|
||||
throw new AuthException("Invalid openId");
|
||||
}
|
||||
throw new AuthException("Invalid openId");
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,19 @@ package me.zhyd.oauth.request;
|
||||
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 15:45
|
||||
* @since 1.8
|
||||
*/
|
||||
public interface AuthRequest {
|
||||
|
||||
/**
|
||||
* 返回认证url,可自行跳转页面
|
||||
*
|
||||
* @return 返回授权地址
|
||||
*/
|
||||
default String authorize() {
|
||||
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
|
||||
@@ -32,18 +33,20 @@ public interface AuthRequest {
|
||||
/**
|
||||
* 撤销授权
|
||||
*
|
||||
* @param accessToken 登录成功后返回的accessToken
|
||||
* @param authToken 登录成功后返回的Token信息
|
||||
* @return AuthResponse
|
||||
*/
|
||||
default AuthResponse revoke(String accessToken) {
|
||||
default AuthResponse revoke(AuthToken authToken) {
|
||||
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新access token (续期)
|
||||
*
|
||||
* @param accessToken 登录成功后返回的accessToken
|
||||
* @param authToken 登录成功后返回的Token信息
|
||||
* @return AuthResponse
|
||||
*/
|
||||
default AuthResponse refresh(String accessToken) {
|
||||
default AuthResponse refresh(AuthToken authToken) {
|
||||
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package me.zhyd.oauth.request;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.model.AuthUserGender;
|
||||
import me.zhyd.oauth.utils.GlobalAuthUtil;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* 淘宝登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthTaobaoRequest extends BaseAuthRequest {
|
||||
|
||||
public AuthTaobaoRequest(AuthConfig config) {
|
||||
super(config, AuthSource.TAOBAO);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
return AuthToken.builder()
|
||||
.accessCode(code)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessCode = authToken.getAccessCode();
|
||||
HttpResponse response = HttpRequest.post(UrlBuilder.getTaobaoAccessTokenUrl(this.config.getClientId(), this.config.getClientSecret(), accessCode, this.config.getRedirectUri())).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.containsKey("error")) {
|
||||
throw new AuthException(ResponseStatus.FAILURE + ":" + object.getString("error_description"));
|
||||
}
|
||||
authToken.setAccessToken(object.getString("access_token"));
|
||||
authToken.setRefreshToken(object.getString("refresh_token"));
|
||||
authToken.setExpireIn(object.getIntValue("expires_in"));
|
||||
authToken.setUid(object.getString("taobao_user_id"));
|
||||
authToken.setOpenId(object.getString("taobao_open_uid"));
|
||||
|
||||
String nick = GlobalAuthUtil.urlDecode(object.getString("taobao_user_nick"));
|
||||
return AuthUser.builder()
|
||||
.username(nick)
|
||||
.nickname(nick)
|
||||
.gender(AuthUserGender.UNKNOW)
|
||||
.token(authToken)
|
||||
.source(AuthSource.TAOBAO)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -6,15 +6,16 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.model.AuthUserGender;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* 腾讯云登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/23 15:48
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthTencentCloudRequest extends BaseAuthRequest {
|
||||
@@ -24,18 +25,21 @@ public class AuthTencentCloudRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getTencentCloudAccessTokenUrl(config.getClientId(), config.getClientSecret(), code);
|
||||
HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.getIntValue("code") != 0) {
|
||||
throw new AuthException("Unable to get token from tencent cloud using code [" + code + "]: " + object.get("msg"));
|
||||
}
|
||||
return object.getString("access_token");
|
||||
return AuthToken.builder()
|
||||
.accessToken(object.getString("access_token"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getTencentCloudUserInfoUrl(accessToken)).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.getIntValue("code") != 0) {
|
||||
@@ -52,7 +56,7 @@ public class AuthTencentCloudRequest extends BaseAuthRequest {
|
||||
.gender(AuthUserGender.getRealGender(object.getString("sex")))
|
||||
.email(object.getString("email"))
|
||||
.remark(object.getString("slogan"))
|
||||
.accessToken(accessToken)
|
||||
.token(authToken)
|
||||
.source(AuthSource.TENCEN_CLOUD)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package me.zhyd.oauth.request;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.*;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
/**
|
||||
* 微信登录
|
||||
*
|
||||
* @author yangkai.shen (https://xkcoding.com)
|
||||
* @version 1.0
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthWeChatRequest extends BaseAuthRequest {
|
||||
public AuthWeChatRequest(AuthConfig config) {
|
||||
super(config, AuthSource.WECHAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信的特殊性,此时返回的信息同时包含 openid 和 access_token
|
||||
*
|
||||
* @param code 授权码
|
||||
* @return 所有信息
|
||||
*/
|
||||
@Override
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getWeChatAccessTokenUrl(config.getClientId(), config.getClientSecret(), code);
|
||||
HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
|
||||
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
|
||||
if (!accessTokenObject.containsKey("access_token") || !accessTokenObject.containsKey("openid") || !accessTokenObject
|
||||
.containsKey("refresh_token")) {
|
||||
throw new AuthException("Unable to get access_token or openid or refresh_token from wechat using code [" + code + "]");
|
||||
}
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
return AuthToken.builder()
|
||||
.accessToken(object.getString("access_token"))
|
||||
.refreshToken(object.getString("refresh_token"))
|
||||
.openId(object.getString("openid"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
String openId = authToken.getOpenId();
|
||||
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getWeChatUserInfoUrl(accessToken, openId)).execute();
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.containsKey("errcode")) {
|
||||
throw new AuthException(object.getString("errmsg"));
|
||||
}
|
||||
|
||||
return AuthUser.builder()
|
||||
.username(object.getString("nickname"))
|
||||
.nickname(object.getString("nickname"))
|
||||
.avatar(object.getString("headimgurl"))
|
||||
.location(object.getString("country") + "-" + object.getString("province") + "-" + object.getString("city"))
|
||||
.gender(AuthUserGender.getRealGender(object.getString("sex")))
|
||||
.token(authToken)
|
||||
.source(AuthSource.WECHAT)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthResponse refresh(AuthToken authToken) {
|
||||
String refreshToken = authToken.getRefreshToken();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getWeChatRefreshUrl(config.getClientId(), refreshToken))
|
||||
.execute();
|
||||
|
||||
JSONObject object = JSONObject.parseObject(response.body());
|
||||
if (object.containsKey("errcode")) {
|
||||
throw new AuthException(object.getString("errmsg"));
|
||||
}
|
||||
|
||||
return AuthResponse.builder().data(object).build();
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.model.AuthUserGender;
|
||||
import me.zhyd.oauth.utils.GlobalAuthUtil;
|
||||
@@ -15,10 +16,10 @@ import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
|
||||
/**
|
||||
* 微博登录
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 16:31
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthWeiboRequest extends BaseAuthRequest {
|
||||
@@ -28,27 +29,34 @@ public class AuthWeiboRequest extends BaseAuthRequest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccessToken(String code) {
|
||||
protected AuthToken getAccessToken(String code) {
|
||||
String accessTokenUrl = UrlBuilder.getWeiboAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
|
||||
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
|
||||
String accessTokenStr = response.body();
|
||||
JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);
|
||||
if (accessTokenObject.containsKey("error")) {
|
||||
throw new AuthException("Unable to get token from gitee using code [" + code + "]");
|
||||
throw new AuthException("Unable to get token from weibo using code [" + code + "]:" + accessTokenObject.getString("error_description"));
|
||||
}
|
||||
String accessToken = accessTokenObject.getString("access_token");
|
||||
String uid = accessTokenObject.getString("uid");
|
||||
return String.format("uid=%s&access_token=%s", uid, accessToken);
|
||||
return AuthToken.builder()
|
||||
.accessToken(accessTokenObject.getString("access_token"))
|
||||
.uid(accessTokenObject.getString("uid"))
|
||||
.expireIn(accessTokenObject.getIntValue("remind_in"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AuthUser getUserInfo(String accessToken) {
|
||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
||||
String accessToken = authToken.getAccessToken();
|
||||
String uid = authToken.getUid();
|
||||
HttpResponse response = HttpRequest.get(UrlBuilder.getWeiboUserInfoUrl(accessToken))
|
||||
.header("Authorization", "OAuth2 " + accessToken)
|
||||
.header("Authorization", "OAuth2 " + String.format("uid=%s&access_token=%s", uid, accessToken))
|
||||
.header("API-RemoteIP", IpUtils.getIp())
|
||||
.execute();
|
||||
String userInfo = response.body();
|
||||
JSONObject object = JSONObject.parseObject(userInfo);
|
||||
if(object.containsKey("error")) {
|
||||
throw new AuthException(object.getString("error"));
|
||||
}
|
||||
return AuthUser.builder()
|
||||
.username(object.getString("name"))
|
||||
.avatar(object.getString("profile_image_url"))
|
||||
@@ -57,7 +65,7 @@ public class AuthWeiboRequest extends BaseAuthRequest {
|
||||
.location(object.getString("location"))
|
||||
.remark(object.getString("description"))
|
||||
.gender(AuthUserGender.getRealGender(object.getString("gender")))
|
||||
.accessToken(GlobalAuthUtil.parseStringToMap(accessToken).get("access_token"))
|
||||
.token(authToken)
|
||||
.source(AuthSource.WEIBO)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.model.AuthSource;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.utils.AuthConfigChecker;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
@@ -12,8 +13,6 @@ import me.zhyd.oauth.utils.UrlBuilder;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/18 13:11
|
||||
* @since 1.8
|
||||
*/
|
||||
@Data
|
||||
@@ -29,9 +28,9 @@ public abstract class BaseAuthRequest implements AuthRequest {
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract String getAccessToken(String code);
|
||||
protected abstract AuthToken getAccessToken(String code);
|
||||
|
||||
protected abstract AuthUser getUserInfo(String accessToken);
|
||||
protected abstract AuthUser getUserInfo(AuthToken authToken);
|
||||
|
||||
@Override
|
||||
public AuthResponse login(String code) {
|
||||
@@ -78,8 +77,13 @@ public abstract class BaseAuthRequest implements AuthRequest {
|
||||
authorizeUrl = UrlBuilder.getAlipayAuthorizeUrl(config.getClientId(), config.getRedirectUri());
|
||||
break;
|
||||
case QQ:
|
||||
authorizeUrl = UrlBuilder.getQqAuthorizeUrl(config.getClientId(), config.getRedirectUri());
|
||||
break;
|
||||
case WECHAT:
|
||||
authorizeUrl = UrlBuilder.getWeChatAuthorizeUrl(config.getClientId(), config.getRedirectUri());
|
||||
break;
|
||||
case TAOBAO:
|
||||
authorizeUrl = UrlBuilder.getTaobaoAuthorizeUrl(config.getClientId(), config.getRedirectUri());
|
||||
break;
|
||||
case GOOGLE:
|
||||
break;
|
||||
|
||||
@@ -3,8 +3,6 @@ package me.zhyd.oauth.request;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 16:37
|
||||
* @since 1.8
|
||||
*/
|
||||
public enum ResponseStatus {
|
||||
|
||||
@@ -5,8 +5,6 @@ import me.zhyd.oauth.config.AuthConfig;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 15:46
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthConfigChecker {
|
||||
@@ -14,6 +12,7 @@ public class AuthConfigChecker {
|
||||
/**
|
||||
* 是否支持第三方登录
|
||||
*
|
||||
* @param config config
|
||||
* @return true or false
|
||||
*/
|
||||
public static boolean isSupportedAuth(AuthConfig config) {
|
||||
|
||||
@@ -8,14 +8,14 @@ import java.net.UnknownHostException;
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 16:31
|
||||
* @since 1.0
|
||||
*/
|
||||
public class IpUtils {
|
||||
|
||||
/**
|
||||
* 获取IP
|
||||
*
|
||||
* @return ip
|
||||
*/
|
||||
public static String getIp() {
|
||||
try {
|
||||
|
||||
@@ -3,8 +3,6 @@ package me.zhyd.oauth.utils;
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 15:48
|
||||
* @since 1.8
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
@@ -9,8 +9,6 @@ import java.text.MessageFormat;
|
||||
*
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/1/31 12:26
|
||||
* @since 1.0
|
||||
*/
|
||||
public class UrlBuilder {
|
||||
@@ -53,6 +51,19 @@ public class UrlBuilder {
|
||||
|
||||
private static final String ALIPAY_AUTHORIZE_PATTERN = "{0}?app_id={1}&scope=auth_user&redirect_uri={2}&state=init";
|
||||
|
||||
private static final String QQ_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}";
|
||||
private static final String QQ_USER_INFO_PATTERN = "{0}?access_token={1}&oauth_consumer_key=12345&openid={2}";
|
||||
private static final String QQ_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}";
|
||||
private static final String QQ_OPENID_PATTERN = "{0}?access_token={1}";
|
||||
|
||||
private static final String WECHAT_AUTHORIZE_PATTERN = "{0}?appid={1}&redirect_uri={2}&response_type=code&scope=snsapi_login&state={3}#wechat_redirect";
|
||||
private static final String WECHAT_ACCESS_TOKEN_PATTERN = "{0}?appid={1}&secret={2}&code={3}&grant_type=authorization_code";
|
||||
private static final String WECHAT_REFRESH_TOKEN_PATTERN = "{0}?appid={1}&grant_type=refresh_token&refresh_token={2}";
|
||||
private static final String WECHAT_USER_INFO_PATTERN = "{0}?access_token={1}&openid={2}&lang=zh_CN";
|
||||
|
||||
private static final String TAOBAO_AUTHORIZE_PATTERN = "{0}?response_type=code&client_id={1}&redirect_uri={2}&state=&view=web";
|
||||
private static final String TAOBAO_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code";
|
||||
|
||||
/**
|
||||
* 获取githubtoken的接口地址
|
||||
*
|
||||
@@ -356,6 +367,52 @@ public class UrlBuilder {
|
||||
return MessageFormat.format(OSCHINA_AUTHORIZE_PATTERN, ApiUrl.OSCHINA.authorize(), clientId, redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取qq token的接口地址
|
||||
*
|
||||
* @param clientId qq应用的App Key
|
||||
* @param clientSecret qq应用的App Secret
|
||||
* @param code qq授权前的code,用来换token
|
||||
* @param redirectUri 待跳转的页面
|
||||
* @return full url
|
||||
*/
|
||||
public static String getQqAccessTokenUrl(String clientId, String clientSecret, String code, String redirectUri) {
|
||||
return MessageFormat.format(QQ_ACCESS_TOKEN_PATTERN, ApiUrl.QQ.accessToken(), clientId, clientSecret, code, redirectUri);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取qq用户详情的接口地址
|
||||
*
|
||||
* @param token qq 应用的token
|
||||
* @param openId qq 应用的openId
|
||||
* @return full url
|
||||
*/
|
||||
public static String getQqUserInfoUrl(String token, String openId) {
|
||||
return MessageFormat.format(QQ_USER_INFO_PATTERN, ApiUrl.QQ.userInfo(), token, openId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取qq授权地址
|
||||
*
|
||||
* @param clientId qq 应用的Client ID
|
||||
* @param redirectUrl qq 应用授权成功后的回调地址
|
||||
* @return full url
|
||||
*/
|
||||
public static String getQqAuthorizeUrl(String clientId, String redirectUrl) {
|
||||
return MessageFormat.format(QQ_AUTHORIZE_PATTERN, ApiUrl.QQ.authorize(), clientId, redirectUrl, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取qq授权地址
|
||||
*
|
||||
* @param url 获取qqopenid的api接口地址
|
||||
* @param token qq 应用授权的token
|
||||
* @return full url
|
||||
*/
|
||||
public static String getQqOpenidUrl(String url, String token) {
|
||||
return MessageFormat.format(QQ_OPENID_PATTERN, url, token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取alipay授权地址
|
||||
*
|
||||
@@ -366,4 +423,73 @@ public class UrlBuilder {
|
||||
public static String getAlipayAuthorizeUrl(String clientId, String redirectUrl) {
|
||||
return MessageFormat.format(ALIPAY_AUTHORIZE_PATTERN, ApiUrl.ALIPAY.authorize(), clientId, redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信 授权地址
|
||||
*
|
||||
* @param clientId 微信应用的appid
|
||||
* @param redirectUrl 微信应用授权成功后的回调地址
|
||||
* @return full url
|
||||
*/
|
||||
public static String getWeChatAuthorizeUrl(String clientId, String redirectUrl) {
|
||||
return MessageFormat.format(WECHAT_AUTHORIZE_PATTERN, ApiUrl.WECHAT.authorize(), clientId, redirectUrl, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信 token的接口地址
|
||||
*
|
||||
* @param clientId 微信应用的appid
|
||||
* @param clientSecret 微信应用的secret
|
||||
* @param code 微信授权前的code,用来换token
|
||||
* @return full url
|
||||
*/
|
||||
public static String getWeChatAccessTokenUrl(String clientId, String clientSecret, String code) {
|
||||
return MessageFormat.format(WECHAT_ACCESS_TOKEN_PATTERN, ApiUrl.WECHAT.accessToken(), clientId, clientSecret, code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信 用户详情的接口地址
|
||||
*
|
||||
* @param token 微信应用返回的 access token
|
||||
* @param openId 微信应用返回的openId
|
||||
* @return full url
|
||||
*/
|
||||
public static String getWeChatUserInfoUrl(String token, String openId) {
|
||||
return MessageFormat.format(WECHAT_USER_INFO_PATTERN, ApiUrl.WECHAT.userInfo(), token, openId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信 刷新令牌 地址
|
||||
*
|
||||
* @param clientId 微信应用的appid
|
||||
* @param refreshToken 微信应用返回的刷新token
|
||||
* @return full url
|
||||
*/
|
||||
public static String getWeChatRefreshUrl(String clientId, String refreshToken) {
|
||||
return MessageFormat.format(WECHAT_REFRESH_TOKEN_PATTERN, ApiUrl.WECHAT.refresh(), clientId, refreshToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Taobao token的接口地址: 淘宝的授权登录,在这一步就会返回用户信息
|
||||
*
|
||||
* @param clientId taobao应用的App Key
|
||||
* @param clientSecret taobao应用的App Secret
|
||||
* @param code taobao授权前的code,用来换token
|
||||
* @param redirectUri 待跳转的页面
|
||||
* @return full url
|
||||
*/
|
||||
public static String getTaobaoAccessTokenUrl(String clientId, String clientSecret, String code, String redirectUri) {
|
||||
return MessageFormat.format(TAOBAO_ACCESS_TOKEN_PATTERN, ApiUrl.TAOBAO.accessToken(), clientId, clientSecret, code, redirectUri);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Taobao授权地址
|
||||
*
|
||||
* @param clientId Taobao 应用的Client ID
|
||||
* @param redirectUrl Taobao 应用授权成功后的回调地址
|
||||
* @return full url
|
||||
*/
|
||||
public static String getTaobaoAuthorizeUrl(String clientId, String redirectUrl) {
|
||||
return MessageFormat.format(TAOBAO_AUTHORIZE_PATTERN, ApiUrl.TAOBAO.authorize(), clientId, redirectUrl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
package me.zhyd.oauth;
|
||||
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.request.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
||||
* @version 1.0
|
||||
* @website https://www.zhyd.me
|
||||
* @date 2019/2/18 13:10
|
||||
* @since 1.8
|
||||
*/
|
||||
public class AuthRequestTest {
|
||||
@@ -116,4 +115,30 @@ public class AuthRequestTest {
|
||||
// 授权登录后会返回一个code,用这个code进行登录
|
||||
authRequest.login("code");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void qqTest() {
|
||||
AuthRequest authRequest = new AuthQqRequest(AuthConfig.builder()
|
||||
.clientId("clientId")
|
||||
.clientSecret("clientSecret")
|
||||
.redirectUri("redirectUri")
|
||||
.build());
|
||||
// 返回授权页面,可自行调整
|
||||
String url = authRequest.authorize();
|
||||
// 授权登录后会返回一个code,用这个code进行登录
|
||||
AuthResponse login = authRequest.login("code");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wechatTest() {
|
||||
AuthRequest authRequest = new AuthWeChatRequest(AuthConfig.builder()
|
||||
.clientId("clientId")
|
||||
.clientSecret("clientSecret")
|
||||
.redirectUri("redirectUri")
|
||||
.build());
|
||||
// 返回授权页面,可自行调整
|
||||
String url = authRequest.authorize();
|
||||
// 授权登录后会返回一个code,用这个code进行登录
|
||||
AuthResponse login = authRequest.login("code");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user