1
0
mirror of synced 2026-05-22 21:53:18 +00:00

Compare commits

..

23 Commits

Author SHA1 Message Date
yadong.zhang 14fab8cd77 🐛 修复QQ登录的问题 2019-05-23 08:55:05 +08:00
yadong.zhang 3c0c809dfc 🐛 修复QQ登录的问题 2019-05-23 08:53:55 +08:00
yadong.zhang 6f40460cce Merge pull request #6 from xkcoding/fix-qq
🐛 修复QQ登录,返回AccessToken以及OpenId数据格式解析错误问题
2019-05-22 23:06:52 +08:00
Yangkai.Shen e81e92130b 🐛 修复QQ登录,返回AccessToken以及OpenId数据格式解析错误问题 2019-05-22 19:05:13 +08:00
yadong.zhang 85afd754a9 📝 更新文档 2019-05-22 09:18:09 +08:00
yadong.zhang a8709a30d3 📝 编写文档 2019-05-21 23:40:37 +08:00
yadong.zhang ffa48970e5 更新 README.md 2019-05-21 23:38:35 +08:00
yadong.zhang 5d753de282 更新 README.md 2019-05-21 23:37:26 +08:00
yadong.zhang a739ca374e 增加facebook授权登录 2019-05-21 23:33:17 +08:00
yadong.zhang 880a7d4157 更新 README.md 2019-05-21 19:07:28 +08:00
yadong.zhang 6fe1aa62c6 📝 更新文档 2019-05-21 19:00:02 +08:00
yadong.zhang 7857db7824 增加Google授权 2019-05-21 18:45:29 +08:00
yadong.zhang 7ed248689f Merge pull request #5 from xkcoding/feature-google
 去除重复代码、新增支持Google登录
