Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ad1c1b

Browse files
committed
⬆️ 使用 justauth-spring-boot-starter 实现第三方登录,同时缓存使用自定义 redis 缓存 state
1 parent ea383ee commit 8ad1c1b

File tree

1 file changed

+86
-154
lines changed

1 file changed

+86
-154
lines changed

‎spring-boot-demo-social/README.md

Lines changed: 86 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
# spring-boot-demo-social
22

3-
> 此 demo 主要演示 Spring Boot 项目如何使用 **[史上最全的第三方登录工具 - JustAuth](https://github.com/zhangyd-c/JustAuth)** 实现第三方登录。
3+
> 此 demo 主要演示 Spring Boot 项目如何使用 **[史上最全的第三方登录工具 - JustAuth](https://github.com/zhangyd-c/JustAuth)** 实现第三方登录,包括QQ登录、GitHub登录、微信登录、谷歌登录、微软登录、小米登录、企业微信登录
44
>
5-
> 如果技术选型是 `JFinal` 的,请查看此 [**`demo`**](https://github.com/xkcoding/jfinal-justauth-demo)
6-
>
7-
> https://github.com/xkcoding/jfinal-justauth-demo
8-
>
9-
> 如果技术选型是 `ActFramework` 的,请查看此 [**`demo`**](https://github.com/xkcoding/act-justauth-demo)
10-
>
11-
> https://github.com/xkcoding/act-justauth-demo
5+
> 通过 [justauth-spring-boot-starter](https://search.maven.org/artifact/com.xkcoding/justauth-spring-boot-starter) 快速集成,好嗨哟~
126
>
137
> JustAuth,如你所见,它仅仅是一个**第三方授权登录****工具类库**,它可以让我们脱离繁琐的第三方登录SDK,让登录变得**So easy!**
148
>
159
> 1. ****:已集成十多家第三方平台(国内外常用的基本都已包含),后续依然还有扩展计划!
16-
> 2. ****:API就是奔着最简单去设计的(见后面[`快速开始`](https://github.com/zhangyd-c/JustAuth#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B)),尽量让您用起来没有障碍感!
17-
>
18-
> PS: 本人十分幸运的参与到了这个SDK的开发,主要开发了**QQ登录、微信登录、小米登录、微软登录、谷歌登录****`5`** 个第三方登录,以及一些BUG的修复工作。再次感谢 [@母狼](https://github.com/zhangyd-c) 开源这个又好用又全面的第三方登录SDK。
10+
>2. ****:API就是奔着最简单去设计的(见后面[`快速开始`](https://github.com/zhangyd-c/JustAuth#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B)),尽量让您用起来没有障碍感!
11+
>
12+
>PS: 本人十分幸运的参与到了这个SDK的开发,主要开发了**QQ登录、微信登录、小米登录、微软登录、谷歌登录****`5`** 个第三方登录,以及一些BUG的修复工作。再次感谢 [@母狼](https://github.com/zhangyd-c) 开源这个又好用又全面的第三方登录SDK。
13+
14+
如果技术选型是 `JFinal` 的,请查看此 [**`demo`**](https://github.com/xkcoding/jfinal-justauth-demo)
15+
16+
https://github.com/xkcoding/jfinal-justauth-demo
17+
18+
如果技术选型是 `ActFramework` 的,请查看此 [**`demo`**](https://github.com/xkcoding/act-justauth-demo)
19+
20+
https://github.com/xkcoding/act-justauth-demo
1921

2022
## 1. 环境准备
2123

@@ -229,6 +231,10 @@ $ nginx -s reload
229231

230232
![image-20190617151624603](assets/image-20190617151624603.png)
231233

234+
#### 1.5.7. 企业微信平台申请
235+
236+
> 参考:https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html
237+
232238
## 2. 主要代码
233239

234240
### 2.1. pom.xml
@@ -256,7 +262,7 @@ $ nginx -s reload
256262
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
257263
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
258264
<java.version>1.8</java.version>
259-
<spring.social.version>1.1.6.RELEASE</spring.social.version>
265+
<justauth-spring-boot.version>1.0.0</justauth-spring-boot.version>
260266
</properties>
261267

262268
<dependencies>
@@ -271,11 +277,22 @@ $ nginx -s reload
271277
<scope>test</scope>
272278
</dependency>
273279

280+
<dependency>
281+
<groupId>org.springframework.boot</groupId>
282+
<artifactId>spring-boot-starter-data-redis</artifactId>
283+
</dependency>
284+
285+
<!-- 对象池,使用redis时必须引入 -->
286+
<dependency>
287+
<groupId>org.apache.commons</groupId>
288+
<artifactId>commons-pool2</artifactId>
289+
</dependency>
290+
274291
<!-- oauth工具类 -->
275292
<dependency>
276-
<groupId>me.zhyd.oauth</groupId>
277-
<artifactId>JustAuth</artifactId>
278-
<version>1.9.5</version>
293+
<groupId>com.xkcoding</groupId>
294+
<artifactId>justauth-spring-boot-starter</artifactId>
295+
<version>${justauth-spring-boot.version}</version>
279296
</dependency>
280297

281298
<dependency>
@@ -316,6 +333,27 @@ server:
316333
servlet:
317334
context-path: /demo
318335

336+
spring:
337+
redis:
338+
host: localhost
339+
# 连接超时时间(记得添加单位,Duration)
340+
timeout: 10000ms
341+
# Redis默认情况下有16个分片,这里配置具体使用的分片
342+
# database: 0
343+
lettuce:
344+
pool:
345+
# 连接池最大连接数(使用负值表示没有限制) 默认 8
346+
max-active: 8
347+
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
348+
max-wait: -1ms
349+
# 连接池中的最大空闲连接 默认 8
350+
max-idle: 8
351+
# 连接池中的最小空闲连接 默认 0
352+
min-idle: 0
353+
cache:
354+
# 一般来说是不用配置的,Spring Cache 会根据依赖的包自行装配
355+
type: redis
356+
319357
oauth:
320358
qq:
321359
client-id: 1015*****
@@ -341,85 +379,16 @@ oauth:
341379
client-id: 2882303**************
342380
client-secret: nFeTt89Yn**************
343381
redirect-uri: http://oauth.xkcoding.com/demo/oauth/mi/callback
382+
wechat_enterprise:
383+
client-id: ww58**********6fbc
384+
client-secret: 8G6PCr0****************************yzaPc78
385+
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_enterprise/callback
386+
agent-id: 10*******02
344387
```
345388
346-
### 2.3. OAuthProperties.java
389+
### 2.3. OauthController.java
347390
348391
```java
349-
/**
350-
* <p>
351-
* 第三方登录配置
352-
* </p>
353-
*
354-
* @package: com.xkcoding.oauth.config.props
355-
* @description: 第三方登录配置
356-
* @author: yangkai.shen
357-
* @date: Created in 2019年05月17日 15:33
358-
* @copyright: Copyright (c) 2019
359-
* @version: V1.0
360-
* @modified: yangkai.shen
361-
*/
362-
@Data
363-
@Component
364-
@ConfigurationProperties(prefix = "oauth")
365-
public class OAuthProperties {
366-
/**
367-
* QQ 配置
368-
*/
369-
private AuthConfig qq;
370-
371-
/**
372-
* github 配置
373-
*/
374-
private AuthConfig github;
375-
376-
/**
377-
* 微信 配置
378-
*/
379-
private AuthConfig wechat;
380-
381-
/**
382-
* Google 配置
383-
*/
384-
private AuthConfig google;
385-
386-
/**
387-
* Microsoft 配置
388-
*/
389-
private AuthConfig microsoft;
390-
391-
/**
392-
* Mi 配置
393-
*/
394-
private AuthConfig mi;
395-
}
396-
```
397-
398-
### 2.4. OauthController.java
399-
400-
```java
401-
package com.xkcoding.social.controller;
402-
403-
import cn.hutool.core.lang.Dict;
404-
import cn.hutool.json.JSONUtil;
405-
import com.xkcoding.social.props.OAuthProperties;
406-
import lombok.RequiredArgsConstructor;
407-
import lombok.extern.slf4j.Slf4j;
408-
import me.zhyd.oauth.config.AuthConfig;
409-
import me.zhyd.oauth.config.AuthSource;
410-
import me.zhyd.oauth.model.AuthCallback;
411-
import me.zhyd.oauth.model.AuthResponse;
412-
import me.zhyd.oauth.request.*;
413-
import me.zhyd.oauth.utils.AuthStateUtils;
414-
import org.springframework.beans.factory.annotation.Autowired;
415-
import org.springframework.web.bind.annotation.GetMapping;
416-
import org.springframework.web.bind.annotation.PathVariable;
417-
import org.springframework.web.bind.annotation.RequestMapping;
418-
import org.springframework.web.bind.annotation.RestController;
419-
420-
import javax.servlet.http.HttpServletResponse;
421-
import java.io.IOException;
422-
423392
/**
424393
* <p>
425394
* 第三方登录 Controller
@@ -438,14 +407,15 @@ import java.io.IOException;
438407
@RequestMapping("/oauth")
439408
@RequiredArgsConstructor(onConstructor_ = @Autowired)
440409
public class OauthController {
441-
private final OAuthProperties properties;
410+
private final AuthRequestFactory factory;
442411

443412
/**
444413
* 登录类型
445414
*/
446415
@GetMapping
447-
public Dict loginType() {
448-
return Dict.create().set("QQ登录", "http://oauth.xkcoding.com/demo/oauth/login/qq").set("GitHub登录", "http://oauth.xkcoding.com/demo/oauth/login/github").set("微信登录", "http://oauth.xkcoding.com/demo/oauth/login/wechat").set("Google登录", "http://oauth.xkcoding.com/demo/oauth/login/google").set("Microsoft 登录", "http://oauth.xkcoding.com/demo/oauth/login/microsoft").set("小米登录", "http://oauth.xkcoding.com/demo/oauth/login/mi");
416+
public Map<String, String> loginType() {
417+
List<String> oauthList = factory.oauthList();
418+
return oauthList.stream().collect(Collectors.toMap(oauth -> oauth.toLowerCase() + "登录", oauth -> "http://oauth.xkcoding.com/demo/oauth/login/" + oauth.toLowerCase()));
449419
}
450420

451421
/**
@@ -457,8 +427,8 @@ public class OauthController {
457427
*/
458428
@RequestMapping("/login/{oauthType}")
459429
public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException {
460-
AuthRequest authRequest = getAuthRequest(oauthType);
461-
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
430+
AuthRequest authRequest = factory.get(getAuthSource(oauthType));
431+
response.sendRedirect(authRequest.authorize(oauthType + "::" + AuthStateUtils.createState()));
462432
}
463433

464434
/**
@@ -470,65 +440,26 @@ public class OauthController {
470440
*/
471441
@RequestMapping("/{oauthType}/callback")
472442
public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) {
473-
AuthRequest authRequest = getAuthRequest(oauthType);
443+
AuthRequest authRequest = factory.get(getAuthSource(oauthType));
474444
AuthResponse response = authRequest.login(callback);
475445
log.info("【response】= {}", JSONUtil.toJsonStr(response));
476446
return response;
477447
}
478448

479-
private AuthRequest getAuthRequest(String oauthType) {
480-
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase());
481-
switch (authSource) {
482-
case QQ:
483-
return getQqAuthRequest();
484-
case GITHUB:
485-
return getGithubAuthRequest();
486-
case WECHAT:
487-
return getWechatAuthRequest();
488-
case GOOGLE:
489-
return getGoogleAuthRequest();
490-
case MICROSOFT:
491-
return getMicrosoftAuthRequest();
492-
case MI:
493-
return getMiAuthRequest();
494-
default:
495-
throw new RuntimeException("暂不支持的第三方登录");
449+
private AuthSource getAuthSource(String type) {
450+
if (StrUtil.isNotBlank(type)) {
451+
return AuthSource.valueOf(type.toUpperCase());
452+
} else {
453+
throw new RuntimeException("不支持的类型");
496454
}
497455
}
498-
499-
private AuthRequest getQqAuthRequest() {
500-
AuthConfig authConfig = properties.getQq();
501-
return new AuthQqRequest(authConfig);
502-
}
503-
504-
private AuthRequest getGithubAuthRequest() {
505-
AuthConfig authConfig = properties.getGithub();
506-
return new AuthGithubRequest(authConfig);
507-
}
508-
509-
private AuthRequest getWechatAuthRequest() {
510-
AuthConfig authConfig = properties.getWechat();
511-
return new AuthWeChatRequest(authConfig);
512-
}
513-
514-
private AuthRequest getGoogleAuthRequest() {
515-
AuthConfig authConfig = properties.getGoogle();
516-
return new AuthGoogleRequest(authConfig);
517-
}
518-
519-
private AuthRequest getMicrosoftAuthRequest() {
520-
AuthConfig authConfig = properties.getMicrosoft();
521-
return new AuthMicrosoftRequest(authConfig);
522-
}
523-
524-
private AuthRequest getMiAuthRequest() {
525-
AuthConfig authConfig = properties.getMi();
526-
return new AuthMiRequest(authConfig);
527-
}
528456
}
529-
530457
```
531458

459+
### 2.4. 如果想要自定义 state 缓存
460+
461+
请看👉[这里](https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social/src/main/java/com/xkcoding/social/config/justauth)
462+
532463
## 3. 运行方式
533464

534465
打开浏览器,输入 http://oauth.xkcoding.com/demo/oauth ,点击各个登录方式自行测试。
@@ -539,16 +470,17 @@ public class OauthController {
539470

540471
## 参考
541472

542-
1. JustAuth 项目地址:https://github.com/zhangyd-c/JustAuth
543-
2. frp内网穿透项目地址:https://github.com/fatedier/frp
544-
3. frp内网穿透官方中文文档:https://github.com/fatedier/frp/blob/master/README_zh.md
545-
4. Frp实现内网穿透:https://zhuanlan.zhihu.com/p/45445979
546-
5. QQ互联文档:http://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0
547-
6. 微信开放平台文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
548-
7. GitHub第三方登录文档:https://developer.github.com/apps/building-oauth-apps/
549-
8. 谷歌Oauth2文档:https://developers.google.com/identity/protocols/OpenIDConnect
550-
9. 微软Oauth2文档:https://docs.microsoft.com/zh-cn/graph/auth-v2-user
551-
10. 小米开放平台账号服务文档:https://dev.mi.com/console/doc/detail?pId=707
473+
1. JustAuth 项目地址:https://github.com/justauth/JustAuth
474+
2. justauth-spring-boot-starter 地址:https://github.com/justauth/justauth-spring-boot-starter
475+
3. frp内网穿透项目地址:https://github.com/fatedier/frp
476+
4. frp内网穿透官方中文文档:https://github.com/fatedier/frp/blob/master/README_zh.md
477+
5. Frp实现内网穿透:https://zhuanlan.zhihu.com/p/45445979
478+
6. QQ互联文档:http://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0
479+
7. 微信开放平台文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN
480+
8. GitHub第三方登录文档:https://developer.github.com/apps/building-oauth-apps/
481+
9. 谷歌Oauth2文档:https://developers.google.com/identity/protocols/OpenIDConnect
482+
10. 微软Oauth2文档:https://docs.microsoft.com/zh-cn/graph/auth-v2-user
483+
11. 小米开放平台账号服务文档:https://dev.mi.com/console/doc/detail?pId=707
552484

553485

554486

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /