Skip to content

Commit 9d2ca4d

Browse files
committed
⬆️ 升级 JustAuth -> 1.8.1 完成,支持 state 校验
1 parent 34c0047 commit 9d2ca4d

File tree

3 files changed

+80
-45
lines changed

3 files changed

+80
-45
lines changed

spring-boot-demo-social/README.md

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ $ nginx -s reload
267267
<dependency>
268268
<groupId>me.zhyd.oauth</groupId>
269269
<artifactId>JustAuth</artifactId>
270-
<version>1.6.0-beta</version>
270+
<version>1.8.1</version>
271271
</dependency>
272272

273273
<dependency>
@@ -440,57 +440,73 @@ public class OauthController {
440440
* 登录成功后的回调
441441
*
442442
* @param oauthType 第三方登录类型
443-
* @param code 携带的授权码
443+
* @param callback 携带返回的信息
444444
* @return 登录成功后的信息
445445
*/
446446
@RequestMapping("/{oauthType}/callback")
447-
public AuthResponse login(@PathVariable String oauthType, String code) {
447+
public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) {
448448
AuthRequest authRequest = getAuthRequest(oauthType);
449-
return authRequest.login(code);
449+
AuthResponse response = authRequest.login(callback);
450+
// 移除校验通过的state
451+
AuthState.delete(oauthType);
452+
return response;
450453
}
451454

452455
private AuthRequest getAuthRequest(String oauthType) {
453456
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase());
457+
String state = AuthState.create(oauthType);
454458
switch (authSource) {
455459
case QQ:
456-
return getQqAuthRequest();
460+
return getQqAuthRequest(state);
457461
case GITHUB:
458-
return getGithubAuthRequest();
462+
return getGithubAuthRequest(state);
459463
case WECHAT:
460-
return getWechatAuthRequest();
464+
return getWechatAuthRequest(state);
461465
case GOOGLE:
462-
return getGoogleAuthRequest();
466+
return getGoogleAuthRequest(state);
463467
case MICROSOFT:
464-
return getMicrosoftAuthRequest();
468+
return getMicrosoftAuthRequest(state);
465469
case MI:
466-
return getMiAuthRequest();
470+
return getMiAuthRequest(state);
467471
default:
468472
throw new RuntimeException("暂不支持的第三方登录");
469473
}
470474
}
471475

