diff --git a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md b/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md index 55b740b56ad88c291c99e42ad4bc4f028b34638c..9077e2240c40d4445b07132e5c255e0ca4aa1076 100644 --- a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md +++ b/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md @@ -18,9 +18,12 @@ - [ ] 否,还没做测试,需要作者自测 注:测试demo可以使用: -- [Springboot版](https://github.com/justauth/JustAuth-demo) +- [simple版](https://github.com/justauth/JustAuth-demo) - [jFinal版](https://github.com/xkcoding/jfinal-justauth-demo) - [ActFramework版](https://github.com/xkcoding/act-justauth-demo) +- [Nutzboot版](https://github.com/EggsBlue/nutzboot-justauth-demo) +- [Blade版](https://github.com/justauth/blade-justauth-demo) + diff --git a/README.en-US.md b/README.en-US.md index 6905b90850cfa332daf101e0a627b56fbd9db3c1..b0ca4c3c917b645bfc992ced734976a15332f560 100644 --- a/README.en-US.md +++ b/README.en-US.md @@ -6,7 +6,7 @@

- + @@ -15,7 +15,7 @@ - + @@ -68,6 +68,7 @@ + @@ -100,7 +101,7 @@ These artifacts are available from Maven Central: me.zhyd.oauth JustAuth - ${latest.version} + 1.13.0 ``` - Using JustAuth @@ -162,7 +163,7 @@ authRequest.login(callback); | | [AuthGitlabRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGitlabRequest.java) | 参考文档 | | | [AuthMeituanRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthMeituanRequest.java) | 参考文档 | | | [AuthElemeRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java) | 参考文档 | -| | [AuthTwitterRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java) | 参考文档 | +| | [AuthTwitterRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java) | 参考文档 | | | [AuthCsdnRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java) | 无 | diff --git a/README.md b/README.md index 655500a98337997e3a1c14284cd24975e7f35066..3c4bd06e277e3f92e1d244fb48e7e2b837c33782 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@

- + @@ -15,7 +15,7 @@ - + @@ -68,6 +68,7 @@

+
@@ -100,7 +101,7 @@ JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具 me.zhyd.oauth JustAuth - 1.12.0 + 1.13.0 ``` - 调用api @@ -119,7 +120,7 @@ authRequest.login(callback); ``` **配套Demo**: -- [Springboot版](https://github.com/justauth/JustAuth-demo) +- [JustAuth-demo](https://github.com/justauth/JustAuth-demo) - [jFinal版](https://github.com/xkcoding/jfinal-justauth-demo): Jfinal集成JustAuth的demo by [xkcoding](https://github.com/xkcoding) - [ActFramework版](https://github.com/xkcoding/act-justauth-demo): ActFramework 集成 JustAuth 的 demo by [xkcoding](https://github.com/xkcoding) - [Nutzboot版](https://github.com/EggsBlue/nutzboot-justauth-demo): NutzBoot集成JustAuth的demo by [蛋蛋](https://github.com/EggsBlue) @@ -168,7 +169,7 @@ authRequest.login(callback); | | [AuthGitlabRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGitlabRequest.java) | 参考文档 | | | [AuthMeituanRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthMeituanRequest.java) | 参考文档 | | | [AuthElemeRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java) | 参考文档 | -| | [AuthTwitterRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java) | 参考文档 | +| | [AuthTwitterRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java) | 参考文档 | | | [AuthCsdnRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java) | 无 | _请知悉:经咨询CSDN官方客服得知,CSDN的授权开放平台已经下线。如果以前申请过的应用,可以继续使用,但是不再支持申请新的应用。so, 本项目中的CSDN登录只能针对少部分用户使用了_ diff --git a/docs/Q&A.md b/docs/Q&A.md index 49303ddd1642e67b9f4132fe70ff839075efcc23..4affc30c56ce9eb13dadc6651eea766184714541 100644 --- a/docs/Q&A.md +++ b/docs/Q&A.md @@ -1,4 +1,25 @@ -## ~~升级到1.8.0后如何启用state?~~ +## 1.项目编译报错 + **问题描述** + +在使用JustAuth时,`AuthConfig.builder()`报错,提示没有`builder()`方法 + + **解决方案:** + +安装完lombok插件即可 + +[eclipse安装lombok](https://www.zhyd.me/article/61) + + **idea安装lombok:** file->settings->plugins->Browse Repositories->输入`lombok`,点击搜索->选中lombok点击install->安装完成后重启idea + + **进阶版问题:** +idea中安装完lombok并重启idea之后,启动项目仍然报错: +![输入图片说明](https://gitee.com/uploads/images/2018/0522/153610_ffe84828_784199.png "1.png") + **解决办法** +![输入图片说明](https://gitee.com/uploads/images/2018/0522/153715_75193de5_784199.jpeg "2.jpg") +默认是eclipse 选择javac就可以了 + + +## 2.~~升级到1.8.0后如何启用state?~~ ~~在原api使用方法的基础上,为config追加一个state即可。~~ ``` @@ -10,7 +31,7 @@ AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder() .build()); ``` -## ~~升级到1.8.0后login方法报错?~~ +## 3.~~升级到1.8.0后login方法报错?~~ ~~这是因为1.8.0版本中新增了[AuthCallback](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/model/AuthCallback.java)类,这个类封装了所有可能的回调参数。目前包含以下三个参数:~~ - ~~`code`: 访问AuthorizeUrl后回调时带的参数code,用来换取token~~ @@ -30,7 +51,7 @@ public Object login(@PathVariable("source") String source, AuthCallback callback ``` ~~_代码截取自_ :https://github.com/justauth/JustAuth-demo~~ -## ~~升级到1.8.0后对于state参数有什么特殊要求吗?~~ +## 4.~~升级到1.8.0后对于state参数有什么特殊要求吗?~~ ~~理论上没有,stata只是用来保持会话状态,因为http协议的无状态性,从授权到回调,无法感知具体是哪个用户触发的。所以可以使用state作为校验。注:state参数每次完整的授权链中只可用一次!(也是为了防止不必要的危险)~~ @@ -42,7 +63,7 @@ public Object login(@PathVariable("source") String source, AuthCallback callback ~~注:`authorize`和`login`(不是指回调传回的`state`,而是声明`request`时传入的`state`)中传的`state`务必保证一致~~ -## 升级到1.9.3+版本后编译失败 +## 5.升级到1.9.3+版本后编译失败 主要明显的就是`IpUtils.getIp`和request的`.state`报错。 @@ -64,7 +85,7 @@ public Object login(@PathVariable("source") String source, AuthCallback callback } ``` -## 升级到最新版本后为什么支付宝登录不能用了? +## 6.升级到最新版本后为什么支付宝登录不能用了? 在升级到新版后,使用支付宝登录会提示`ClassNotFoundExcption`异常,这是因为从`1.9.4`版本开始,JustAuth将不在强依赖`alipay-sdk-java`,如果你需要用到Alipay的授权登陆,那么你还需要添加以下依赖: @@ -75,3 +96,45 @@ public Object login(@PathVariable("source") String source, AuthCallback callback 3.7.4.ALL ``` + +## 7.“Scope参数错误或者Scope没有权限”是怎么回事? + +微信开放平台授权登录提示“Scope参数错误或者Scope没有权限” + +![Scope参数错误或者Scope没有权限](./_media/scope_error.png) + +解决办法:请检查微信开放平台的应用是否有“微信登录”接口的权限。 + +![“微信登录”接口](./_media/scope_error2.png) + +“接口状态”必须为“已获得”! + +## 8.微信登录时能在微信端提示登录成功吗? + +不可以,这是**微信公众平台**的功能,截至到目前(JustAuth v1.12.0)为止,暂不支持**微信公众平台**的授权登录 + +## 9.微信登录时能不需要手机确认吗?扫码后就自动登录 + +不可以,微信开放平台不支持这种操作。可以把微信扫码登录理解成qq用账号密码登录,扫完码后不手动点确认,微信怎么知道用户是否同意了授权? +当然,**微信公众平台**的授权流程可以越过这个限制,只要关注了公众号,后续扫码成功后就会自动登录,但是这是**微信公众平台**的功能,截至到目前(JustAuth v1.12.0)为止,暂不支持**微信公众平台**的授权登录 + +## 10.本地如何测那些*不支持本地地址回调*的授权登录? + +推荐几种方案: +1. 改`hosts`,然后将测试程序的端口改为`80` +2. 使用`Nginx`/`Apache`做代理 +3. FRP内网穿透,参考地址:[使用内网穿透的方式集成第三方登录](https://xkcoding.com/2019/05/22/spring-boot-login-with-oauth.html) + +## 11.如何获取QQ登录的`unionId`? + +在AuthConfig中设备`unionId`为`true` + +```java +AuthRequest authRequest = new AuthQqRequest(AuthConfig.builder() + .clientId("clientId") + .clientSecret("clientSecret") + .redirectUri("redirectUri") + .unionId(true) + .build()); +``` +> 注:使用unionId要求开发者必须已在qq开放平台申请了获取unionId的权限,否则可能会发生错误!切记!参考链接:[unionid介绍](http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D) \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index 017fab189b789911543a7bab4c801486418015ff..6b8f3b50ee479ac64995dbfc75b3395b0464710f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -9,7 +9,7 @@

