From 2868a64aaa7205443d30c3ea947c2bc46b249e27 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Thu, 1 Aug 2019 15:43:02 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=8D=87=E7=BA=A7=20JustAuth=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=B8=BA=201.9.5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-demo-social/README.md | 70 ++++++++++++---------- spring-boot-demo-social/pom.xml | 2 +- .../social/controller/OauthController.java | 49 ++++++--------- 3 files changed, 60 insertions(+), 61 deletions(-) diff --git a/spring-boot-demo-social/README.md b/spring-boot-demo-social/README.md index 441d448..dc3069f 100644 --- a/spring-boot-demo-social/README.md +++ b/spring-boot-demo-social/README.md @@ -275,7 +275,7 @@ $ nginx -s reload me.zhyd.oauth JustAuth - 1.8.1 + 1.9.5 @@ -398,6 +398,28 @@ public class OAuthProperties { ### 2.4. OauthController.java ```java +package com.xkcoding.social.controller; + +import cn.hutool.core.lang.Dict; +import cn.hutool.json.JSONUtil; +import com.xkcoding.social.props.OAuthProperties; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.model.AuthCallback; +import me.zhyd.oauth.model.AuthResponse; +import me.zhyd.oauth.request.*; +import me.zhyd.oauth.utils.AuthStateUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + /** *

* 第三方登录 Controller @@ -411,6 +433,7 @@ public class OAuthProperties { * @version: V1.0 * @modified: yangkai.shen */ +@Slf4j @RestController @RequestMapping("/oauth") @RequiredArgsConstructor(onConstructor_ = @Autowired) @@ -422,13 +445,7 @@ public class OauthController { */ @GetMapping public Dict loginType() { - 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"); + 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"); } /** @@ -441,7 +458,7 @@ public class OauthController { @RequestMapping("/login/{oauthType}") public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException { AuthRequest authRequest = getAuthRequest(oauthType); - response.sendRedirect(authRequest.authorize()); + response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); } /** @@ -455,68 +472,61 @@ public class OauthController { public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) { AuthRequest authRequest = getAuthRequest(oauthType); AuthResponse response = authRequest.login(callback); - // 移除校验通过的state - AuthState.delete(oauthType); + log.info("【response】= {}", JSONUtil.toJsonStr(response)); return response; } private AuthRequest getAuthRequest(String oauthType) { AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); - String state = AuthState.create(oauthType); switch (authSource) { case QQ: - return getQqAuthRequest(state); + return getQqAuthRequest(); case GITHUB: - return getGithubAuthRequest(state); + return getGithubAuthRequest(); case WECHAT: - return getWechatAuthRequest(state); + return getWechatAuthRequest(); case GOOGLE: - return getGoogleAuthRequest(state); + return getGoogleAuthRequest(); case MICROSOFT: - return getMicrosoftAuthRequest(state); + return getMicrosoftAuthRequest(); case MI: - return getMiAuthRequest(state); + return getMiAuthRequest(); default: throw new RuntimeException("暂不支持的第三方登录"); } } - private AuthRequest getQqAuthRequest(String state) { + private AuthRequest getQqAuthRequest() { AuthConfig authConfig = properties.getQq(); - authConfig.setState(state); return new AuthQqRequest(authConfig); } - private AuthRequest getGithubAuthRequest(String state) { + private AuthRequest getGithubAuthRequest() { AuthConfig authConfig = properties.getGithub(); - authConfig.setState(state); return new AuthGithubRequest(authConfig); } - private AuthRequest getWechatAuthRequest(String state) { + private AuthRequest getWechatAuthRequest() { AuthConfig authConfig = properties.getWechat(); - authConfig.setState(state); return new AuthWeChatRequest(authConfig); } - private AuthRequest getGoogleAuthRequest(String state) { + private AuthRequest getGoogleAuthRequest() { AuthConfig authConfig = properties.getGoogle(); - authConfig.setState(state); return new AuthGoogleRequest(authConfig); } - private AuthRequest getMicrosoftAuthRequest(String state) { + private AuthRequest getMicrosoftAuthRequest() { AuthConfig authConfig = properties.getMicrosoft(); - authConfig.setState(state); return new AuthMicrosoftRequest(authConfig); } - private AuthRequest getMiAuthRequest(String state) { + private AuthRequest getMiAuthRequest() { AuthConfig authConfig = properties.getMi(); - authConfig.setState(state); return new AuthMiRequest(authConfig); } } + ``` ## 3. 运行方式 diff --git a/spring-boot-demo-social/pom.xml b/spring-boot-demo-social/pom.xml index 350d66e..82b2bde 100644 --- a/spring-boot-demo-social/pom.xml +++ b/spring-boot-demo-social/pom.xml @@ -39,7 +39,7 @@ me.zhyd.oauth JustAuth - 1.8.1 + 1.9.5 diff --git a/spring-boot-demo-social/src/main/java/com/xkcoding/social/controller/OauthController.java b/spring-boot-demo-social/src/main/java/com/xkcoding/social/controller/OauthController.java index 4e7963c..7dc3cec 100644 --- a/spring-boot-demo-social/src/main/java/com/xkcoding/social/controller/OauthController.java +++ b/spring-boot-demo-social/src/main/java/com/xkcoding/social/controller/OauthController.java @@ -1,14 +1,16 @@ package com.xkcoding.social.controller; import cn.hutool.core.lang.Dict; +import cn.hutool.json.JSONUtil; import com.xkcoding.social.props.OAuthProperties; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthSource; import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthResponse; import me.zhyd.oauth.request.*; -import me.zhyd.oauth.utils.AuthState; +import me.zhyd.oauth.utils.AuthStateUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -31,6 +33,7 @@ import java.io.IOException; * @version: V1.0 * @modified: yangkai.shen */ +@Slf4j @RestController @RequestMapping("/oauth") @RequiredArgsConstructor(onConstructor_ = @Autowired) @@ -42,13 +45,7 @@ public class OauthController { */ @GetMapping public Dict loginType() { - 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"); + 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"); } /** @@ -61,7 +58,7 @@ public class OauthController { @RequestMapping("/login/{oauthType}") public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException { AuthRequest authRequest = getAuthRequest(oauthType); - response.sendRedirect(authRequest.authorize()); + response.sendRedirect(authRequest.authorize(AuthStateUtils.createState())); } /** @@ -75,65 +72,57 @@ public class OauthController { public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) { AuthRequest authRequest = getAuthRequest(oauthType); AuthResponse response = authRequest.login(callback); - // 移除校验通过的state - AuthState.delete(oauthType); + log.info("【response】= {}", JSONUtil.toJsonStr(response)); return response; } private AuthRequest getAuthRequest(String oauthType) { AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); - String state = AuthState.create(oauthType); switch (authSource) { case QQ: - return getQqAuthRequest(state); + return getQqAuthRequest(); case GITHUB: - return getGithubAuthRequest(state); + return getGithubAuthRequest(); case WECHAT: - return getWechatAuthRequest(state); + return getWechatAuthRequest(); case GOOGLE: - return getGoogleAuthRequest(state); + return getGoogleAuthRequest(); case MICROSOFT: - return getMicrosoftAuthRequest(state); + return getMicrosoftAuthRequest(); case MI: - return getMiAuthRequest(state); + return getMiAuthRequest(); default: throw new RuntimeException("暂不支持的第三方登录"); } } - private AuthRequest getQqAuthRequest(String state) { + private AuthRequest getQqAuthRequest() { AuthConfig authConfig = properties.getQq(); - authConfig.setState(state); return new AuthQqRequest(authConfig); } - private AuthRequest getGithubAuthRequest(String state) { + private AuthRequest getGithubAuthRequest() { AuthConfig authConfig = properties.getGithub(); - authConfig.setState(state); return new AuthGithubRequest(authConfig); } - private AuthRequest getWechatAuthRequest(String state) { + private AuthRequest getWechatAuthRequest() { AuthConfig authConfig = properties.getWechat(); - authConfig.setState(state); return new AuthWeChatRequest(authConfig); } - private AuthRequest getGoogleAuthRequest(String state) { + private AuthRequest getGoogleAuthRequest() { AuthConfig authConfig = properties.getGoogle(); - authConfig.setState(state); return new AuthGoogleRequest(authConfig); } - private AuthRequest getMicrosoftAuthRequest(String state) { + private AuthRequest getMicrosoftAuthRequest() { AuthConfig authConfig = properties.getMicrosoft(); - authConfig.setState(state); return new AuthMicrosoftRequest(authConfig); } - private AuthRequest getMiAuthRequest(String state) { + private AuthRequest getMiAuthRequest() { AuthConfig authConfig = properties.getMi(); - authConfig.setState(state); return new AuthMiRequest(authConfig); } }