提交 264294ca 编写于 作者: 智布道's avatar 智布道 👁

📝 添加微博授权登录的帮助文档

上级 19ec5650
......@@ -10,14 +10,14 @@
- [√ Github登录](oauth/github.md)
- [√ qq登录](oauth/qq.md)
- [√ 微信登录](oauth/wechat.md)
- [微博登录](oauth/weibo.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)
- [腾讯云登录](oauth/tencentCloud.md)
- [Google登录](oauth/google.md)
- [Facebook登录](oauth/facebook.md)
- [抖音登录](oauth/douyin.md)
......
......@@ -17,11 +17,13 @@
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
<link rel="stylesheet" href="//unpkg.com/gitalk/dist/gitalk.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.css">
</head>
<body>
<div data-app id="app">Please wait...</div>
<script>
var num = 0;
window.$docsify = {
el: '#app',
name: 'JustAuth',
......@@ -63,18 +65,31 @@
return html
+ '\n----\n'
+ 'Last modified {docsify-updated} '
+ '最后更新时间: {docsify-updated} '
+ editHtml
})
}
]
],
markdown: {
renderer: {
code: function(code, lang) {
if (lang === "mermaid") {
return (
'<div class="mermaid">' + mermaid.render('mermaid-svg-' + num++, code) + "</div>"
);
}
return this.origin.code.apply(this, arguments);
}
}
}
}
</script>
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
<script src="//unpkg.com/docsify/lib/plugins/search.min.js"></script>
<script src="//unpkg.com/docsify-copy-code"></script>
<script src="https://cdn.jsdelivr.net/npm/mermaid@7.1.0/dist/mermaid.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>mermaid.initialize({startOnLoad:true});</script>
<script src="//unpkg.com/prismjs/components/prism-java.min.js"></script>
<script src="//unpkg.com/docsify/lib/plugins/gitalk.min.js"></script>
<script src="//unpkg.com/gitalk/dist/gitalk.min.js"></script>
......
(敬请期待...)
## 1. 申请应用
### 1.注册微博开放平台账号
https://open.weibo.com/apps。如果已有则忽略该步骤,直接进入第二步。
### 2.创建应用
通过顶部菜单栏的【微连接-网站接入】或者直接点击【[网站接入](https://open.weibo.com/connect)】进入网站接入界面
![file](../_media/oauth/weibo_01.png)
点击【立即接入】按钮进入创建应用页面,填入**应用名称****应用分类**选择默认的“网页应用”即可
![file](../_media/oauth/weibo_02.png)
创建完成后会自动跳转到应用信息页面,如下图,根据提示完善应用即可
![file](../_media/oauth/weibo_03.png)
注:全部填写完成后,需要提交审核。
提交审核完成后,将Oauth需要用到的数据copy下来(下面截图取自我已经审核通过的应用):
![file](../_media/oauth/weibo_04.png)
![file](../_media/oauth/weibo_05.png)
微博平台的OAuth支持revoke操作,所以会有一个【取消授权回调页】配置。
## 2. 集成JustAuth
### 2.1 引入依赖
```xml
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>${latest.version}</version>
</dependency>
```
`${latest.version}`表示当前最新的版本,可以在[这儿](https://github.com/justauth/JustAuth/releases)获取最新的版本信息。
### 2.2 创建Request
```java
AuthRequest authRequest = new AuthWeiboRequest(AuthConfig.builder()
.clientId("App Key")
.clientSecret("App Secret")
.redirectUri("授权回调页")
.build());
```
### 2.3 生成授权地址
我们可以直接使用以下方式生成第三方平台的授权链接:
```java
String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
```
这个链接我们可以直接后台重定向跳转,也可以返回到前端后,前端控制跳转。前端控制的好处就是,可以将第三方的授权页嵌入到iframe中,适配网站设计。
### 2.4 以上完整代码如下
```java
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthWeiboRequest;
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);
}
@RequestMapping("/revoke/{token}")
public Object revokeAuth(@PathVariable("token") String token) throws IOException {
AuthRequest authRequest = getAuthRequest();
return authRequest.revoke(AuthToken.builder().accessToken(token).build());
}
private AuthRequest getAuthRequest() {
return new AuthWeiboRequest(AuthConfig.builder()
.clientId("App Key")
.clientSecret("App Secret")
.redirectUri("授权回调页")
.build());
}
}
```
授权链接访问成功后会看到以下页面内容:
![授权登录](../_media/oauth/weibo_06.png)
网页登录或者手机扫码登录并授权第三方应用的登录请求后,将会获取到用户的信息
## 3. 授权结果
```json
{
"code": 2000,
"msg": null,
"data": {
"uuid": "xxxxxxx",
"username": "七彩狼丿",
"nickname": "七彩狼丿",
"avatar": "https://tva3.sinaimg.cn/crop.0.0.1424.1424.50/649b9a6fjw8ezwz4inarqj213k13kwjh.jpg?KID=imgbed,tva&Expires=1577025808&ssig=Q7m4jDuwUt",
"blog": "https://www.zhyd.me",
"company": null,
"location": "北京 朝阳区",
"email": null,
"remark": "长大了,不得不接受一些事实",
"gender": "MALE",
"source": "WEIBO",
"token": {
"accessToken": "2.xxxxxCNUC",
"expireIn": 157679999,
"refreshToken": null,
"uid": "xxxxxxx",
"openId": "xxxxxxx",
"accessCode": null,
"unionId": null,
"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
......@@ -5,9 +5,12 @@
- `checkState`方法从`AuthDefaultRequest`中提出到`AuthChecker`
- `AuthResponseStatus`枚举类中增加`ILLEGAL_STATUS``REQUIRED_REFRESH_TOKEN`两个枚举值
- `AuthSource`接口中增加`getName`方法,用来对外提供实际`source`的字符串值
- 增加微信、QQ、支付宝授权登录的帮助文档
- `AuthWeiboRequest`微博授权登录中实现`revoke`方法,支持手动回收授权
- 增加微信、QQ、支付宝、微博授权登录的帮助文档
- [帮助文档](https://docs.justauth.whnb.wang)中增加自定义的404页面
- [帮助文档](https://docs.justauth.whnb.wang)中增加Gittalk插件
- [帮助文档](https://docs.justauth.whnb.wang)中增加Java代码高亮的插件
- [帮助文档](https://docs.justauth.whnb.wang)中修复[腾讯云登录]链接错误的问题
## v1.13.1
### 2019/11/12
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册