Browse Source

spring-boot-demo-oauth github 登录完成

pull/1/head
Yangkai.Shen 6 years ago
parent
commit
ad8d343da1
6 changed files with 179 additions and 0 deletions
  1. +23
    -0
      spring-boot-demo-oauth/pom.xml
  2. +32
    -0
      spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/CommonProperties.java
  3. +29
    -0
      spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/OAuthProperties.java
  4. +85
    -0
      spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/controller/OauthController.java
  5. +0
    -0
      spring-boot-demo-oauth/src/main/resources/application.properties
  6. +10
    -0
      spring-boot-demo-oauth/src/main/resources/application.yml

+ 23
- 0
spring-boot-demo-oauth/pom.xml View File

@@ -34,12 +34,35 @@
<scope>test</scope>
</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工具类 -->
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>1.0.1</version>
</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>

<build>


+ 32
- 0
spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/CommonProperties.java View File

@@ -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;
}

+ 29
- 0
spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/OAuthProperties.java View File

@@ -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;
}

+ 85
- 0
spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/controller/OauthController.java View File

@@ -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
spring-boot-demo-oauth/src/main/resources/application.properties View File


+ 10
- 0
spring-boot-demo-oauth/src/main/resources/application.yml View File

@@ -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

Loading…
Cancel
Save