@@ -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. 运行方式
0 commit comments