472-
private AuthRequest getQqAuthRequest() {
473-
return new AuthQqRequest(properties.getQq());
476+
private AuthRequest getQqAuthRequest(String state) {
477+
AuthConfig authConfig = properties.getQq();
478+
authConfig.setState(state);
479+
return new AuthQqRequest(authConfig);
474480
}
475481

476-
private AuthRequest getGithubAuthRequest() {
477-
return new AuthGithubRequest(properties.getGithub());
482+
private AuthRequest getGithubAuthRequest(String state) {
483+
AuthConfig authConfig = properties.getGithub();
484+
authConfig.setState(state);
485+
return new AuthGithubRequest(authConfig);
478486
}
479487

480-
private AuthRequest getWechatAuthRequest() {
481-
return new AuthWeChatRequest(properties.getWechat());
488+
private AuthRequest getWechatAuthRequest(String state) {
489+
AuthConfig authConfig = properties.getWechat();
490+
authConfig.setState(state);
491+
return new AuthWeChatRequest(authConfig);
482492
}
483493

484-
private AuthRequest getGoogleAuthRequest() {
485-
return new AuthGoogleRequest(properties.getGoogle());
494+
private AuthRequest getGoogleAuthRequest(String state) {
495+
AuthConfig authConfig = properties.getGoogle();
496+
authConfig.setState(state);
497+
return new AuthGoogleRequest(authConfig);
486498
}
487499

488-
private AuthRequest getMicrosoftAuthRequest() {
489-
return new AuthMicrosoftRequest(properties.getMicrosoft());
500+
private AuthRequest getMicrosoftAuthRequest(String state) {
501+
AuthConfig authConfig = properties.getMicrosoft();
502+
authConfig.setState(state);
503+
return new AuthMicrosoftRequest(authConfig);
490504
}
491505

492-
private AuthRequest getMiAuthRequest() {
493-
return new AuthMiRequest(properties.getMi());
506+
private AuthRequest getMiAuthRequest(String state) {
507+
AuthConfig authConfig = properties.getMi();
508+
authConfig.setState(state);
509+
return new AuthMiRequest(authConfig);
494510
}
495511
}
496512
```

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.6.0-beta</version>
42+
<version>1.8.1</version>
4343
</dependency>
4444

4545
<dependency>

spring-boot-demo-social/src/main/java/com/xkcoding/social/controller/OauthController.java

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import cn.hutool.core.lang.Dict;
44
import com.xkcoding.social.props.OAuthProperties;
55
import lombok.RequiredArgsConstructor;
6+
import me.zhyd.oauth.config.AuthConfig;
7+
import me.zhyd.oauth.config.AuthSource;
8+
import me.zhyd.oauth.model.AuthCallback;
69
import me.zhyd.oauth.model.AuthResponse;
7-
import me.zhyd.oauth.model.AuthSource;
810
import me.zhyd.oauth.request.*;
11+
import me.zhyd.oauth.utils.AuthState;
912
import org.springframework.beans.factory.annotation.Autowired;
1013
import org.springframework.web.bind.annotation.GetMapping;
1114
import org.springframework.web.bind.annotation.PathVariable;
@@ -65,56 +68,72 @@ public void renderAuth(@PathVariable String oauthType, HttpServletResponse respo
6568
* 登录成功后的回调
6669
*
6770
* @param oauthType 第三方登录类型
68-
* @param code 携带的授权码
71+
* @param callback 携带返回的信息
6972
* @return 登录成功后的信息
7073
*/
7174
@RequestMapping("/{oauthType}/callback")
72-
public AuthResponse login(@PathVariable String oauthType, String code) {
75+
public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) {
7376
AuthRequest authRequest = getAuthRequest(oauthType);
74-
return authRequest.login(code);
77+
AuthResponse response = authRequest.login(callback);
78+
// 移除校验通过的state
79+
AuthState.delete(oauthType);
80+
return response;
7581
}
7682

7783
private AuthRequest getAuthRequest(String oauthType) {
7884
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase());
85+
String state = AuthState.create(oauthType);
7986
switch (authSource) {
8087
case QQ:
81-
return getQqAuthRequest();
88+
return getQqAuthRequest(state);
8289
case GITHUB:
83-
return getGithubAuthRequest();
90+
return getGithubAuthRequest(state);
8491
case WECHAT:
85-
return getWechatAuthRequest();
92+
return getWechatAuthRequest(state);
8693
case GOOGLE:
87-
return getGoogleAuthRequest();
94+
return getGoogleAuthRequest(state);
8895
case MICROSOFT:
89-
return getMicrosoftAuthRequest();
96+
return getMicrosoftAuthRequest(state);
9097
case MI:
91-
return getMiAuthRequest();
98+
return getMiAuthRequest(state);
9299
default:
93100
throw new RuntimeException("暂不支持的第三方登录");
94101
}
95102
}
96103

97-
private AuthRequest getQqAuthRequest() {
98-
return new AuthQqRequest(properties.getQq());
104+
private AuthRequest getQqAuthRequest(String state) {
105+
AuthConfig authConfig = properties.getQq();
106+
authConfig.setState(state);
107+
return new AuthQqRequest(authConfig);
99108
}
100109

101-
private AuthRequest getGithubAuthRequest() {
102-
return new AuthGithubRequest(properties.getGithub());
110+
private AuthRequest getGithubAuthRequest(String state) {
111+
AuthConfig authConfig = properties.getGithub();
112+
authConfig.setState(state);
113+
return new AuthGithubRequest(authConfig);
103114
}
104115

105-
private AuthRequest getWechatAuthRequest() {
106-
return new AuthWeChatRequest(properties.getWechat());
116+
private AuthRequest getWechatAuthRequest(String state) {
117+
AuthConfig authConfig = properties.getWechat();
118+
authConfig.setState(state);
119+
return new AuthWeChatRequest(authConfig);
107120
}
108121

109-
private AuthRequest getGoogleAuthRequest() {
110-
return new AuthGoogleRequest(properties.getGoogle());
122+
private AuthRequest getGoogleAuthRequest(String state) {
123+
AuthConfig authConfig = properties.getGoogle();
124+
authConfig.setState(state);
125+
return new AuthGoogleRequest(authConfig);
111126
}
112127

113-
private AuthRequest getMicrosoftAuthRequest() {
114-
return new AuthMicrosoftRequest(properties.getMicrosoft());
128+
private AuthRequest getMicrosoftAuthRequest(String state) {
129+
AuthConfig authConfig = properties.getMicrosoft();
130+
authConfig.setState(state);
131+
return new AuthMicrosoftRequest(authConfig);
115132
}
116133

117-
private AuthRequest getMiAuthRequest() {
118-
return new AuthMiRequest(properties.getMi());
134+
private AuthRequest getMiAuthRequest(String state) {
135+
AuthConfig authConfig = properties.getMi();
136+
authConfig.setState(state);
137+
return new AuthMiRequest(authConfig);
119138
}
120139
}

0 commit comments

Comments
 (0)