diff --git a/spring-boot-demo-oauth/pom.xml b/spring-boot-demo-oauth/pom.xml index 07286c7..1b4b2ac 100644 --- a/spring-boot-demo-oauth/pom.xml +++ b/spring-boot-demo-oauth/pom.xml @@ -34,12 +34,35 @@ test + + cn.hutool + hutool-all + + + + com.google.zxing + core + 3.3.3 + + me.zhyd.oauth JustAuth 1.0.1 + + + org.projectlombok + lombok + true + + + + net.dreamlu + mica-auto + 1.0.2 + diff --git a/spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/CommonProperties.java b/spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/CommonProperties.java new file mode 100644 index 0000000..bfcc090 --- /dev/null +++ b/spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/CommonProperties.java @@ -0,0 +1,32 @@ +package com.xkcoding.oauth.config.props; + +import lombok.Data; + +/** + *

+ * 通用配置 + *

+ * + * @package: com.xkcoding.oauth.config.props + * @description: 通用配置 + * @author: yangkai.shen + * @date: Created in 2019-05-17 16:02 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +public class CommonProperties { + /** + * clientId + */ + private String clientId; + /** + * clientSecret + */ + private String clientSecret; + /** + * 成功后的回调 + */ + private String redirectUri; +} diff --git a/spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/OAuthProperties.java b/spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/OAuthProperties.java new file mode 100644 index 0000000..bddaf6d --- /dev/null +++ b/spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/OAuthProperties.java @@ -0,0 +1,29 @@ +package com.xkcoding.oauth.config.props; + +import lombok.Data; +import me.zhyd.oauth.config.AuthConfig; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + *

+ * 第三方登录配置 + *

+ * + * @package: com.xkcoding.oauth.config.props + * @description: 第三方登录配置 + * @author: yangkai.shen + * @date: Created in 2019-05-17 15:33 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Data +@Component +@ConfigurationProperties(prefix = "oauth") +public class OAuthProperties { + /** + * github 配置 + */ + private CommonProperties github; +} diff --git a/spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/controller/OauthController.java b/spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/controller/OauthController.java new file mode 100644 index 0000000..87a6d1c --- /dev/null +++ b/spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/controller/OauthController.java @@ -0,0 +1,85 @@ +package com.xkcoding.oauth.controller; + +import com.xkcoding.oauth.config.props.CommonProperties; +import com.xkcoding.oauth.config.props.OAuthProperties; +import lombok.RequiredArgsConstructor; +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.model.AuthResponse; +import me.zhyd.oauth.model.AuthSource; +import me.zhyd.oauth.request.AuthGithubRequest; +import me.zhyd.oauth.request.AuthRequest; +import org.springframework.beans.factory.annotation.Autowired; +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 + *

+ * + * @package: com.xkcoding.oauth.controller + * @description: 第三方登录 Controller + * @author: yangkai.shen + * @date: Created in 2019-05-17 10:07 + * @copyright: Copyright (c) 2019 + * @version: V1.0 + * @modified: yangkai.shen + */ +@RestController +@RequestMapping("/oauth") +@RequiredArgsConstructor(onConstructor_ = @Autowired) +public class OauthController { + private final OAuthProperties properties; + + /** + * 登录 + * + * @param oauthType 第三方登录类型 + * @param response response + * @throws IOException + */ + @RequestMapping("/login/{oauthType}") + public void renderAuth(@PathVariable String oauthType, HttpServletResponse response) throws IOException { + AuthRequest authRequest = getAuthRequest(oauthType); + response.sendRedirect(authRequest.authorize()); + } + + /** + * 登录成功后的回调 + * + * @param oauthType 第三方登录类型 + * @param code 携带的授权码 + * @return 登录成功后的信息 + */ + @RequestMapping("/{oauthType}/callback") + public AuthResponse login(@PathVariable String oauthType, String code) { + AuthRequest authRequest = getAuthRequest(oauthType); + return authRequest.login(code); + } + + private AuthRequest getAuthRequest(String oauthType) { + AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); + switch (authSource) { + case GITHUB: + return getGithubAuthRequest(); + default: + throw new RuntimeException("暂不支持的第三方登录"); + } + } + + private AuthRequest getGithubAuthRequest() { + return new AuthGithubRequest(buildAuthConfig(properties.getGithub())); + } + + private AuthConfig buildAuthConfig(CommonProperties properties) { + return AuthConfig.builder() + .clientId(properties.getClientId()) + .clientSecret(properties.getClientSecret()) + .redirectUri(properties.getRedirectUri()) + .build(); + } +} diff --git a/spring-boot-demo-oauth/src/main/resources/application.properties b/spring-boot-demo-oauth/src/main/resources/application.properties deleted file mode 100644 index e69de29..0000000 diff --git a/spring-boot-demo-oauth/src/main/resources/application.yml b/spring-boot-demo-oauth/src/main/resources/application.yml new file mode 100644 index 0000000..d5b79ff --- /dev/null +++ b/spring-boot-demo-oauth/src/main/resources/application.yml @@ -0,0 +1,10 @@ +server: + port: 8080 + servlet: + context-path: /demo + +oauth: + github: + client-id: 2d25a70d12e3d5f01086 + client-secret: 5a2919b5fe911567343aea2ccc7a5ad7871306d1 + redirect-uri: http://oauth.xkcoding.com/demo/oauth/github/callback \ No newline at end of file