1
0
mirror of synced 2026-05-22 13:43:20 +00:00

Compare commits

...

20 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
33 changed files with 758 additions and 104 deletions
+2
View File
@@ -26,3 +26,5 @@ hs_err_pid*
.idea
*.iml
*.sh
target
+49 -42
View File
@@ -18,40 +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="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>
<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>
<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>
@@ -69,7 +52,7 @@ JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>1.2.0</version>
<version>1.3.1</version>
</dependency>
```
- 调用api
@@ -86,6 +69,8 @@ authRequest.authorize();
authRequest.login("code");
```
**配套Demo**[JustAuth-demo](https://gitee.com/yadong.zhang/JustAuth-demo)
具体的例子可以参考:
- [实现Gitee授权登录](http://t.cn/ExDKxQs)
@@ -104,9 +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/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/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) | 待续 |
## 后续开发计划
@@ -114,6 +101,15 @@ authRequest.login("code");
另外,期待您和我一起完善这个项目!
## 贡献代码
1. fork本项目到自己的repo
2. 把fork过去的项目也就是你仓库中的项目clone到你的本地
3. 修改代码
4. commit后push到自己的库
5. 发起PRpull request 请求
6. 等待作者合并
## 致谢
在项目立项初期,也对当前开源圈的一些相同类型的项目作过调研,同时本项目也参考过这些项目,再次感谢开源圈内的朋友。
@@ -121,7 +117,7 @@ authRequest.login("code");
[YurunOAuthLogin](https://gitee.com/yurunsoft/YurunOAuthLogin): PHP 第三方登录授权 SDK
## 参考图例
## 参考授权图例
#### 授权gitee
@@ -173,17 +169,28 @@ 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.2.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
@@ -383,6 +383,64 @@ public enum ApiUrl {
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
@@ -19,5 +21,9 @@ public enum AuthSource {
TAOBAO,
QQ,
WECHAT,
/**
* 谷歌登录,参考文档:https://developers.google.com/identity/protocols/OpenIDConnect
*/
GOOGLE,
FACEBOOK,
}
@@ -17,4 +17,12 @@ public class AuthToken {
private String uid;
private String openId;
private String accessCode;
/**
* Google附带属性
*/
private String scope;
private String tokenType;
private String idToken;
}
@@ -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();
}
}
@@ -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();
}
}
@@ -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();
}
@@ -61,9 +68,14 @@ public class AuthQqRequest extends BaseAuthRequest {
}
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");
}
@@ -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"))
@@ -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,52 +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 TAOBAO:
authorizeUrl = UrlBuilder.getTaobaoAuthorizeUrl(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}";
@@ -64,6 +68,10 @@ public class UrlBuilder {
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的接口地址
*
@@ -492,4 +500,72 @@ public class UrlBuilder {
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");
}
}
+8
View File
@@ -1,3 +1,11 @@
### 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和微信登录)