2019-05-21 18:13:31 +08:00
Yangkai.Shen d24733e016 新增支持Google登录 2019-05-21 15:09:16 +08:00
Yangkai.Shen 2dba2baef7 🎨 去除重复代码 2019-05-21 15:08:45 +08:00
yadong.zhang 2abca4f10f Merge pull request #4 from xkcoding/patch-3
gitignore文件更新,AuthConfig 添加 set 方法支持作为外部配置类
2019-05-20 14:36:43 +08:00
Yangkai.Shen 7db4dcce2a 🎨 添加 Setter 方法,方便作为配置类参数 2019-05-20 09:46:16 +08:00
Yangkai.Shen 14c2b4cacf 🙈 更新 .gitignore 文件 排除 target 文件夹 2019-05-20 09:45:09 +08:00
yadong.zhang 54e97b0ec4 ♻️ 优化获取authorize的代码 2019-05-18 18:26:38 +08:00
yadong.zhang aa187dd109 更新 README.md 2019-05-18 15:46:55 +08:00
yadong.zhang 3cf8beab83 📝 编写文档 2019-05-18 15:39:00 +08:00
yadong.zhang 3824bc0887 📝 编写文档 2019-05-18 15:32:58 +08:00
yadong.zhang b8c3caea73 支持淘宝登录、修改AuthUser.java类中的accessToken属性,由原本的accessToken (String)改为token (AuthToken) 2019-05-18 15:31:17 +08:00
45 changed files with 920 additions and 121 deletions
+2
View File
@@ -26,3 +26,5 @@ hs_err_pid*
.idea
*.iml
*.sh
target
+54 -40
View File
@@ -18,38 +18,23 @@
<center>
<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>
</tr>
</thead>
<tbody>
<tr>
<td align="center" width="200"><a href="#授权gitee">Gitee</a></td>
<td align="center" width="200"><a href="#授权github">Github</a></td>
<td align="center" width="200"><a href="#授权weibo">Weibo</a></td>
<td align="center" width="200"><a href="#授权钉钉">钉钉</a></td>
<td align="center" width="200"><a href="#授权百度">百度</a></td>
<td align="center" width="200"><a href="#授权csdn">CSDN</a></td>
<td align="center" width="200"><a href="#授权coding">Coding</a></td>
<td align="center" width="200"><a href="#授权腾讯云开发者平台" title="coding升级后就变成腾讯云开发者平台了">腾讯云</a></td>
<td align="center" width="200"><a href="#授权oschina">OSChina</a></td>
<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>
</tr>
</tbody>
<tr>
<td align="center" width="200"><a href="#授权gitee"><img src="https://gitee.com/logo_icon.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权github"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/github.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权weibo"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/weibo.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权钉钉"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/dingding.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权百度"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/baidu.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权coding"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/coding.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权腾讯云开发者平台"><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="#授权oschina"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/oschinas.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权支付宝"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/alipay.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权qq"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/qq.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权微信"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权淘宝"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/taobao.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权google"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/google.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权facebook"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/facebook.png" width="20"></a></td>
<td align="center" width="200"><a href="#授权csdn"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/csdn.png" width="20"></a></td>
</tr>
</table>
</center>
@@ -67,7 +52,7 @@ JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>1.1.0</version>
<version>1.3.1</version>
</dependency>
```
- 调用api
@@ -84,6 +69,8 @@ authRequest.authorize();
authRequest.login("code");
```
**配套Demo**[JustAuth-demo](https://gitee.com/yadong.zhang/JustAuth-demo)
具体的例子可以参考:
- [实现Gitee授权登录](http://t.cn/ExDKxQs)
@@ -94,7 +81,7 @@ 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> |
@@ -102,8 +89,11 @@ authRequest.login("code");
| <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/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> |
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/google.png" width="20"> | [AuthGoogleRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java) | <a href="https://developers.google.com/identity/protocols/OpenIDConnect" 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/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/wechats.png" width="20"> | [AuthWeChatRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.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> |
## 后续开发计划
@@ -111,6 +101,15 @@ authRequest.login("code");
另外,期待您和我一起完善这个项目!
## 贡献代码
1. fork本项目到自己的repo
2. 把fork过去的项目也就是你仓库中的项目clone到你的本地
3. 修改代码
4. commit后push到自己的库
5. 发起PRpull request 请求
6. 等待作者合并
## 致谢
在项目立项初期,也对当前开源圈的一些相同类型的项目作过调研,同时本项目也参考过这些项目,再次感谢开源圈内的朋友。
@@ -118,7 +117,7 @@ authRequest.login("code");
[YurunOAuthLogin](https://gitee.com/yurunsoft/YurunOAuthLogin): PHP 第三方登录授权 SDK
## 参考图例
## 参考授权图例
#### 授权gitee
@@ -168,15 +167,30 @@ authRequest.login("code");
待续
# 交流
#### 授权淘宝
| 微信(备注:加群) | 公众号 |
![授权淘宝登录](https://images.gitee.com/uploads/images/2019/0518/154604_68b38305_784199.png "授权淘宝登录")
#### 授权Google
![授权google登录](https://images.gitee.com/uploads/images/2019/0521/190650_85c5f1c7_784199.png "授权google登录")
#### 授权Facebook
![授权facebook登录](https://images.gitee.com/uploads/images/2019/0521/233647_6a89fb45_784199.png "授权facebook登录")
## 关注&交流
| 公众号 | 微信(备注:加群) |
| :------------: | :------------: |
| <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" /> |
| <img src="https://gitee.com/yadong.zhang/static/raw/master/wx/wechat_account.jpg" width="200" /> | <img src="https://gitee.com/yadong.zhang/static/raw/master/wx/wx.png" width="170"/> |
**QQ群**
- JustAuth交流群 230017570):专业交流该项目
[![](https://images.gitee.com/uploads/images/2019/0129/191256_a40bceba_784199.png)](https://shang.qq.com/wpa/qunwpa?idkey=3571c554a143eff1e15807de033a240196c6b493b25b903d1d37571cfb6040aa)
- 开源总群 (190886500):各个开源项目的都有,也有博客建设等方面的朋友。(注意,该群需付费进入,防止发垃圾广告、垃圾推广等人士)
## 请喝咖啡
+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>1.1.0</version>
<version>1.3.1</version>
<name>JustAuth</name>
<url>https://gitee.com/yadong.zhang/JustAuth</url>
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 支付宝授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class AlipayAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getAlipayAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,15 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
/**
* 授权接口,用来获取具体第三方平台的授权地址
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public interface Authorization {
String getAuthorizeUrl(AuthConfig config);
}
@@ -0,0 +1,76 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthSource;
import me.zhyd.oauth.request.ResponseStatus;
import java.util.HashMap;
import java.util.Map;
/**
* 授权工厂类,负责创建指定平台的授权类获取授权地址
* <p>
* 使用策略模式 + 工厂模式 避免大量的if elseswatch)操作
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class AuthorizationFactory {
private static Map<String, Authorization> authorizationMap = new HashMap<>();
private static boolean loader = false;
private AuthorizationFactory() {
}
/**
* 根据第三方平台,获取具体的授权工具
*
* @param source 平台
* @return 具体的Authorization
*/
public static Authorization getAuthorize(AuthSource source) {
if (null == source) {
throw new AuthException(ResponseStatus.NO_AUTH_SOURCE);
}
registerAllAuthorize();
Authorization authorization = authorizationMap.get(source.toString());
if (null == authorization) {
throw new AuthException(ResponseStatus.UNIDENTIFIED_PLATFORM);
}
return authorization;
}
/**
* 将所有Authorize的实现类注册到authorizeMap中,
* 每次增加新的平台都需要在这儿添加注册代码
*/
private static void registerAllAuthorize() {
if (loader) {
return;
}
AuthorizationFactory.register(AuthSource.ALIPAY, new AlipayAuthorization());
AuthorizationFactory.register(AuthSource.BAIDU, new BaiduAuthorization());
AuthorizationFactory.register(AuthSource.CODING, new CodingAuthorization());
AuthorizationFactory.register(AuthSource.CSDN, new CsdnAuthorization());
AuthorizationFactory.register(AuthSource.DINGTALK, new DingTalkAuthorization());
AuthorizationFactory.register(AuthSource.GITEE, new GiteeAuthorization());
AuthorizationFactory.register(AuthSource.GITHUB, new GithubAuthorization());
AuthorizationFactory.register(AuthSource.GOOGLE, new GoogleAuthorization());
AuthorizationFactory.register(AuthSource.OSCHINA, new OschinaAuthorization());
AuthorizationFactory.register(AuthSource.QQ, new QqAuthorization());
AuthorizationFactory.register(AuthSource.TAOBAO, new TaobaoAuthorization());
AuthorizationFactory.register(AuthSource.TENCEN_CLOUD, new TencentCloudAuthorization());
AuthorizationFactory.register(AuthSource.WECHAT, new WeChatAuthorization());
AuthorizationFactory.register(AuthSource.WEIBO, new WeiboAuthorization());
AuthorizationFactory.register(AuthSource.FACEBOOK, new FacebookAuthorization());
loader = true;
}
private static void register(AuthSource authSource, Authorization authorization) {
authorizationMap.put(authSource.toString(), authorization);
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 百度授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class BaiduAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getBaiduAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* Coding授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class CodingAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getCodingAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* CSDN授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class CsdnAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getCsdnAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 钉钉授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class DingTalkAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getDingTalkQrConnectUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* Facebook授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.3
* @since 1.3
*/
public class FacebookAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getFacebookAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 码云授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class GiteeAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* Github授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class GithubAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* Google授权
*
* @author yangkai.shen (https://xkcoding.com)
* @version 1.3
* @since 1.3
*/
public class GoogleAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getGoogleAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 开源中国授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class OschinaAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getOschinaAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* QQ授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class QqAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getQqAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 淘宝授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class TaobaoAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getTaobaoAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 腾讯云授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class TencentCloudAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getTencentCloudAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 微信授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class WeChatAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getWeChatAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -0,0 +1,19 @@
package me.zhyd.oauth.authorization;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.utils.UrlBuilder;
/**
* 微博授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class WeiboAuthorization implements Authorization {
@Override
public String getAuthorizeUrl(AuthConfig config) {
return UrlBuilder.getWeiboAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
}
@@ -1,9 +1,6 @@
package me.zhyd.oauth.config;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
/**
* JustAuth配置类
@@ -12,6 +9,7 @@ import lombok.NoArgsConstructor;
* @version 1.0
* @since 1.8
*/
@Setter
@Getter
@Builder
@NoArgsConstructor
@@ -358,6 +358,93 @@ public enum ApiUrl {
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);
}
},
/**
* Google
*/
GOOGLE {
@Override
public String authorize() {
return "https://accounts.google.com/o/oauth2/v2/auth";
}
@Override
public String accessToken() {
return "https://www.googleapis.com/oauth2/v4/token";
}
@Override
public String userInfo() {
return "https://oauth2.googleapis.com/tokeninfo";
}
@Override
public String revoke() {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
@Override
public String refresh() {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
},
/**
* Facebook
*/
FACEBOOK {
@Override
public String authorize() {
return "https://www.facebook.com/v3.3/dialog/oauth";
}
@Override
public String accessToken() {
return "https://graph.facebook.com/v3.3/oauth/access_token";
}
@Override
public String userInfo() {
return "https://graph.facebook.com/v3.3/me";
}
@Override
public String revoke() {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
@Override
public String refresh() {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
};
/**
@@ -1,6 +1,8 @@
package me.zhyd.oauth.model;
/**
* 授权来源(平台)
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
@@ -16,8 +18,12 @@ public enum AuthSource {
OSCHINA,
TENCEN_CLOUD,
ALIPAY,
TAOBAO,
QQ,
WECHAT,
/**
* 谷歌登录,参考文档:https://developers.google.com/identity/protocols/OpenIDConnect
*/
GOOGLE,
FACEBOOK,
}
@@ -12,12 +12,17 @@ import lombok.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;
/**
* Google附带属性
*/
private String scope;
private String tokenType;
private String idToken;
}
@@ -21,5 +21,5 @@ public class AuthUser {
private String remark;
private AuthUserGender gender;
private AuthSource source;
private String accessToken;
private AuthToken token;
}
@@ -72,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,6 +3,7 @@ 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.*;
@@ -49,7 +50,7 @@ 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();
}
@@ -60,6 +61,12 @@ public class AuthBaiduRequest extends BaseAuthRequest {
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();
}
@@ -56,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();
}
@@ -46,7 +46,7 @@ public class AuthCsdnRequest extends BaseAuthRequest {
}
return AuthUser.builder()
.username(object.getString("username"))
.accessToken(accessToken)
.token(authToken)
.source(AuthSource.CSDN)
.build();
}
@@ -52,6 +52,7 @@ 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();
}
@@ -0,0 +1,72 @@
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.UrlBuilder;
/**
* Facebook登录
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public class AuthFacebookRequest extends BaseAuthRequest {
public AuthFacebookRequest(AuthConfig config) {
super(config, AuthSource.FACEBOOK);
}
@Override
protected AuthToken getAccessToken(String code) {
String accessTokenUrl = UrlBuilder.getFacebookAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
JSONObject object = JSONObject.parseObject(response.body());
if (object.containsKey("error")) {
throw new AuthException(object.getJSONObject("error").getString("message"));
}
return AuthToken.builder()
.accessToken(object.getString("access_token"))
.expireIn(object.getIntValue("expires_in"))
.tokenType(object.getString("token_type"))
.build();
}
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
HttpResponse response = HttpRequest.get(UrlBuilder.getFacebookUserInfoUrl(accessToken)).execute();
String userInfo = response.body();
JSONObject object = JSONObject.parseObject(userInfo);
if (object.containsKey("error")) {
throw new AuthException(object.getJSONObject("error").getString("message"));
}
String picture = null;
if (object.containsKey("picture")) {
JSONObject pictureObj = object.getJSONObject("picture");
pictureObj = pictureObj.getJSONObject("data");
if (null != pictureObj) {
picture = pictureObj.getString("url");
}
}
return AuthUser.builder()
.username(object.getString("name"))
.nickname(object.getString("name"))
.avatar(picture)
.location(object.getString("locale"))
.email(object.getString("email"))
.gender(AuthUserGender.getRealGender(object.getString("gender")))
.token(authToken)
.source(AuthSource.FACEBOOK)
.build();
}
}
@@ -51,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();
}
@@ -54,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();
}
@@ -0,0 +1,63 @@
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.utils.UrlBuilder;
/**
* Google登录
*
* @author yangkai.shen (https://xkcoding.com)
* @version 1.3
* @since 1.3
*/
public class AuthGoogleRequest extends BaseAuthRequest {
public AuthGoogleRequest(AuthConfig config) {
super(config, AuthSource.GOOGLE);
}
@Override
protected AuthToken getAccessToken(String code) {
String accessTokenUrl = UrlBuilder.getGoogleAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config
.getRedirectUri());
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
JSONObject object = JSONObject.parseObject(response.body());
if (object.containsKey("error") || object.containsKey("error_description")) {
throw new AuthException("get google access_token has error:[" + object.getString("error") + "], error_description:[" + object
.getString("error_description") + "]");
}
return AuthToken.builder()
.accessToken(object.getString("access_token"))
.expireIn(object.getIntValue("expires_in"))
.scope(object.getString("scope"))
.tokenType(object.getString("token_type"))
.idToken(object.getString("id_token"))
.build();
}
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getIdToken();
HttpResponse response = HttpRequest.get(UrlBuilder.getGoogleUserInfoUrl(accessToken)).execute();
String userInfo = response.body();
JSONObject object = JSONObject.parseObject(userInfo);
return AuthUser.builder()
.username(object.getString("name"))
.avatar(object.getString("picture"))
.nickname(object.getString("name"))
.location(object.getString("locale"))
.email(object.getString("email"))
.token(authToken)
.source(AuthSource.GOOGLE)
.build();
}
}
@@ -53,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();
}
@@ -1,5 +1,6 @@
package me.zhyd.oauth.request;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
@@ -9,9 +10,12 @@ 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.StringUtils;
import me.zhyd.oauth.utils.UrlBuilder;
import java.util.Map;
/**
* qq登录
*
@@ -26,14 +30,17 @@ public class AuthQqRequest extends BaseAuthRequest {
@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());
String accessTokenUrl = UrlBuilder.getQqAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config
.getRedirectUri());
HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
Map<String, String> accessTokenObject = GlobalAuthUtil.parseStringToMap(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"))
.accessToken(accessTokenObject.get("access_token"))
.expireIn(Integer.valueOf(accessTokenObject.get("expires_in")))
.refreshToken(accessTokenObject.get("refresh_token"))
.build();
}
@@ -55,15 +62,20 @@ public class AuthQqRequest extends BaseAuthRequest {
.nickname(object.getString("nickname"))
.avatar(avatar)
.gender(AuthUserGender.getRealGender(object.getString("gender")))
.accessToken(accessToken)
.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();
HttpResponse response = HttpRequest.get(UrlBuilder.getQqOpenidUrl("https://graph.qq.com/oauth2.0/me", accessToken))
.execute();
if (response.isOk()) {
JSONObject object = JSONObject.parseObject(response.body());
String body = response.body();
String removePrefix = StrUtil.replace(body, "callback(", "");
String removeSuffix = StrUtil.replace(removePrefix, ");", "");
String openId = StrUtil.trim(removeSuffix);
JSONObject object = JSONObject.parseObject(openId);
if (object.containsKey("openid")) {
return object.getString("openid");
}
@@ -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();
}
}
@@ -56,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();
}
@@ -30,12 +30,11 @@ public class AuthWeChatRequest extends BaseAuthRequest {
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
JSONObject object = JSONObject.parseObject(response.body());
if (!object.containsKey("access_token") || !object.containsKey("openid") || !object
.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"))
@@ -60,7 +59,7 @@ public class AuthWeChatRequest extends BaseAuthRequest {
.avatar(object.getString("headimgurl"))
.location(object.getString("country") + "-" + object.getString("province") + "-" + object.getString("city"))
.gender(AuthUserGender.getRealGender(object.getString("sex")))
.accessToken(accessToken)
.token(authToken)
.source(AuthSource.WECHAT)
.build();
}
@@ -35,24 +35,28 @@ public class AuthWeiboRequest extends BaseAuthRequest {
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 AuthToken.builder()
.accessToken(String.format("uid=%s&access_token=%s", uid, accessToken))
.accessToken(accessTokenObject.getString("access_token"))
.uid(accessTokenObject.getString("uid"))
.expireIn(accessTokenObject.getIntValue("remind_in"))
.build();
}
@Override
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"))
@@ -61,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();
}
@@ -1,6 +1,7 @@
package me.zhyd.oauth.request;
import lombok.Data;
import me.zhyd.oauth.authorization.AuthorizationFactory;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthResponse;
@@ -8,7 +9,6 @@ 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;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
@@ -44,49 +44,6 @@ public abstract class BaseAuthRequest implements AuthRequest {
@Override
public String authorize() {
String authorizeUrl = null;
switch (source) {
case WEIBO:
authorizeUrl = UrlBuilder.getWeiboAuthorizeUrl(config.getClientId(), config.getRedirectUri());
break;
case BAIDU:
authorizeUrl = UrlBuilder.getBaiduAuthorizeUrl(config.getClientId(), config.getRedirectUri());
break;
case DINGTALK:
authorizeUrl = UrlBuilder.getDingTalkQrConnectUrl(config.getClientId(), config.getRedirectUri());
break;
case GITEE:
authorizeUrl = UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
break;
case GITHUB:
authorizeUrl = UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
break;
case CSDN:
authorizeUrl = UrlBuilder.getCsdnAuthorizeUrl(config.getClientId(), config.getRedirectUri());
break;
case CODING:
authorizeUrl = UrlBuilder.getCodingAuthorizeUrl(config.getClientId(), config.getRedirectUri());
break;
case TENCEN_CLOUD:
authorizeUrl = UrlBuilder.getTencentCloudAuthorizeUrl(config.getClientId(), config.getRedirectUri());
break;
case OSCHINA:
authorizeUrl = UrlBuilder.getOschinaAuthorizeUrl(config.getClientId(), config.getRedirectUri());
break;
case ALIPAY:
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 GOOGLE:
break;
default:
break;
}
return authorizeUrl;
return AuthorizationFactory.getAuthorize(source).getAuthorizeUrl(config);
}
}
@@ -11,6 +11,8 @@ public enum ResponseStatus {
NOT_IMPLEMENTED(5001, "Not Implemented"),
PARAMETER_INCOMPLETE(5002, "Parameter incomplete"),
UNSUPPORTED(5003, "Unsupported operation"),
NO_AUTH_SOURCE(5004, "AuthSource cannot be null"),
UNIDENTIFIED_PLATFORM(5005, "Unidentified platform"),
;
private int code;
@@ -17,6 +17,10 @@ public class UrlBuilder {
private static final String GITHUB_USER_INFO_PATTERN = "{0}?access_token={1}";
private static final String GITHUB_AUTHORIZE_PATTERN = "{0}?client_id={1}&state=1&redirect_uri={2}";
private static final String GOOGLE_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&scope=openid%20email%20profile&redirect_uri={2}&state={3}";
private static final String GOOGLE_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code";
private static final String GOOGLE_USER_INFO_PATTERN = "{0}?id_token={1}";
private static final String WEIBO_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}";
private static final String WEIBO_USER_INFO_PATTERN = "{0}?{1}";
private static final String WEIBO_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}";
@@ -61,6 +65,13 @@ public class UrlBuilder {
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";
private static final String FACEBOOK_AUTHORIZE_PATTERN = "{0}?client_id={1}&redirect_uri={2}&state={3}&response_type=code&scope=";
private static final String FACEBOOK_ACCESS_TOKEN_PATTERN = "{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code";
private static final String FACEBOOK_USER_INFO_PATTERN = "{0}?access_token={1}&fields=id,name,birthday,gender,hometown,email,devices,picture.width(400)";
/**
* 获取githubtoken的接口地址
*
@@ -465,4 +476,96 @@ public class UrlBuilder {
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);
}
/**
* 获取Google授权地址
*
* @param clientId google 应用的Client ID
* @param redirectUrl google 应用授权成功后的回调地址
* @return full url
*/
public static String getGoogleAuthorizeUrl(String clientId, String redirectUrl) {
return MessageFormat.format(GOOGLE_AUTHORIZE_PATTERN, ApiUrl.GOOGLE.authorize(), clientId, redirectUrl, System.currentTimeMillis());
}
/**
* 获取Google token的接口地址
*
* @param clientId google应用的Client ID
* @param clientSecret google应用的Client Secret
* @param code google授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return full url
*/
public static String getGoogleAccessTokenUrl(String clientId, String clientSecret, String code, String redirectUri) {
return MessageFormat.format(GOOGLE_ACCESS_TOKEN_PATTERN, ApiUrl.GOOGLE.accessToken(), clientId, clientSecret, code, redirectUri);
}
/**
* 获取Google用户详情的接口地址
*
* @param token google 应用的token
* @return full url
*/
public static String getGoogleUserInfoUrl(String token) {
return MessageFormat.format(GOOGLE_USER_INFO_PATTERN, ApiUrl.GOOGLE.userInfo(), token);
}
/**
* 获取Facebook授权地址
*
* @param clientId Facebook 应用的Client ID
* @param redirectUrl Facebook 应用授权成功后的回调地址
* @return full url
*/
public static String getFacebookAuthorizeUrl(String clientId, String redirectUrl) {
return MessageFormat.format(FACEBOOK_AUTHORIZE_PATTERN, ApiUrl.FACEBOOK.authorize(), clientId, redirectUrl, System.currentTimeMillis());
}
/**
* 获取Facebook token的接口地址
*
* @param clientId Facebook 应用的Client ID
* @param clientSecret Facebook 应用的Client Secret
* @param code Facebook 授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return full url
*/
public static String getFacebookAccessTokenUrl(String clientId, String clientSecret, String code, String redirectUri) {
return MessageFormat.format(FACEBOOK_ACCESS_TOKEN_PATTERN, ApiUrl.FACEBOOK.accessToken(), clientId, clientSecret, code, redirectUri);
}
/**
* 获取Facebook用户详情的接口地址
*
* @param token Facebook 应用的token
* @return full url
*/
public static String getFacebookUserInfoUrl(String token) {
return MessageFormat.format(FACEBOOK_USER_INFO_PATTERN, ApiUrl.FACEBOOK.userInfo(), token);
}
}
@@ -141,4 +141,17 @@ public class AuthRequestTest {
// 授权登录后会返回一个code,用这个code进行登录
AuthResponse login = authRequest.login("code");
}
@Test
public void googleTest() {
AuthRequest authRequest = new AuthGoogleRequest(AuthConfig.builder()
.clientId("clientId")
.clientSecret("clientSecret")
.redirectUri("redirectUri")
.build());
// 返回授权页面,可自行调整
String url = authRequest.authorize();
// 授权登录后会返回一个code,用这个code进行登录
AuthResponse login = authRequest.login("code");
}
}
+20
View File
@@ -0,0 +1,20 @@
### 2019/05/23
1. 修复QQ登录的问题
2. 发布1.3.1版本的jar包到公开仓库
### 2019/05/21
1. 新增google授权登录
2. 新增facebook授权登录
3. 发布1.3.0版本的jar包到公开仓库
### 2019/05/18
1. 发布1.1.0版本的jar包到公开仓库(支持qq和微信登录)
2. 支持淘宝登录
3. 修改`AuthUser.java`类中的`accessToken`属性,由原本的~~accessToken (String)~~改为`token (AuthToken)`
4. 修复一些bug
5. 发布1.2.0版本的jar包到公开仓库(支持淘宝登录)
----
### 2019/05/17
1. 增加qq和微信的授权登录
2. 修改getAccessToken方法的返回值