@@ -275,7 +275,7 @@ $ nginx -s reload | |||||
<dependency> | <dependency> | ||||
<groupId>me.zhyd.oauth</groupId> | <groupId>me.zhyd.oauth</groupId> | ||||
<artifactId>JustAuth</artifactId> | <artifactId>JustAuth</artifactId> | ||||
<version>1.8.1</version> | |||||
<version>1.9.5</version> | |||||
</dependency> | </dependency> | ||||
<dependency> | <dependency> | ||||
@@ -398,6 +398,28 @@ public class OAuthProperties { | |||||
### 2.4. OauthController.java | ### 2.4. OauthController.java | ||||
```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; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
* 第三方登录 Controller | * 第三方登录 Controller | ||||
@@ -411,6 +433,7 @@ public class OAuthProperties { | |||||
* @version: V1.0 | * @version: V1.0 | ||||
* @modified: yangkai.shen | * @modified: yangkai.shen | ||||
*/ | */ | ||||
@Slf4j | |||||
@RestController | @RestController | ||||
@RequestMapping("/oauth") | @RequestMapping("/oauth") | ||||
@RequiredArgsConstructor(onConstructor_ = @Autowired) | @RequiredArgsConstructor(onConstructor_ = @Autowired) | ||||
@@ -422,13 +445,7 @@ public class OauthController { | |||||
*/ | */ | ||||
@GetMapping | @GetMapping | ||||
public Dict loginType() { | 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}") | @RequestMapping("/login/{oauthType}") | ||||
public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException { | public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException { | ||||
AuthRequest authRequest = getAuthRequest(oauthType); | 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) { | public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) { | ||||
AuthRequest authRequest = getAuthRequest(oauthType); | AuthRequest authRequest = getAuthRequest(oauthType); | ||||
AuthResponse response = authRequest.login(callback); | AuthResponse response = authRequest.login(callback); | ||||
// 移除校验通过的state | |||||
AuthState.delete(oauthType); | |||||
log.info("【response】= {}", JSONUtil.toJsonStr(response)); | |||||
return response; | return response; | ||||
} | } | ||||
private AuthRequest getAuthRequest(String oauthType) { | private AuthRequest getAuthRequest(String oauthType) { | ||||
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); | AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); | ||||
String state = AuthState.create(oauthType); | |||||
switch (authSource) { | switch (authSource) { | ||||
case QQ: | case QQ: | ||||
return getQqAuthRequest(state); | |||||
return getQqAuthRequest(); | |||||
case GITHUB: | case GITHUB: | ||||
return getGithubAuthRequest(state); | |||||
return getGithubAuthRequest(); | |||||
case WECHAT: | case WECHAT: | ||||
return getWechatAuthRequest(state); | |||||
return getWechatAuthRequest(); | |||||
case GOOGLE: | case GOOGLE: | ||||
return getGoogleAuthRequest(state); | |||||
return getGoogleAuthRequest(); | |||||
case MICROSOFT: | case MICROSOFT: | ||||
return getMicrosoftAuthRequest(state); | |||||
return getMicrosoftAuthRequest(); | |||||
case MI: | case MI: | ||||
return getMiAuthRequest(state); | |||||
return getMiAuthRequest(); | |||||
default: | default: | ||||
throw new RuntimeException("暂不支持的第三方登录"); | throw new RuntimeException("暂不支持的第三方登录"); | ||||
} | } | ||||
} | } | ||||
private AuthRequest getQqAuthRequest(String state) { | |||||
private AuthRequest getQqAuthRequest() { | |||||
AuthConfig authConfig = properties.getQq(); | AuthConfig authConfig = properties.getQq(); | ||||
authConfig.setState(state); | |||||
return new AuthQqRequest(authConfig); | return new AuthQqRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getGithubAuthRequest(String state) { | |||||
private AuthRequest getGithubAuthRequest() { | |||||
AuthConfig authConfig = properties.getGithub(); | AuthConfig authConfig = properties.getGithub(); | ||||
authConfig.setState(state); | |||||
return new AuthGithubRequest(authConfig); | return new AuthGithubRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getWechatAuthRequest(String state) { | |||||
private AuthRequest getWechatAuthRequest() { | |||||
AuthConfig authConfig = properties.getWechat(); | AuthConfig authConfig = properties.getWechat(); | ||||
authConfig.setState(state); | |||||
return new AuthWeChatRequest(authConfig); | return new AuthWeChatRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getGoogleAuthRequest(String state) { | |||||
private AuthRequest getGoogleAuthRequest() { | |||||
AuthConfig authConfig = properties.getGoogle(); | AuthConfig authConfig = properties.getGoogle(); | ||||
authConfig.setState(state); | |||||
return new AuthGoogleRequest(authConfig); | return new AuthGoogleRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getMicrosoftAuthRequest(String state) { | |||||
private AuthRequest getMicrosoftAuthRequest() { | |||||
AuthConfig authConfig = properties.getMicrosoft(); | AuthConfig authConfig = properties.getMicrosoft(); | ||||
authConfig.setState(state); | |||||
return new AuthMicrosoftRequest(authConfig); | return new AuthMicrosoftRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getMiAuthRequest(String state) { | |||||
private AuthRequest getMiAuthRequest() { | |||||
AuthConfig authConfig = properties.getMi(); | AuthConfig authConfig = properties.getMi(); | ||||
authConfig.setState(state); | |||||
return new AuthMiRequest(authConfig); | return new AuthMiRequest(authConfig); | ||||
} | } | ||||
} | } | ||||
``` | ``` | ||||
## 3. 运行方式 | ## 3. 运行方式 | ||||
@@ -39,7 +39,7 @@ | |||||
<dependency> | <dependency> | ||||
<groupId>me.zhyd.oauth</groupId> | <groupId>me.zhyd.oauth</groupId> | ||||
<artifactId>JustAuth</artifactId> | <artifactId>JustAuth</artifactId> | ||||
<version>1.8.1</version> | |||||
<version>1.9.5</version> | |||||
</dependency> | </dependency> | ||||
<dependency> | <dependency> | ||||
@@ -1,14 +1,16 @@ | |||||
package com.xkcoding.social.controller; | package com.xkcoding.social.controller; | ||||
import cn.hutool.core.lang.Dict; | import cn.hutool.core.lang.Dict; | ||||
import cn.hutool.json.JSONUtil; | |||||
import com.xkcoding.social.props.OAuthProperties; | import com.xkcoding.social.props.OAuthProperties; | ||||
import lombok.RequiredArgsConstructor; | import lombok.RequiredArgsConstructor; | ||||
import lombok.extern.slf4j.Slf4j; | |||||
import me.zhyd.oauth.config.AuthConfig; | import me.zhyd.oauth.config.AuthConfig; | ||||
import me.zhyd.oauth.config.AuthSource; | import me.zhyd.oauth.config.AuthSource; | ||||
import me.zhyd.oauth.model.AuthCallback; | import me.zhyd.oauth.model.AuthCallback; | ||||
import me.zhyd.oauth.model.AuthResponse; | import me.zhyd.oauth.model.AuthResponse; | ||||
import me.zhyd.oauth.request.*; | 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.beans.factory.annotation.Autowired; | ||||
import org.springframework.web.bind.annotation.GetMapping; | import org.springframework.web.bind.annotation.GetMapping; | ||||
import org.springframework.web.bind.annotation.PathVariable; | import org.springframework.web.bind.annotation.PathVariable; | ||||
@@ -31,6 +33,7 @@ import java.io.IOException; | |||||
* @version: V1.0 | * @version: V1.0 | ||||
* @modified: yangkai.shen | * @modified: yangkai.shen | ||||
*/ | */ | ||||
@Slf4j | |||||
@RestController | @RestController | ||||
@RequestMapping("/oauth") | @RequestMapping("/oauth") | ||||
@RequiredArgsConstructor(onConstructor_ = @Autowired) | @RequiredArgsConstructor(onConstructor_ = @Autowired) | ||||
@@ -42,13 +45,7 @@ public class OauthController { | |||||
*/ | */ | ||||
@GetMapping | @GetMapping | ||||
public Dict loginType() { | 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}") | @RequestMapping("/login/{oauthType}") | ||||
public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException { | public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException { | ||||
AuthRequest authRequest = getAuthRequest(oauthType); | 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) { | public AuthResponse login(@PathVariable String oauthType, AuthCallback callback) { | ||||
AuthRequest authRequest = getAuthRequest(oauthType); | AuthRequest authRequest = getAuthRequest(oauthType); | ||||
AuthResponse response = authRequest.login(callback); | AuthResponse response = authRequest.login(callback); | ||||
// 移除校验通过的state | |||||
AuthState.delete(oauthType); | |||||
log.info("【response】= {}", JSONUtil.toJsonStr(response)); | |||||
return response; | return response; | ||||
} | } | ||||
private AuthRequest getAuthRequest(String oauthType) { | private AuthRequest getAuthRequest(String oauthType) { | ||||
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); | AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); | ||||
String state = AuthState.create(oauthType); | |||||
switch (authSource) { | switch (authSource) { | ||||
case QQ: | case QQ: | ||||
return getQqAuthRequest(state); | |||||
return getQqAuthRequest(); | |||||
case GITHUB: | case GITHUB: | ||||
return getGithubAuthRequest(state); | |||||
return getGithubAuthRequest(); | |||||
case WECHAT: | case WECHAT: | ||||
return getWechatAuthRequest(state); | |||||
return getWechatAuthRequest(); | |||||
case GOOGLE: | case GOOGLE: | ||||
return getGoogleAuthRequest(state); | |||||
return getGoogleAuthRequest(); | |||||
case MICROSOFT: | case MICROSOFT: | ||||
return getMicrosoftAuthRequest(state); | |||||
return getMicrosoftAuthRequest(); | |||||
case MI: | case MI: | ||||
return getMiAuthRequest(state); | |||||
return getMiAuthRequest(); | |||||
default: | default: | ||||
throw new RuntimeException("暂不支持的第三方登录"); | throw new RuntimeException("暂不支持的第三方登录"); | ||||
} | } | ||||
} | } | ||||
private AuthRequest getQqAuthRequest(String state) { | |||||
private AuthRequest getQqAuthRequest() { | |||||
AuthConfig authConfig = properties.getQq(); | AuthConfig authConfig = properties.getQq(); | ||||
authConfig.setState(state); | |||||
return new AuthQqRequest(authConfig); | return new AuthQqRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getGithubAuthRequest(String state) { | |||||
private AuthRequest getGithubAuthRequest() { | |||||
AuthConfig authConfig = properties.getGithub(); | AuthConfig authConfig = properties.getGithub(); | ||||
authConfig.setState(state); | |||||
return new AuthGithubRequest(authConfig); | return new AuthGithubRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getWechatAuthRequest(String state) { | |||||
private AuthRequest getWechatAuthRequest() { | |||||
AuthConfig authConfig = properties.getWechat(); | AuthConfig authConfig = properties.getWechat(); | ||||
authConfig.setState(state); | |||||
return new AuthWeChatRequest(authConfig); | return new AuthWeChatRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getGoogleAuthRequest(String state) { | |||||
private AuthRequest getGoogleAuthRequest() { | |||||
AuthConfig authConfig = properties.getGoogle(); | AuthConfig authConfig = properties.getGoogle(); | ||||
authConfig.setState(state); | |||||
return new AuthGoogleRequest(authConfig); | return new AuthGoogleRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getMicrosoftAuthRequest(String state) { | |||||
private AuthRequest getMicrosoftAuthRequest() { | |||||
AuthConfig authConfig = properties.getMicrosoft(); | AuthConfig authConfig = properties.getMicrosoft(); | ||||
authConfig.setState(state); | |||||
return new AuthMicrosoftRequest(authConfig); | return new AuthMicrosoftRequest(authConfig); | ||||
} | } | ||||
private AuthRequest getMiAuthRequest(String state) { | |||||
private AuthRequest getMiAuthRequest() { | |||||
AuthConfig authConfig = properties.getMi(); | AuthConfig authConfig = properties.getMi(); | ||||
authConfig.setState(state); | |||||
return new AuthMiRequest(authConfig); | return new AuthMiRequest(authConfig); | ||||
} | } | ||||
} | } |