diff --git a/docs/_media/oauth/wechat_01.png b/docs/_media/oauth/wechat_01.png new file mode 100644 index 0000000000000000000000000000000000000000..fa3fdb2aa41559f70472ad7c19e5626a468ad9c1 Binary files /dev/null and b/docs/_media/oauth/wechat_01.png differ diff --git a/docs/_media/oauth/wechat_02.png b/docs/_media/oauth/wechat_02.png new file mode 100644 index 0000000000000000000000000000000000000000..d434c08054543802b0b1e94ffa958f39b57d5fc2 Binary files /dev/null and b/docs/_media/oauth/wechat_02.png differ diff --git a/docs/_media/oauth/wechat_03.png b/docs/_media/oauth/wechat_03.png new file mode 100644 index 0000000000000000000000000000000000000000..10fd90cf950939c941123c1e03ad02827cdbe8ff Binary files /dev/null and b/docs/_media/oauth/wechat_03.png differ diff --git a/docs/_media/oauth/wechat_04.png b/docs/_media/oauth/wechat_04.png new file mode 100644 index 0000000000000000000000000000000000000000..91afa0708df422ed153b1b6040c0f15dab2e30dc Binary files /dev/null and b/docs/_media/oauth/wechat_04.png differ diff --git a/docs/_media/oauth/wechat_05.png b/docs/_media/oauth/wechat_05.png new file mode 100644 index 0000000000000000000000000000000000000000..7d4066f0fc09b2600a86850ae1aa6dc5f6ef278a Binary files /dev/null and b/docs/_media/oauth/wechat_05.png differ diff --git a/docs/_media/oauth/wechat_06.png b/docs/_media/oauth/wechat_06.png new file mode 100644 index 0000000000000000000000000000000000000000..8d278390fa940c488271b05e902181a499437d0c Binary files /dev/null and b/docs/_media/oauth/wechat_06.png differ diff --git a/docs/_media/oauth/wechat_07.png b/docs/_media/oauth/wechat_07.png new file mode 100644 index 0000000000000000000000000000000000000000..8a86a6df44b2643779f542c3edc5bc6341c6af5c Binary files /dev/null and b/docs/_media/oauth/wechat_07.png differ diff --git a/docs/_sidebar.md b/docs/_sidebar.md index bec5bab07e99e1b5daea9549a5888b840728dfcc..2baa3d064d694f937423a81eeb5e9c8a8eb020d4 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -7,9 +7,9 @@ - [Q&A](Q&A.md) - 集成第三方 - [√ Gitee登录](oauth/gitee.md) - - [Github登录](oauth/github.md) + - [√ Github登录](oauth/github.md) - [qq登录](oauth/qq.md) - - [微信登录](oauth/wechat.md) + - [√ 微信登录](oauth/wechat.md) - [微博登录](oauth/weibo.md) - [支付宝登录](oauth/alipay.md) - [百度登录](oauth/baidu.md) diff --git a/docs/oauth/gitee.md b/docs/oauth/gitee.md index 35695b36d72deb13647b12ac8bfceee20e2ebca9..c2f7e849bf3b918698990206ba5ee76580892b55 100644 --- a/docs/oauth/gitee.md +++ b/docs/oauth/gitee.md @@ -66,6 +66,7 @@ String authorizeUrl = authRequest.authorize(); import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.request.AuthGiteeRequest; import me.zhyd.oauth.request.AuthRequest; +import me.zhyd.oauth.utils.AuthStateUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -81,7 +82,7 @@ public class RestAuthController { @RequestMapping("/render") public void renderAuth(HttpServletResponse response) throws IOException { AuthRequest authRequest = getAuthRequest(); - response.sendRedirect(authRequest.authorize()); + response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); } @RequestMapping("/callback") diff --git a/docs/oauth/github.md b/docs/oauth/github.md index be420c376c724d1e099abbaa8c438e8184ae9bd7..9a52933c70071e98a6ce2627e16cfec1c8375634 100644 --- a/docs/oauth/github.md +++ b/docs/oauth/github.md @@ -63,6 +63,7 @@ String authorizeUrl = authRequest.authorize(); import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.request.AuthGithubRequest; import me.zhyd.oauth.request.AuthRequest; +import me.zhyd.oauth.utils.AuthStateUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -78,7 +79,7 @@ public class RestAuthController { @RequestMapping("/render") public void renderAuth(HttpServletResponse response) throws IOException { AuthRequest authRequest = getAuthRequest(); - response.sendRedirect(authRequest.authorize()); + response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); } @RequestMapping("/callback") diff --git a/docs/oauth/wechat.md b/docs/oauth/wechat.md index 00ff6e4b57c63ff0bf9041d762d4776acca02f47..c74daa88c608c9b3856f31511d1773b0f8077939 100644 --- a/docs/oauth/wechat.md +++ b/docs/oauth/wechat.md @@ -1,7 +1,164 @@ -(敬请期待...) - ## 1. 申请应用 +### 1.注册微信开放平台账号 +[https://open.weixin.qq.com/](https://open.weixin.qq.com/)。如果已有则忽略该步骤,直接进入第二步。 +### 2. 申请开发者资质认证 + +这儿需要重点说明的是, 微信开放平台创建应用,需要申请**开发者资质认证** + +![file](../_media/oauth/wechat_01.png) + +如上图:账号中心->开发者资质认证 + +但是微信**不支持**个人开发者认证,必须要企业信息才能提交认证。 + +对于个人开发者来说,我们提供三种解决方案: +- 用自己公司的信息做认证(请确保合法合规) +- 找朋友帮忙认证 +- 去TB或者其他平台花钱找人认证,这种方式一般支持:短期租、长期认证 + +### 3.创建第三方应用 + +认证通过后切换到“网站应用”标签页,点击“创建网站应用”按钮 +![file](../_media/oauth/wechat_02.png) + + +![file](../_media/oauth/wechat_03.png) + +创建完成后,就能在“网站应用”列表中看到相关信息 + +![file](../_media/oauth/wechat_04.png) + +点击“查看”进入应用详情,可以看到 “OAuth三大件”:`Client ID`、`Client Secret`和`Callback URL`。 + +![file](../_media/oauth/wechat_05.png) + +**注意:** +- “授权回调域”填写对应域名即可。比如我想给我的博客 [https://www.zhyd.me](https://www.zhyd.me) 增加微信第三方登录,那么我在配回调地址时,就只需要填写 `www.zhyd.me`即可,实际我们自己开发程序时, 可以随便配置具体的回调地址,只要确保是在 `www.zhyd.me` 下的地址就行。本例中, 我在程序中配置的回调地址为:`https://www.zhyd.me/oauth/callback/wechat` +- 重要的事情说三遍:**一定要确保应用详情页的“接口信息”中的“微信登录”接口的状态为“已获得”!** **一定要确保应用详情页的“接口信息”中的“微信登录”接口的状态为“已获得”!** **一定要确保应用详情页的“接口信息”中的“微信登录”接口的状态为“已获得”!**否则一定会遇到这个问题:[“Scope参数错误或者Scope没有权限”是怎么回事?](../Q&A.md?id=_7scope参数错误或者scope没有权限是怎么回事?) + + ## 2. 集成JustAuth -## 3. 授权结果 \ No newline at end of file + +### 2.1 引入依赖 + +```xml + + me.zhyd.oauth + JustAuth + ${latest.version} + +``` + +`${latest.version}`表示当前最新的版本,可以在[这儿](https://github.com/justauth/JustAuth/releases)获取最新的版本信息。 + +### 2.2 创建Request + +```java +AuthRequest authRequest = new AuthWeChatRequest(AuthConfig.builder() + .clientId("Client ID") + .clientSecret("Client Secret") + .redirectUri("https://www.zhyd.me/oauth/callback/wechat") + .build()); +``` + +### 2.3 生成授权地址 + +我们可以直接使用以下方式生成第三方平台的授权链接: +```java +String authorizeUrl = authRequest.authorize(); +``` +这个链接我们可以直接后台重定向跳转,也可以返回到前端后,前端控制跳转。前端控制的好处就是,可以将第三方的授权页嵌入到iframe中,适配网站设计。 + + +### 2.4 以上完整代码如下 + +```java +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.request.AuthWeChatRequest; +import me.zhyd.oauth.request.AuthRequest; +import me.zhyd.oauth.utils.AuthStateUtils; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + + +@RestController +@RequestMapping("/oauth") +public class RestAuthController { + + @RequestMapping("/render") + public void renderAuth(HttpServletResponse response) throws IOException { + AuthRequest authRequest = getAuthRequest(); + response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); + } + + @RequestMapping("/callback") + public Object login(String code) { + AuthRequest authRequest = getAuthRequest(); + return authRequest.login(code); + } + + private AuthRequest getAuthRequest() { + return new AuthWeChatRequest(AuthConfig.builder() + .clientId("Client ID") + .clientSecret("Client Secret") + .redirectUri("https://www.zhyd.me/oauth/callback/wechat") + .build()); + } +} +``` +授权链接访问成功后会看到以下页面内容: +![file](../_media/oauth/wechat_06.png) + +使用手机扫码后,将会进入确认授权页面 + +![file](../_media/oauth/wechat_07.png) + +点击同意授权即可完成微信的oauth登录。 + +## 3. 授权结果 + +```json +{ + "code": 2000, + "msg": null, + "data": { + "uuid": "xxxxxxx", + "username": "RD", + "nickname": "RD", + "avatar": "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83ereNicKErbtBVnraCnzjGia2ZWPSkI9Ok4ScrmkdiacgPN6D5qeYBf3iba2lXknE7YaMMHHRrMW6Op8eQ/132", + "blog": null, + "company": null, + "location": "中国-北京-", + "email": null, + "remark": null, + "gender": "MALE", + "source": "WECHAT", + "token": { + "accessToken": "28_Bzvt4UxxxxFElOxxxxgTqIJmSE5LxB2Az7stFqYVnFoIhL7RbevA0Urc", + "expireIn": 7200, + "refreshToken": "28_vtarjUxxxxfKh0Axxxxe_vy7E62hAYkMJ5M_JT4gMXM7r1kT_SeKAQz8Ojow", + "uid": null, + "openId": "xxxxxxx", + "accessCode": null, + "unionId": "xxxxxxx", + "scope": null, + "tokenType": null, + "idToken": null, + "macAlgorithm": null, + "macKey": null, + "code": null, + "oauthToken": null, + "oauthTokenSecret": null, + "userId": null, + "screenName": null, + "oauthCallbackConfirmed": null + } + } +} +``` \ No newline at end of file