@@ -34,12 +34,35 @@ | |||||
<scope>test</scope> | <scope>test</scope> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>com.google.zxing</groupId> | |||||
<artifactId>core</artifactId> | |||||
<version>3.3.3</version> | |||||
</dependency> | |||||
<!-- oauth工具类 --> | <!-- oauth工具类 --> | ||||
<dependency> | <dependency> | ||||
<groupId>me.zhyd.oauth</groupId> | <groupId>me.zhyd.oauth</groupId> | ||||
<artifactId>JustAuth</artifactId> | <artifactId>JustAuth</artifactId> | ||||
<version>1.0.1</version> | <version>1.0.1</version> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>net.dreamlu</groupId> | |||||
<artifactId>mica-auto</artifactId> | |||||
<version>1.0.2</version> | |||||
</dependency> | |||||
</dependencies> | </dependencies> | ||||
<build> | <build> | ||||
@@ -0,0 +1,32 @@ | |||||
package com.xkcoding.oauth.config.props; | |||||
import lombok.Data; | |||||
/** | |||||
* <p> | |||||
* 通用配置 | |||||
* </p> | |||||
* | |||||
* @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; | |||||
} |
@@ -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; | |||||
/** | |||||
* <p> | |||||
* 第三方登录配置 | |||||
* </p> | |||||
* | |||||
* @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; | |||||
} |
@@ -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; | |||||
/** | |||||
* <p> | |||||
* 第三方登录 Controller | |||||
* </p> | |||||
* | |||||
* @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(); | |||||
} | |||||
} |
@@ -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 |