@@ -275,7 +275,7 @@ $ nginx -s reload
275
275
<dependency >
276
276
<groupId >me.zhyd.oauth</groupId >
277
277
<artifactId >JustAuth</artifactId >
278
- <version >1.8.1 </version >
278
+ <version >1.9.5 </version >
279
279
</dependency >
280
280
281
281
<dependency >
@@ -398,6 +398,28 @@ public class OAuthProperties {
398
398
### 2.4. OauthController.java
399
399
400
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
+
401
423
/**
402
424
* <p >
403
425
* 第三方登录 Controller
@@ -411,6 +433,7 @@ public class OAuthProperties {
411
433
* @version : V1.0
412
434
* @modified: yangkai.shen
413
435
*/
436
+ @Slf4j
414
437
@RestController
415
438
@RequestMapping (" /oauth" )
416
439
@RequiredArgsConstructor (onConstructor_ = @Autowired )
@@ -422,13 +445,7 @@ public class OauthController {
422
445
*/
423
446
@GetMapping
424
447
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" );
432
449
}
433
450
434
451
/**
@@ -441,7 +458,7 @@ public class OauthController {
441
458
@RequestMapping (" /login/{oauthType}" )
442
459
public void renderAuth (@PathVariable String oauthType , HttpServletResponse response ) throws IOException {
443
460
AuthRequest authRequest = getAuthRequest(oauthType);
444
- response. sendRedirect(authRequest. authorize());
461
+ response. sendRedirect(authRequest. authorize(AuthStateUtils . createState() ));
445
462
}
446
463
447
464
/**
@@ -455,68 +472,61 @@ public class OauthController {
455
472
public AuthResponse login (@PathVariable String oauthType , AuthCallback callback ) {
456
473
AuthRequest authRequest = getAuthRequest(oauthType);
457
474
AuthResponse response = authRequest. login(callback);
458
- // 移除校验通过的state
459
- AuthState . delete(oauthType);
475
+ log. info(" 【response】= {}" , JSONUtil . toJsonStr(response));
460
476
return response;
461
477
}
462
478
463
479
private AuthRequest getAuthRequest (String oauthType ) {
464
480
AuthSource authSource = AuthSource . valueOf(oauthType. toUpperCase());
465
- String state = AuthState . create(oauthType);
466
481
switch (authSource) {
467
482
case QQ :
468
- return getQqAuthRequest(state );
483
+ return getQqAuthRequest();
469
484
case GITHUB :
470
- return getGithubAuthRequest(state );
485
+ return getGithubAuthRequest();
471
486
case WECHAT :
472
- return getWechatAuthRequest(state );
487
+ return getWechatAuthRequest();
473
488
case GOOGLE :
474
- return getGoogleAuthRequest(state );
489
+ return getGoogleAuthRequest();
475
490
case MICROSOFT :
476
- return getMicrosoftAuthRequest(state );
491
+ return getMicrosoftAuthRequest();
477
492
case MI :
478
- return getMiAuthRequest(state );
493
+ return getMiAuthRequest();
479
494
default :
480
495
throw new RuntimeException (" 暂不支持的第三方登录" );
481
496
}
482
497
}
483
498
484
- private AuthRequest getQqAuthRequest (String state ) {
499
+ private AuthRequest getQqAuthRequest () {
485
500
AuthConfig authConfig = properties. getQq();
486
- authConfig. setState(state);
487
501
return new AuthQqRequest (authConfig);
488
502
}
489
503
490
- private AuthRequest getGithubAuthRequest (String state ) {
504
+ private AuthRequest getGithubAuthRequest () {
491
505
AuthConfig authConfig = properties. getGithub();
492
- authConfig. setState(state);
493
506
return new AuthGithubRequest (authConfig);
494
507
}
495
508
496
- private AuthRequest getWechatAuthRequest (String state ) {
509
+ private AuthRequest getWechatAuthRequest () {
497
510
AuthConfig authConfig = properties. getWechat();
498
- authConfig. setState(state);
499
511
return new AuthWeChatRequest (authConfig);
500
512
}
501
513
502
- private AuthRequest getGoogleAuthRequest (String state ) {
514
+ private AuthRequest getGoogleAuthRequest () {
503
515
AuthConfig authConfig = properties. getGoogle();
504
- authConfig. setState(state);
505
516
return new AuthGoogleRequest (authConfig);
506
517
}
507
518
508
- private AuthRequest getMicrosoftAuthRequest (String state ) {
519
+ private AuthRequest getMicrosoftAuthRequest () {
509
520
AuthConfig authConfig = properties. getMicrosoft();
510
- authConfig. setState(state);
511
521
return new AuthMicrosoftRequest (authConfig);
512
522
}
513
523
514
- private AuthRequest getMiAuthRequest (String state ) {
524
+ private AuthRequest getMiAuthRequest () {
515
525
AuthConfig authConfig = properties. getMi();
516
- authConfig. setState(state);
517
526
return new AuthMiRequest (authConfig);
518
527
}
519
528
}
529
+
520
530
```
521
531
522
532
## 3. 运行方式
0 commit comments