Skip to content

Commit 2868a64

Browse files
committed
✨ 升级 JustAuth 版本为 1.9.5
1 parent e7b8abc commit 2868a64

File tree

3 files changed

+60
-61
lines changed

3 files changed

+60
-61
lines changed

spring-boot-demo-social/README.md

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ $ nginx -s reload
275275
<dependency>
276276
<groupId>me.zhyd.oauth</groupId>
277277
<artifactId>JustAuth</artifactId>
278-
<version>1.8.1</version>
278+
<version>1.9.5</version>
279279
</dependency>
280280

281281
<dependency>
@@ -398,6 +398,28 @@ public class OAuthProperties {
398398
### 2.4. OauthController.java
399399

400400
```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+
401423
/**
402424
* <p>
403425
* 第三方登录 Controller
@@ -411,6 +433,7 @@ public class OAuthProperties {
411433
* @version: V1.0
412434
* @modified: yangkai.shen
413435
*/
436+
@Slf4j
414437
@RestController
415438
@RequestMapping("/oauth")
416439
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@@ -422,13 +445,7 @@ public class OauthController {
422445
*/
423446
@GetMapping
424447
public Dict loginType() {
425-
return Dict.create()
426-
.set("QQ登录", "http://oauth.xkcoding.com/demo/oauth/login/qq")
427-
.set("GitHub登录", "http://oauth.xkcoding.com/demo/oauth/login/github")
428-
.set("微信登录", "http://oauth.xkcoding.com/demo/oauth/login/wechat")
429-
.set("Google登录", "http://oauth.xkcoding.com/demo/oauth/login/google")
430-
.set("Microsoft 登录", "http://oauth.xkcoding.com/demo/oauth/login/microsoft")
431-
.set("小米登录", "http://oauth.xkcoding.com/demo/oauth/login/mi");
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");
432449
}
433450

434451
/**
@@ -441,7 +458,7 @@ public class OauthController {
441458
@RequestMapping("/login/{oauthType}")
442459
public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException {
443460
AuthRequest authRequest = getAuthRequest(oauthType);
444-
response.sendRedirect(authRequest.authorize());
461+
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
445462
}
446463

447464
/**
@@ -455,68 +472,61 @@ public class OauthController {
455472
public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) {
456473
AuthRequest authRequest = getAuthRequest(oauthType);
457474
AuthResponse response = authRequest.login(callback);
458-
// 移除校验通过的state
459-
AuthState.delete(oauthType);
475+
log.info("【response】= {}", JSONUtil.toJsonStr(response));
460476
return response;
461477
}
462478

463479
private AuthRequest getAuthRequest(String oauthType) {
464480
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase());
465-
String state = AuthState.create(oauthType);
466481
switch (authSource) {
467482
case QQ:
468-
return getQqAuthRequest(state);
483+
return getQqAuthRequest();
469484
case GITHUB:
470-
return getGithubAuthRequest(state);
485+
return getGithubAuthRequest();
471486
case WECHAT:
472-
return getWechatAuthRequest(state);
487+
return getWechatAuthRequest();
473488
case GOOGLE:
474-
return getGoogleAuthRequest(state);
489+
return getGoogleAuthRequest();
475490
case MICROSOFT:
476-
return getMicrosoftAuthRequest(state);
491+
return getMicrosoftAuthRequest();
477492
case MI:
478-
return getMiAuthRequest(state);
493+
return getMiAuthRequest();
479494
default:
480495
throw new RuntimeException("暂不支持的第三方登录");
481496
}
482497
}
483498

484-
private AuthRequest getQqAuthRequest(String state) {
499+
private AuthRequest getQqAuthRequest() {
485500
AuthConfig authConfig = properties.getQq();
486-
authConfig.setState(state);
487501
return new AuthQqRequest(authConfig);
488502
}
489503

490-
private AuthRequest getGithubAuthRequest(String state) {
504+
private AuthRequest getGithubAuthRequest() {
491505
AuthConfig authConfig = properties.getGithub();
492-
authConfig.setState(state);
493506
return new AuthGithubRequest(authConfig);
494507
}
495508

496-
private AuthRequest getWechatAuthRequest(String state) {
509+
private AuthRequest getWechatAuthRequest() {
497510
AuthConfig authConfig = properties.getWechat();
498-
authConfig.setState(state);
499511
return new AuthWeChatRequest(authConfig);
500512
}
501513

502-
private AuthRequest getGoogleAuthRequest(String state) {
514+
private AuthRequest getGoogleAuthRequest() {
503515
AuthConfig authConfig = properties.getGoogle();
504-
authConfig.setState(state);
505516
return new AuthGoogleRequest(authConfig);
506517
}
507518

508-
private AuthRequest getMicrosoftAuthRequest(String state) {
519+
private AuthRequest getMicrosoftAuthRequest() {
509520
AuthConfig authConfig = properties.getMicrosoft();
510-
authConfig.setState(state);
511521
return new AuthMicrosoftRequest(authConfig);
512522
}
513523

514-
private AuthRequest getMiAuthRequest(String state) {
524+
private AuthRequest getMiAuthRequest() {
515525
AuthConfig authConfig = properties.getMi();
516-
authConfig.setState(state);
517526
return new AuthMiRequest(authConfig);
518527
}
519528
}
529+
520530
```
521531

522532
## 3. 运行方式

spring-boot-demo-social/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<dependency>
4040
<groupId>me.zhyd.oauth</groupId>
4141
<artifactId>JustAuth</artifactId>
42-
<version>1.8.1</version>
42+
<version>1.9.5</version>
4343
</dependency>
4444

4545
<dependency>
Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.xkcoding.social.controller;
22

33
import cn.hutool.core.lang.Dict;
4+
import cn.hutool.json.JSONUtil;
45
import com.xkcoding.social.props.OAuthProperties;
56
import lombok.RequiredArgsConstructor;
7+
import lombok.extern.slf4j.Slf4j;
68
import me.zhyd.oauth.config.AuthConfig;
79
import me.zhyd.oauth.config.AuthSource;
810
import me.zhyd.oauth.model.AuthCallback;
911
import me.zhyd.oauth.model.AuthResponse;
1012
import me.zhyd.oauth.request.*;
11-
import me.zhyd.oauth.utils.AuthState;
13+
import me.zhyd.oauth.utils.AuthStateUtils;
1214
import org.springframework.beans.factory.annotation.Autowired;
1315
import org.springframework.web.bind.annotation.GetMapping;
1416
import org.springframework.web.bind.annotation.PathVariable;
@@ -31,6 +33,7 @@
3133
* @version: V1.0
3234
* @modified: yangkai.shen
3335
*/
36+
@Slf4j
3437
@RestController
3538
@RequestMapping("/oauth")
3639
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@@ -42,13 +45,7 @@ public class OauthController {
4245
*/
4346
@GetMapping
4447
public Dict loginType() {
45-
return Dict.create()
46-
.set("QQ登录", "http://oauth.xkcoding.com/demo/oauth/login/qq")
47-
.set("GitHub登录", "http://oauth.xkcoding.com/demo/oauth/login/github")
48-
.set("微信登录", "http://oauth.xkcoding.com/demo/oauth/login/wechat")
49-
.set("Google登录", "http://oauth.xkcoding.com/demo/oauth/login/google")
50-
.set("Microsoft 登录", "http://oauth.xkcoding.com/demo/oauth/login/microsoft")
51-
.set("小米登录", "http://oauth.xkcoding.com/demo/oauth/login/mi");
48+
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");
5249
}
5350

5451
/**
@@ -61,7 +58,7 @@ public Dict loginType() {
6158
@RequestMapping("/login/{oauthType}")
6259
public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException {
6360
AuthRequest authRequest = getAuthRequest(oauthType);
64-
response.sendRedirect(authRequest.authorize());
61+
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
6562
}
6663

6764
/**
@@ -75,65 +72,57 @@ public void renderAuth(@PathVariable String oauthType, HttpServletResponse respo
7572
public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) {
7673
AuthRequest authRequest = getAuthRequest(oauthType);
7774
AuthResponse response = authRequest.login(callback);
78-
// 移除校验通过的state
79-
AuthState.delete(oauthType);
75+
log.info("【response】= {}", JSONUtil.toJsonStr(response));
8076
return response;
8177
}
8278

8379
private AuthRequest getAuthRequest(String oauthType) {
8480
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase());
85-
String state = AuthState.create(oauthType);
8681
switch (authSource) {
8782
case QQ:
88-
return getQqAuthRequest(state);
83+
return getQqAuthRequest();
8984
case GITHUB:
90-
return getGithubAuthRequest(state);
85+
return getGithubAuthRequest();
9186
case WECHAT:
92-
return getWechatAuthRequest(state);
87+
return getWechatAuthRequest();
9388
case GOOGLE:
94-
return getGoogleAuthRequest(state);
89+
return getGoogleAuthRequest();
9590
case MICROSOFT:
96-
return getMicrosoftAuthRequest(state);
91+
return getMicrosoftAuthRequest();
9792
case MI:
98-
return getMiAuthRequest(state);
93+
return getMiAuthRequest();
9994
default:
10095
throw new RuntimeException("暂不支持的第三方登录");
10196
}
10297
}
10398

104-
private AuthRequest getQqAuthRequest(String state) {
99+
private AuthRequest getQqAuthRequest() {
105100
AuthConfig authConfig = properties.getQq();
106-
authConfig.setState(state);
107101
return new AuthQqRequest(authConfig);
108102
}
109103

110-
private AuthRequest getGithubAuthRequest(String state) {
104+
private AuthRequest getGithubAuthRequest() {
111105
AuthConfig authConfig = properties.getGithub();
112-
authConfig.setState(state);
113106
return new AuthGithubRequest(authConfig);
114107
}
115108

116-
private AuthRequest getWechatAuthRequest(String state) {
109+
private AuthRequest getWechatAuthRequest() {
117110
AuthConfig authConfig = properties.getWechat();
118-
authConfig.setState(state);
119111
return new AuthWeChatRequest(authConfig);
120112
}
121113

122-
private AuthRequest getGoogleAuthRequest(String state) {
114+
private AuthRequest getGoogleAuthRequest() {
123115
AuthConfig authConfig = properties.getGoogle();
124-
authConfig.setState(state);
125116
return new AuthGoogleRequest(authConfig);
126117
}
127118

128-
private AuthRequest getMicrosoftAuthRequest(String state) {
119+
private AuthRequest getMicrosoftAuthRequest() {
129120
AuthConfig authConfig = properties.getMicrosoft();
130-
authConfig.setState(state);
131121
return new AuthMicrosoftRequest(authConfig);
132122
}
133123

134-
private AuthRequest getMiAuthRequest(String state) {
124+
private AuthRequest getMiAuthRequest() {
135125
AuthConfig authConfig = properties.getMi();
136-
authConfig.setState(state);
137126
return new AuthMiRequest(authConfig);
138127
}
139128
}

0 commit comments

Comments
 (0)