- + @@ -18,7 +18,7 @@ - + @@ -91,6 +91,7 @@ JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具 | | [AuthGitlabRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGitlabRequest.java) | 参考文档 | | | [AuthMeituanRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthMeituanRequest.java) | 参考文档 | | | [AuthElemeRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java) | 参考文档 | +| | [AuthTwitterRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java) | 参考文档 | | | [AuthCsdnRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java) | 无 | diff --git a/docs/_coverpage.md b/docs/_coverpage.md index d3f43875d4f056650f2fa2502d9c46b2d3e469b6..372bb568acc0c4dd0285de3e854cd80464bdb2f2 100644 --- a/docs/_coverpage.md +++ b/docs/_coverpage.md @@ -1,6 +1,6 @@ ![](_media/logo.png) -# JustAuth 1.12.0 +# JustAuth 1.13.0 史上最全的整合第三方登录的开源库 diff --git a/docs/_media/custom/gitlab.png b/docs/_media/custom/gitlab.png new file mode 100644 index 0000000000000000000000000000000000000000..567f6fcd0850cf2ca54bc127b050d410b2699b25 Binary files /dev/null and b/docs/_media/custom/gitlab.png differ diff --git a/docs/_media/custom/gitlab2.png b/docs/_media/custom/gitlab2.png new file mode 100644 index 0000000000000000000000000000000000000000..26b904d13493196a9f345487339989f56b20e823 Binary files /dev/null and b/docs/_media/custom/gitlab2.png differ diff --git a/docs/_media/custom/gitlab3.png b/docs/_media/custom/gitlab3.png new file mode 100644 index 0000000000000000000000000000000000000000..679930a2a1394ba5e40830bc1cc064794699dfe0 Binary files /dev/null and b/docs/_media/custom/gitlab3.png differ diff --git a/docs/_media/custom/gitlab4.png b/docs/_media/custom/gitlab4.png new file mode 100644 index 0000000000000000000000000000000000000000..4049d38efe3829f820e9b7f189edc3a5e452c2c0 Binary files /dev/null and b/docs/_media/custom/gitlab4.png differ diff --git a/docs/_media/oauth/gitee_01.png b/docs/_media/oauth/gitee_01.png new file mode 100644 index 0000000000000000000000000000000000000000..3c8d6edd6b6fec39a2b4fa684d355ac961dcf05e Binary files /dev/null and b/docs/_media/oauth/gitee_01.png differ diff --git a/docs/_media/oauth/gitee_02.png b/docs/_media/oauth/gitee_02.png new file mode 100644 index 0000000000000000000000000000000000000000..03e6778adc4075aa37988a85c668c9f68d072f0b Binary files /dev/null and b/docs/_media/oauth/gitee_02.png differ diff --git a/docs/_media/oauth/gitee_03.png b/docs/_media/oauth/gitee_03.png new file mode 100644 index 0000000000000000000000000000000000000000..6987ab9a22c394a19bdaaf900c409e37fcd6844c Binary files /dev/null and b/docs/_media/oauth/gitee_03.png differ diff --git a/docs/_media/oauth/gitee_04.png b/docs/_media/oauth/gitee_04.png new file mode 100644 index 0000000000000000000000000000000000000000..4d1e91eaecb1b46ae816db5a83b6e87eee816025 Binary files /dev/null and b/docs/_media/oauth/gitee_04.png differ diff --git a/docs/_media/oauth/kujiale_1.png b/docs/_media/oauth/kujiale_1.png new file mode 100644 index 0000000000000000000000000000000000000000..02eb4626367e0919baa6455465919212c120dce4 Binary files /dev/null and b/docs/_media/oauth/kujiale_1.png differ diff --git a/docs/_media/oauth/kujiale_2.png b/docs/_media/oauth/kujiale_2.png new file mode 100644 index 0000000000000000000000000000000000000000..dac82e0be86417772c41f31cc47f2d4ce8d56847 Binary files /dev/null and b/docs/_media/oauth/kujiale_2.png differ diff --git a/docs/_media/oauth/kujiale_3.png b/docs/_media/oauth/kujiale_3.png new file mode 100644 index 0000000000000000000000000000000000000000..71552ff005f90a025a4e652715a97e7e32dec024 Binary files /dev/null and b/docs/_media/oauth/kujiale_3.png differ diff --git a/docs/_media/oauth/kujiale_4.png b/docs/_media/oauth/kujiale_4.png new file mode 100644 index 0000000000000000000000000000000000000000..5dc48a1fdf88af297bb8305a90294b2509d95967 Binary files /dev/null and b/docs/_media/oauth/kujiale_4.png differ diff --git a/docs/_media/oauth/kujiale_5.png b/docs/_media/oauth/kujiale_5.png new file mode 100644 index 0000000000000000000000000000000000000000..1dd25bfe743adb4a88fa285d89cf9c619ad313e1 Binary files /dev/null and b/docs/_media/oauth/kujiale_5.png differ diff --git a/docs/_media/oauth/kujiale_6.png b/docs/_media/oauth/kujiale_6.png new file mode 100644 index 0000000000000000000000000000000000000000..5af409b772882ee2042d65edb65bf02c6498087d Binary files /dev/null and b/docs/_media/oauth/kujiale_6.png differ diff --git a/docs/_media/scope_error.png b/docs/_media/scope_error.png new file mode 100644 index 0000000000000000000000000000000000000000..33f4b8717df38bf8cd49744fdc1dd5c1609e0b96 Binary files /dev/null and b/docs/_media/scope_error.png differ diff --git a/docs/_media/scope_error2.png b/docs/_media/scope_error2.png new file mode 100644 index 0000000000000000000000000000000000000000..db50aae6b1027ea44bc5152f1da42885e2c9059c Binary files /dev/null and b/docs/_media/scope_error2.png differ diff --git a/docs/_sidebar.md b/docs/_sidebar.md index deb1e5c112203556770b0592a51dda1308f47722..0e16a670914eefd95e2045ec13edc45ce01f7884 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -4,11 +4,43 @@ - [名词解释](explain.md) - [OAuth流程](oauth.md) - [如何使用](how-to-use.md) -- 其他特性 +- [Q&A](Q&A.md) +- 集成第三方 + - [Gitee登录](oauth/gitee.md) + - [Github登录](oauth/github.md) + - [qq登录](oauth/qq.md) + - [微信登录](oauth/wechat.md) + - [微博登录](oauth/weibo.md) + - [支付宝登录](oauth/alipay.md) + - [百度登录](oauth/baidu.md) + - [Coding登录](oauth/coding.md) + - [钉钉登录](oauth/dingtalk.md) + - [开源中国登录](oauth/oschina.md) + - [淘宝登录](oauth/taobao.md) + - [腾讯云登录](oauth/tenchentCloud.md) + - [Google登录](oauth/google.md) + - [Facebook登录](oauth/facebook.md) + - [抖音登录](oauth/douyin.md) + - [领英登录](oauth/linkedin.md) + - [微软登录](oauth/microsoft.md) + - [小米登录](oauth/mi.md) + - [头条登录](oauth/toutiao.md) + - [Teambition登录](oauth/teambition.md) + - [人人登录](oauth/renren.md) + - [Pinterest登录](oauth/pinterest.md) + - [Stackoverflow登录](oauth/stackoverflow.md) + - [华为登录](oauth/huawei.md) + - [微信企业版登录](oauth/wechatEnterprise.md) + - [酷家乐登录](oauth/kujiale.md) + - [Gitlab登录](oauth/gitlab.md) + - [美团登录](oauth/meituan.md) + - [饿了么登录](oauth/eleme.md) + - [CSDN登录](oauth/csdn.md) +- 高级特性 - [使用State](using-state.md) - [自定义state缓存](customize-the-state-cache.md) + - [自定义第三方平台的OAuth](customize-the-oauth.md) - [配套项目](supporting.md) -- [Q&A](Q&A.md) - [Who is using](users.md) - [致谢](thx.md) - [更新记录](update.md) diff --git a/docs/contributors.md b/docs/contributors.md index 8393a50b766109be8e5c6f21fe3d31755b24ac99..ad11a9e87e9b8bfe35de90e82d8539738c4ce960 100644 --- a/docs/contributors.md +++ b/docs/contributors.md @@ -10,7 +10,7 @@ - 提供ActFramework版demo - 提供SpringBoot快速集成的justauth-spring-boot-starter - · pengisgood : [Github] | [个人网站] - - 集成人人登录、Pinterest登录、StackOverflow登录 + - 集成人人登录、Pinterest登录、StackOverflow登录、Twitter登录 - · skqing : [Gitee] | [个人网站] - 修复钉钉登录的部分问题 - 优化微博登录 diff --git a/docs/customize-the-oauth.md b/docs/customize-the-oauth.md new file mode 100644 index 0000000000000000000000000000000000000000..efe69e0c53cf5073a42590e38c8482943ef5ed2a --- /dev/null +++ b/docs/customize-the-oauth.md @@ -0,0 +1,208 @@ +# 自定义第三方平台的OAuth + +[JustAuth](https://github.com/justauth/JustAuth)发展到现在,基本上已经涵盖了国内外大多数知名的网站。[JustAuth](https://github.com/justauth/JustAuth)也一直以它的**全**和**简**,备受各位朋友的厚爱、支持。 + +但现在OAuth技术越来越成熟,越来越多的个人站长或者企业都开始搭建自己的OAuth授权平台,那么针对这种情况,[JustAuth](https://github.com/justauth/JustAuth)并不能做到面面俱到,无法去集成所有支持OAuth的网站(这也是不现实的)。 + +既然考虑到有这种需求,那么就要想办法解决,想办法填补漏洞,不为了自己,也为了陪伴[JustAuth](https://github.com/justauth/JustAuth)一路走来的所有朋友们。 + +[JustAuth](https://github.com/justauth/JustAuth)开发团队也在[v1.12.0](https://github.com/justauth/JustAuth/releases/tag/v1.12.0)版本中新加入了一大特性,就是可以支持任意支持OAuth的网站通过JustAuth实现便捷的OAuth登录! + +下面我们会通过一个小例子,讲解如何使用JustAuth集成自己搭建的GitLab服务。 + +> 注:本节将会使用[JustAuth-demo](https://github.com/justauth/JustAuth-demo)Demo项目进行演示 + +## 搭建Gitlab服务 + +请自行解决,如果已有搭建好的Gitlab私服,请直接进入下面的步骤 + +## 创建应用 + +![gitlab私服创建应用](./_media/custom/gitlab.png) +![gitlab私服应用详情](./_media/custom/gitlab2.png) + +## 实现AuthSource接口 + +`AuthSource.java`是为了提供OAuth平台的API地址的统一接口,提供以下方法供实现: +- `AuthSource#authorize()`: 获取授权url. 必须实现 +- `AuthSource#accessToken()`: 获取accessToken的url. 必须实现 +- `AuthSource#userInfo()`: 获取用户信息的url. 必须实现 +- `AuthSource#revoke()`: 获取取消授权的url. 非必须实现接口(部分平台不支持) +- `AuthSource#refresh()`: 获取刷新授权的url. 非必须实现接口(部分平台不支持) + +**注:** +- 当通过JustAuth扩展实现第三方授权时,请参考`AuthDefaultSource`自行创建对应的枚举类并实现`AuthSource`接口 +- 如果不是使用的枚举类,那么在授权成功后获取用户信息时,需要**单独处理source字段**的赋值 +- 如果扩展了对应枚举类时,在`me.zhyd.oauth.request.AuthRequest#login(AuthCallback)`中可以通过`xx.toString()`获取对应的source + +```java +package me.zhyd.justauth; + +import me.zhyd.oauth.config.AuthSource; + +/** + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @website https://www.zhyd.me + * @date 2019/10/26 16:54 + * @since 1.8 + */ +public enum AuthCustomSource implements AuthSource { + + /** + * 自己搭建的gitlab私服 + */ + MYGITLAB { + /** + * 授权的api + * + * @return url + */ + @Override + public String authorize() { + return "http://gitlab.xxx.com/oauth/authorize"; + } + + /** + * 获取accessToken的api + * + * @return url + */ + @Override + public String accessToken() { + return "http://gitlab.xxx.com/oauth/token"; + } + + /** + * 获取用户信息的api + * + * @return url + */ + @Override + public String userInfo() { + return "http://gitlab.xxx.com/api/v4/user"; + } + } +} +``` + +## 创建自定义的Request + +```java +package me.zhyd.justauth; + +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson.JSONObject; +import me.zhyd.oauth.cache.AuthStateCache; +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.enums.AuthUserGender; +import me.zhyd.oauth.exception.AuthException; +import me.zhyd.oauth.model.AuthCallback; +import me.zhyd.oauth.model.AuthToken; +import me.zhyd.oauth.model.AuthUser; +import me.zhyd.oauth.request.AuthDefaultRequest; +import me.zhyd.oauth.utils.UrlBuilder; + +/** + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @website https://www.zhyd.me + * @date 2019/10/26 17:00 + * @since 1.8 + */ +public class AuthMyGitlabRequest extends AuthDefaultRequest { + + + public AuthMyGitlabRequest(AuthConfig config) { + super(config, AuthCustomSource.MYGITLAB); + } + + public AuthMyGitlabRequest(AuthConfig config, AuthStateCache authStateCache) { + super(config, AuthCustomSource.MYGITLAB, authStateCache); + } + + @Override + protected AuthToken getAccessToken(AuthCallback authCallback) { + HttpResponse response = doPostAuthorizationCode(authCallback.getCode()); + JSONObject object = JSONObject.parseObject(response.body()); + + this.checkResponse(object); + + return AuthToken.builder() + .accessToken(object.getString("access_token")) + .refreshToken(object.getString("refresh_token")) + .idToken(object.getString("id_token")) + .tokenType(object.getString("token_type")) + .scope(object.getString("scope")) + .build(); + } + + @Override + protected AuthUser getUserInfo(AuthToken authToken) { + HttpResponse response = doGetUserInfo(authToken); + JSONObject object = JSONObject.parseObject(response.body()); + + this.checkResponse(object); + + return AuthUser.builder() + .uuid(object.getString("id")) + .username(object.getString("username")) + .nickname(object.getString("name")) + .avatar(object.getString("avatar_url")) + .blog(object.getString("web_url")) + .company(object.getString("organization")) + .location(object.getString("location")) + .email(object.getString("email")) + .remark(object.getString("bio")) + .gender(AuthUserGender.UNKNOWN) + .token(authToken) + .source(source.toString()) + .build(); + } + + private void checkResponse(JSONObject object) { + // oauth/token 验证异常 + if (object.containsKey("error")) { + throw new AuthException(object.getString("error_description")); + } + // user 验证异常 + if (object.containsKey("message")) { + throw new AuthException(object.getString("message")); + } + } + + /** + * 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state} + * + * @param state state 验证授权流程的参数,可以防止csrf + * @return 返回授权地址 + * @since 1.11.0 + */ + @Override + public String authorize(String state) { + return UrlBuilder.fromBaseUrl(super.authorize(state)) + .queryParam("scope", "read_user+openid") + .build(); + } +} +``` + +## 使用`AuthMyGitlabRequest`实现Gitlab登录 + +```java +AuthRequest authRequest = new AuthMyGitlabRequest(AuthConfig.builder() + .clientId("63398e403231d4aa7e856cf5413620d536a876cb94e8d10ced0d3191b5d1d246") + .clientSecret("65b0eba68fff019e682e6755882a24dfdbf0a61be55de119cb8970320186c8eb") + .redirectUri("http://127.0.0.1:8443/oauth/callback/mygitlab") + .build()); +``` + +> 注:完整代码都在[JustAuth-demo](https://github.com/justauth/JustAuth-demo)Demo项目中,本节只讲解关键代码 + +## 效果 + +![gitlab私服验证授权](./_media/custom/gitlab3.png) +![gitlab私服登录完成](./_media/custom/gitlab4.png) + + + diff --git a/docs/customize-the-state-cache.md b/docs/customize-the-state-cache.md index 4f1fc91b18f280e4d377f6eb5f1ec1d2aa7e3773..af8e532ef0f76e4212b52d4030052e46f2c5028e 100644 --- a/docs/customize-the-state-cache.md +++ b/docs/customize-the-state-cache.md @@ -1,3 +1,123 @@ # 自定义state缓存 +> starter 内置了2种缓存实现,一种是上一节演示的默认实现,另一种是用户自定义的扩展实现。 +本节将会使用[JustAuth-demo](https://github.com/justauth/JustAuth-demo)Demo进行演示扩展Redis缓存的方式,当然了,你也可以自定义实现你自己的缓存。 -待补充 +## 添加 Redis 依赖 +```xml + + org.springframework.boot + spring-boot-starter-data-redis + +``` + +### 添加redis配置 + +```xml +# 集成redis实现自定义的state缓存 +spring.redis.database=0 +spring.redis.host=localhost +spring.redis.port=6379 +spring.redis.password=xxxx +``` +### 实现state缓存接口 + +```java +package me.zhyd.justauth; + +import me.zhyd.oauth.cache.AuthCacheConfig; +import me.zhyd.oauth.cache.AuthStateCache; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.concurrent.TimeUnit; + +/** + * 扩展Redis版的state缓存 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @date 2019/10/24 13:38 + * @since 1.8 + */ +@Component +public class AuthStateRedisCache implements AuthStateCache { + + @Autowired + private RedisTemplate redisTemplate; + + private ValueOperations valueOperations; + + @PostConstruct + public void init() { + valueOperations = redisTemplate.opsForValue(); + } + + /** + * 存入缓存,默认3分钟 + * + * @param key 缓存key + * @param value 缓存内容 + */ + @Override + public void cache(String key, String value) { + valueOperations.set(key, value, AuthCacheConfig.timeout, TimeUnit.MILLISECONDS); + } + + /** + * 存入缓存 + * + * @param key 缓存key + * @param value 缓存内容 + * @param timeout 指定缓存过期时间(毫秒) + */ + @Override + public void cache(String key, String value, long timeout) { + valueOperations.set(key, value, timeout, TimeUnit.MILLISECONDS); + } + + /** + * 获取缓存内容 + * + * @param key 缓存key + * @return 缓存内容 + */ + @Override + public String get(String key) { + return valueOperations.get(key); + } + + /** + * 是否存在key,如果对应key的value值已过期,也返回false + * + * @param key 缓存key + * @return true:存在key,并且value没过期;false:key不存在或者已过期 + */ + @Override + public boolean containsKey(String key) { + return redisTemplate.hasKey(key); + } +} +``` + +### 获取Request + +本节以Gitee为例 + +```java +// 1. 注入新添加的cache +@Autowired +private AuthStateRedisCache stateRedisCache; + +// 2. 创建request时传入stateRedisCache +AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder() + .clientId("clientId") + .clientSecret("clientSecret") + .redirectUri("redirectUri") + .build(), stateRedisCache);// 此处传入自定义实现的类 +``` + + +到此已经完成了通过redis**扩展实现state缓存**的功能。当然,只要你愿意,你可以使用**任何一种**缓存技术去实现这个功能,并不局限于redis。 diff --git a/docs/how-to-use.md b/docs/how-to-use.md index 23f4ab2af4483d08c5dc6d43479d164981603a69..519bc5efa0d231293b3805b79fc12805dd10175e 100644 --- a/docs/how-to-use.md +++ b/docs/how-to-use.md @@ -153,3 +153,16 @@ public Object revokeAuth(@PathVariable("source") String source, @PathVariable("t - [实现Github授权登录](http://t.cn/EJ0Fxqo) - [Spring Boot 快速集成第三方登录功能](http://t.cn/AiWWx5kH) : QQ、Github、微信、谷歌、微软、小米、企业微信 - [集成企业微信](https://mp.weixin.qq.com/s?__biz=MzA3NDk3OTIwMg==&mid=2450633170&idx=2&sn=456b70742a86948a193c691f3e47b72e&chksm=8892933fbfe51a29c1da386a2252d4bf91bfbd14e1ac0b99b783763a0d12e2e4b2d7c4369933&token=482455242&lang=zh_CN#rd) + +## 配套项目 + +**配套Demo**: +- [JustAuth-demo](https://github.com/justauth/JustAuth-demo):普通版springboot项目demo +- [jFinal版](https://github.com/xkcoding/jfinal-justauth-demo): Jfinal集成JustAuth的demo by [xkcoding](https://github.com/xkcoding) +- [ActFramework版](https://github.com/xkcoding/act-justauth-demo): ActFramework 集成 JustAuth 的 demo by [xkcoding](https://github.com/xkcoding) +- [Nutzboot版](https://github.com/EggsBlue/nutzboot-justauth-demo): NutzBoot集成JustAuth的demo by [蛋蛋](https://github.com/EggsBlue) +- [Blade版](https://github.com/justauth/blade-justauth-demo): Blade集成JustAuth的demo + +## springboot starter插件 +- [justauth-spring-boot-starter](https://github.com/xkcoding/justauth-spring-boot-starter): Spring Boot 集成 JustAuth 的最佳实践 by [xkcoding](https://github.com/xkcoding) +- [justauth-spring-boot-starter-demo](https://github.com/justauth/justauth-spring-boot-starter-demo): Spring Boot 使用 justauth-spring-boot-starter 快速集成 JustAuth by [xkcoding](https://github.com/xkcoding) diff --git a/docs/index.html b/docs/index.html index 615050559ba810908825327249f7ea5ec8222c2e..35f09f51606a57344bf2465518d47585bd97f54c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -74,5 +74,7 @@ } + + diff --git a/docs/oauth.md b/docs/oauth.md index 7c36f2c7a46dc0eadeec715d1717d829448035ac..897c15df952ee44750588c8e3858beef632195e8 100644 --- a/docs/oauth.md +++ b/docs/oauth.md @@ -5,7 +5,7 @@ - [The OAuth 2.0 Authorization Framework](https://tools.ietf.org/html/rfc6749) - [OAuth 2.0](https://oauth.net/2/) -## OAuth 2的授权流程 +## OAuth 2 的授权流程 ### 参与的角色 @@ -38,6 +38,8 @@ 上面的流程图取自[The OAuth 2.0 Authorization Framework#1.2](https://tools.ietf.org/html/rfc6749#section-1.2) +**流程解析** + - (A) 用户打开**客户端**以后,**客户端**要求**用户**给予授权。 - (B) **用户**同意给予**客户端**授权。 - (C) **客户端**使用上一步获得的授权,向**认证服务器**申请令牌。 @@ -56,6 +58,25 @@ - Client Credentials - 适用于客户端调用主服务API型应用(比如百度API Store) -在`JustAuth`中是使用的`Authorization Code`授权方式,下面将主要讲解`Authorization Code`的授权流程 +## 直白话 OAuth 2 流程 + +以上流程理解起来可能有些难度,这儿我们给出一个白话版的流程图 + +这儿引入三个角色: +- 用户A:可以理解成你自己 +- 网站B:可以理解成 Oschina +- 第三方C:可以理解成 Github + +需求:你想通过Github第三方登录Oschina -(未完待续) \ No newline at end of file +

+ sequenceDiagram + 用户A->>网站B: 1.我想登录你 + 网站B->>用户A: 2.我不认识你 + 用户A->>第三方C: 3.老铁我去过你那儿,咱俩认识,你帮我授权给网站B + 第三方C->>网站B: 4.用户A是我老铁,给你他的授权码 + 网站B->>第三方C: 5.这个授权码是你那儿的人吗?是的话给我他的令牌 + 第三方C->>网站B: 6.是我这儿的人,让他登录吧 + 网站B->>用户A: 7.得嘞,您走着 + 用户A->>网站B: 8.登录成功 +
diff --git a/docs/oauth/alipay.md b/docs/oauth/alipay.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/alipay.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/baidu.md b/docs/oauth/baidu.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/baidu.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/coding.md b/docs/oauth/coding.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/coding.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/csdn.md b/docs/oauth/csdn.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/csdn.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/dingtalk.md b/docs/oauth/dingtalk.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/dingtalk.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/douyin.md b/docs/oauth/douyin.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/douyin.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/eleme.md b/docs/oauth/eleme.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/eleme.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/facebook.md b/docs/oauth/facebook.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/facebook.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/gitee.md b/docs/oauth/gitee.md new file mode 100644 index 0000000000000000000000000000000000000000..35695b36d72deb13647b12ac8bfceee20e2ebca9 --- /dev/null +++ b/docs/oauth/gitee.md @@ -0,0 +1,144 @@ +## 1. 申请应用 + +### 1.1 注册账号 + +注册Gitee账号:[https://gitee.com](https://gitee.com)。如果已有则忽略该步骤,直接进入第二步。 + +### 1.2 创建第三方授权应用 + +进入第三方应用页面([https://gitee.com/oauth/applications](https://gitee.com/oauth/applications)) + +![Gitee第三方应用管理页面](../_media/oauth/gitee_02.png) + +创建应用 + +![创建OAuth应用](../_media/oauth/gitee_03.png) + +- **应用名称** 一般填写自己的网站名称即可 +- **应用描述** 一般填写自己的应用描述即可 +- **应用主页** 填写自己的网站首页地址 +- **应用回调地址** 重点,该地址为用户授权后需要跳转到的自己网站的地址,默认携带一个code参数 +- **权限** 根据页面提示操作,默认勾选第一个就行。 + +以上信息输入完成后,点击确定按钮创建应用。创建完成后,点击进入应用详情页,可以看到应用的密钥等信息 + +![应用详情](../_media/oauth/gitee_04.png) + +记录以下三个信息:`Client ID`、`Client Secret`和`应用回调地址`,后面我们会用到 + + +## 2. 集成JustAuth + +### 2.1 引入依赖 + +```xml + + me.zhyd.oauth + JustAuth + ${latest.version} + +``` + +`${latest.version}`表示当前最新的版本,可以在[这儿](https://github.com/justauth/JustAuth/releases)获取最新的版本信息。 + +### 2.2 创建Request + +```java +AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder() + .clientId("Client ID") + .clientSecret("Client Secret") + .redirectUri("应用回调地址") + .build()); +``` + +### 2.3 生成授权地址 + +我们可以直接使用以下方式生成第三方平台的授权链接: +```java +String authorizeUrl = authRequest.authorize(); +``` +这个链接我们可以直接后台重定向跳转,也可以返回到前端后,前端控制跳转。前端控制的好处就是,可以将第三方的授权页嵌入到iframe中,适配网站设计。 + + +### 2.4 以上完整代码如下 + +```java +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.request.AuthGiteeRequest; +import me.zhyd.oauth.request.AuthRequest; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + + +@RestController +@RequestMapping("/oauth") +public class RestAuthController { + + @RequestMapping("/render") + public void renderAuth(HttpServletResponse response) throws IOException { + AuthRequest authRequest = getAuthRequest(); + response.sendRedirect(authRequest.authorize()); + } + + @RequestMapping("/callback") + public Object login(String code) { + AuthRequest authRequest = getAuthRequest(); + return authRequest.login(code); + } + + private AuthRequest getAuthRequest() { + return new AuthGiteeRequest(AuthConfig.builder() + .clientId("Client ID") + .clientSecret("Client Secret") + .redirectUri("应用回调地址") + .build()); + } +} +``` +授权链接访问成功后会看到以下页面内容: +![授权登录](../_media/oauth/gitee_01.png) + +点击同意授权即可完成gitee的oauth登录。 + +## 3. 授权结果 + +注:数据以脱敏 + +```json +{ + "code": 2000, + "msg": null, + "data": { + "uuid": "xxx", + "username": "yadong.zhang", + "nickname": "yadong.zhang", + "avatar": "https://avatar.gitee.com/uploads/99/784199_yadong.zhang.png?1462325358", + "blog": "https://www.zhyd.me", + "company": null, + "location": null, + "email": null, + "remark": "程序员", + "gender": "UNKNOWN", + "source": "GITEE", + "token": { + "accessToken": "630740bxxxxxxxx4", + "expireIn": 86400, + "refreshToken": "1f2e0faedxxxxx537ec8", + "uid": null, + "openId": null, + "accessCode": null, + "unionId": null, + "scope": "user_info", + "tokenType": "bearer", + "idToken": null, + "macAlgorithm": null, + "macKey": null, + "code": null + } + } +} +``` \ No newline at end of file diff --git a/docs/oauth/github.md b/docs/oauth/github.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/github.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/gitlab.md b/docs/oauth/gitlab.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/gitlab.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/google.md b/docs/oauth/google.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/google.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/huawei.md b/docs/oauth/huawei.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/huawei.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/kujiale.md b/docs/oauth/kujiale.md new file mode 100644 index 0000000000000000000000000000000000000000..d8bc9a26cce5cb0a5af82a1de485b11ea8d97608 --- /dev/null +++ b/docs/oauth/kujiale.md @@ -0,0 +1,161 @@ +## 1. 申请应用 +企业可通过两种途径申请应用,一、直接通过线下商务合作,由商务人员线下提供开通的对应应用的秘钥信息。二、线上自助申请(此步骤申请的应用可以在应用审核上线后出现在商家后台的应用市场内)。此步骤主要介绍线上自助申请的流程,若以获取到对应的秘钥信息,则直接进入步骤2. +注意:目前酷家乐的开放平台只针对合作企业开放,暂时不对个人开发者开放。商务合作或酷家乐应用市场相关信息可访问[https://www.kujiale.com/](酷家乐首页)通过首页上的在线客服咨询。 +### 1.1 注册账号 +注册酷家乐账号:[https://www.kujiale.com/](https://www.kujiale.com/)。如果已有则忽略该步骤,直接进入第二步。(一般建议直接使用企业账号) +### 1.2 认证开发者 +进入酷家乐开放平台([https://open.kujiale.com/](https://open.kujiale.com/))点击右上角的管理中心进入第三方应用管理页面。 +![酷家乐第三方应用管理中心](../_media/oauth/kujiale_1.png) +首次进入左侧菜单栏的-服务入驻申请模块,系统会要求进行开发者实名认证。 +![实名认证](../_media/oauth/kujiale_2.png) +根据实名认证的流程,开发者需要依次提供:1.开发者信息,2.开发者企业信息,3.开发者企业人员构成信息,4.开发者企业经验背景信息 +![实名认证流程](../_media/oauth/kujiale_3.png) +在2-3个工作日内,酷家乐运营同学会对提交信息进行审核,审核通过后可进入应用申请。 +ps:如果有商务同学跟进的话可以联系商务同学提速,基本在1小时内能得到响应。 +### 1.2 创建第三方授权应用 +进入服务入驻申请模块([https://open.kujiale.com/open/management/service-application](服务入驻申请)),申请应用服务。 +![创建应用信息](../_media/oauth/kujiale_4.png) +选择服务类型以后提供相应的应用信息申请应用。 +![创建应用信息](../_media/oauth/kujiale_5.png) +oauth2.0基础信息 +- **应用名称** 一般填写自己的网站名称即可 +- **应用简介** 填写应用描述,预期实现的功能场景。 +- **应用回调地址** 重点,该地址为用户授权后需要跳转到的自己网站的地址,默认携带一个code参数 +- **OAuth申请** 根据申请应用的具体业务场景而定,获取账号基本信息为其他权限(可申请用户授权资源项)申请的基础,即获取账号基本信息必选。若只是依托于酷家乐账号登录则只选择获取账号基本信息即可。 +应用市场基础信息(当前只提供应用市场申请能力;应用市场的具体的营销规则,合作模式以及营销分成规则可通过在线客服与我们的商务同学取得联系。) +- **应用图标** 应用图标是应用展示在酷家乐应用市场内的图片表示,一般可以提供商家的品牌logo +- **购买通知地址** 当应用在应用市场内由其他商家付钱触发购买行为,酷家乐会通过此接口向第三方推送购买通知。 +联系信息 +- **咨询电话** 第三方需要为自己在酷家乐应用市场内的应用提供客户咨询能力。 +- **客服链接** 第三方需要为自己在酷家乐应用市场内的应用提供客服支持。 +- **开发者邮箱** 应用负责人联系邮箱,用于推送相关应用信息,例如:应用状态,应用服务情况,应用数据统计情况,以及相关的酷家乐接口迭代推送。 +- **开发者电话** 应用负责人联系人电话,帮助在系统层面出现问题时候可以快速联系到负责人。 +协议信息 +- **应用协议名称** 应用的使用者需要知晓并且同意的协议名称。 +- **应用协议内容** 应用的使用者需要知晓并且同意才可以使用购买该应用的协议内容。 + +保存并提交审核后,由运营在管理后台审核应用的相关信息以及应用申请相关权限的合理性。在审核通过后,可以在应用服务模块中查看到该应用,同时访问应用详情可以获取到此应用的秘钥信息(client_id:应用的唯一标识。client_secret:client_id对应的密钥,访问用户资源时用来验证应用的合法性。) + +## 2. 集成JustAuth + +### 2.1 引入依赖 + +```xml + + me.zhyd.oauth + JustAuth + ${latest.version} + +``` + +`${latest.version}`表示当前最新的版本,可以在[这儿](https://github.com/justauth/JustAuth/releases)获取最新的版本信息。 + +### 2.2 创建Request + +```java +AuthRequest authRequest = new AuthKujialeRequest(AuthConfig.builder() + .clientId("Client ID") + .clientSecret("Client Secret") + .redirectUri("应用回调地址") + .build()); +``` + +### 2.3 生成授权地址 + +我们可以直接使用以下方式生成第三方平台的授权链接: +```java +String authorizeUrl = authRequest.authorize(String state); +``` +这个授权链接,默认的是`get_user_info`scope,即只想用户申请用户基本信息的授权,如果想自定义`scope`可以使用如下方式,单独传入对应的`scope`即可 +```java +String authorizeUrl = authRequest.authorize(String state, String scopeStr); +``` +这个链接我们可以直接后台重定向跳转,也可以返回到前端后,前端控制跳转。前端控制的好处就是,可以将第三方的授权页嵌入到iframe中,适配网站设计。 + +参数说明: +- state:用于防范CSRF攻击,各第三方可以视自己网站的安全性而定(建议预先了解一下oauth2.0的技术以及state参数的意义) +- scopeStr:酷家乐授权选项scope,以逗号隔开,具体资源可以参考每个[https://open.kujiale.com/open/apps?app_id=2](酷家乐开放接口文档)内的授权scope内容。 + +### 2.4 以上完整代码如下 + +```java +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.request.AuthKujialeRequest; +import me.zhyd.oauth.request.AuthRequest; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + + +@RestController +@RequestMapping("/oauth") +public class RestAuthController { + + @RequestMapping("/render") + public void renderAuth(HttpServletResponse response) throws IOException { + AuthRequest authRequest = getAuthRequest(); + response.sendRedirect(authRequest.authorize("state")); + } + + @RequestMapping("/callback") + public Object login(String code, String state) { + AuthRequest authRequest = getAuthRequest(); + //此处如果对安全性有要求,请校验state参数 + return authRequest.login(code); + } + + private AuthRequest getAuthRequest() { + return new AuthKujialeRequest(AuthConfig.builder() + .clientId("Client ID") + .clientSecret("Client Secret") + .redirectUri("应用回调地址") + .build()); + } +} +``` +授权链接访问成功后会看到以下页面内容: +![授权登录](../_media/oauth/kujiale_6.png) + +点击同意授权即可完成酷家乐的oauth登录。 + +## 3. 授权结果 + +注:数据已脱敏 + +```json +{ + "code": 2000, + "msg": null, + "data": { + "uuid": "xxx", + "username": "yadong.zhang", + "nickname": "yadong.zhang", + "avatar": "https://qhyxpicoss.kujiale.com/avatars/2017/03/03/origin8762512743744383438.jpg", + "blog": null, + "company": null, + "location": null, + "email": null, + "remark": null, + "gender": null, + "source": "KUJIALE", + "token": { + "accessToken": "630740bxxxxxxxx4", + "expireIn": 86400, + "refreshToken": "1f2e0faedxxxxx537ec8", + "uid": null, + "openId": null, + "accessCode": null, + "unionId": null, + "scope": null, + "tokenType": null, + "idToken": null, + "macAlgorithm": null, + "macKey": null, + "code": null + } + } +} +``` \ No newline at end of file diff --git a/docs/oauth/linkedin.md b/docs/oauth/linkedin.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/linkedin.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/meituan.md b/docs/oauth/meituan.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/meituan.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/mi.md b/docs/oauth/mi.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/mi.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/microsoft.md b/docs/oauth/microsoft.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/microsoft.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/oschina.md b/docs/oauth/oschina.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/oschina.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/pinterest.md b/docs/oauth/pinterest.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/pinterest.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/qq.md b/docs/oauth/qq.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/qq.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/renren.md b/docs/oauth/renren.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/renren.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/stackoverflow.md b/docs/oauth/stackoverflow.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/stackoverflow.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/taobao.md b/docs/oauth/taobao.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/taobao.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/teambition.md b/docs/oauth/teambition.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/teambition.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/tencentCloud.md b/docs/oauth/tencentCloud.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/tencentCloud.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/toutiao.md b/docs/oauth/toutiao.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/toutiao.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/wechat.md b/docs/oauth/wechat.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/wechat.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/wechatEnterprise.md b/docs/oauth/wechatEnterprise.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/wechatEnterprise.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/oauth/weibo.md b/docs/oauth/weibo.md new file mode 100644 index 0000000000000000000000000000000000000000..00ff6e4b57c63ff0bf9041d762d4776acca02f47 --- /dev/null +++ b/docs/oauth/weibo.md @@ -0,0 +1,7 @@ +(敬请期待...) + +## 1. 申请应用 + +## 2. 集成JustAuth + +## 3. 授权结果 \ No newline at end of file diff --git a/docs/supporting.md b/docs/supporting.md index 47090afa5f7316fcc46f475fdb1fffc9372ec3a1..d0abedba7aa07dcf251fc911744027912633d967 100644 --- a/docs/supporting.md +++ b/docs/supporting.md @@ -1,5 +1,5 @@ **配套Demo**: -- [Springboot版](https://github.com/justauth/JustAuth-demo) +- [simple版](https://github.com/justauth/JustAuth-demo) - [jFinal版](https://github.com/xkcoding/jfinal-justauth-demo): Jfinal集成JustAuth的demo by [xkcoding](https://github.com/xkcoding) - [ActFramework版](https://github.com/xkcoding/act-justauth-demo): ActFramework 集成 JustAuth 的 demo by [xkcoding](https://github.com/xkcoding) - [Nutzboot版](https://github.com/EggsBlue/nutzboot-justauth-demo): NutzBoot集成JustAuth的demo by [蛋蛋](https://github.com/EggsBlue) diff --git a/src/main/java/me/zhyd/oauth/config/AuthSource.java b/src/main/java/me/zhyd/oauth/config/AuthSource.java index 4bfe07066e34d89435e1a1886f6c24f9809f6452..f42949a5123ee2a2e0900fc729ee5216b577ff04 100644 --- a/src/main/java/me/zhyd/oauth/config/AuthSource.java +++ b/src/main/java/me/zhyd/oauth/config/AuthSource.java @@ -5,12 +5,12 @@ import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.model.AuthCallback; /** - * OAuth平台的API管理类的统一接口,提供以下接口: - * 1) {@link AuthSource#authorize()}: 获取授权api. 必须实现 - * 2) {@link AuthSource#accessToken()}: 获取授权api. 必须实现 - * 3) {@link AuthSource#userInfo()}: 获取授权api. 必须实现 - * 4) {@link AuthSource#revoke()}: 获取授权api. 非必须实现接口(部分平台不支持) - * 5) {@link AuthSource#refresh()} ()}: 获取授权api. 非必须实现接口(部分平台不支持) + * OAuth平台的API地址的统一接口,提供以下方法: + * 1) {@link AuthSource#authorize()}: 获取授权url. 必须实现 + * 2) {@link AuthSource#accessToken()}: 获取accessToken的url. 必须实现 + * 3) {@link AuthSource#userInfo()}: 获取用户信息的url. 必须实现 + * 4) {@link AuthSource#revoke()}: 获取取消授权的url. 非必须实现接口(部分平台不支持) + * 5) {@link AuthSource#refresh()}: 获取刷新授权的url. 非必须实现接口(部分平台不支持) *

* 注: * ①、如需通过JustAuth扩展实现第三方授权,请参考{@link AuthDefaultSource}自行创建对应的枚举类并实现{@link AuthSource}接口