Browse Source

spring-boot-demo-oauth 完成

pull/1/head
Yangkai.Shen 6 years ago
parent
commit
9a86e0d0f1
6 changed files with 56 additions and 51 deletions
  1. +1
    -1
      spring-boot-demo-oauth/pom.xml
  2. +1
    -0
      spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/SpringBootDemoOauthApplication.java
  3. +0
    -32
      spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/CommonProperties.java
  4. +16
    -2
      spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/OAuthProperties.java
  5. +27
    -13
      spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/controller/OauthController.java
  6. +11
    -3
      spring-boot-demo-oauth/src/main/resources/application.yml

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

@@ -49,7 +49,7 @@
<dependency> <dependency>
<groupId>me.zhyd.oauth</groupId> <groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId> <artifactId>JustAuth</artifactId>
<version>1.2.0</version>
<version>1.3.2</version>
</dependency> </dependency>


<dependency> <dependency>


+ 1
- 0
spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/SpringBootDemoOauthApplication.java View File

@@ -2,6 +2,7 @@ package com.xkcoding.oauth;


import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;


/** /**
* <p> * <p>


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

@@ -1,32 +0,0 @@
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;
}

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

@@ -22,9 +22,23 @@ import org.springframework.stereotype.Component;
@Component @Component
@ConfigurationProperties(prefix = "oauth") @ConfigurationProperties(prefix = "oauth")
public class OAuthProperties { public class OAuthProperties {
/**
* QQ 配置
*/
private AuthConfig qq;

/** /**
* github 配置 * github 配置
*/ */
private CommonProperties github;
private CommonProperties wechat;
private AuthConfig github;

/**
* 微信 配置
*/
private AuthConfig wechat;

/**
* Google 配置
*/
private AuthConfig google;
} }

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

@@ -1,15 +1,13 @@
package com.xkcoding.oauth.controller; package com.xkcoding.oauth.controller;


import com.xkcoding.oauth.config.props.CommonProperties;
import cn.hutool.core.lang.Dict;
import com.xkcoding.oauth.config.props.OAuthProperties; import com.xkcoding.oauth.config.props.OAuthProperties;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthResponse; import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthSource; import me.zhyd.oauth.model.AuthSource;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.request.AuthWeChatRequest;
import me.zhyd.oauth.request.*;
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.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -36,6 +34,18 @@ import java.io.IOException;
public class OauthController { public class OauthController {
private final OAuthProperties properties; private final OAuthProperties properties;


/**
* 登录类型
*/
@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");
}

/** /**
* 登录 * 登录
* *
@@ -65,28 +75,32 @@ public class OauthController {
private AuthRequest getAuthRequest(String oauthType) { private AuthRequest getAuthRequest(String oauthType) {
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase());
switch (authSource) { switch (authSource) {
case QQ:
return getQqAuthRequest();
case GITHUB: case GITHUB:
return getGithubAuthRequest(); return getGithubAuthRequest();
case WECHAT: case WECHAT:
return getWechatAuthRequest(); return getWechatAuthRequest();
case GOOGLE:
return getGoogleAuthRequest();
default: default:
throw new RuntimeException("暂不支持的第三方登录"); throw new RuntimeException("暂不支持的第三方登录");
} }
} }


private AuthRequest getQqAuthRequest() {
return new AuthQqRequest(properties.getQq());
}

private AuthRequest getGithubAuthRequest() { private AuthRequest getGithubAuthRequest() {
return new AuthGithubRequest(buildAuthConfig(properties.getGithub()));
return new AuthGithubRequest(properties.getGithub());
} }


private AuthRequest getWechatAuthRequest() { private AuthRequest getWechatAuthRequest() {
return new AuthWeChatRequest(buildAuthConfig(properties.getWechat()));
return new AuthWeChatRequest(properties.getWechat());
} }


private AuthConfig buildAuthConfig(CommonProperties properties) {
return AuthConfig.builder()
.clientId(properties.getClientId())
.clientSecret(properties.getClientSecret())
.redirectUri(properties.getRedirectUri())
.build();
private AuthRequest getGoogleAuthRequest() {
return new AuthGoogleRequest(properties.getGoogle());
} }
} }

+ 11
- 3
spring-boot-demo-oauth/src/main/resources/application.yml View File

@@ -4,11 +4,19 @@ server:
context-path: /demo context-path: /demo


oauth: oauth:
qq:
client-id: 101577785
client-secret: 1f7d08df5576671a5b799e73cc2d629e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
github: github:
client-id: 2d25a70d12e3d5f01086 client-id: 2d25a70d12e3d5f01086
client-secret: 5a2919b5fe911567343aea2ccc7a5ad7871306d1 client-secret: 5a2919b5fe911567343aea2ccc7a5ad7871306d1
redirect-uri: http://oauth.xkcoding.com/demo/oauth/github/callback redirect-uri: http://oauth.xkcoding.com/demo/oauth/github/callback
wechat: wechat:
client-id:
client-secret:
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat/callback
client-id: wxdcb31cd7f1794ff4
client-secret: b4e9dc6841ef7d2f520d449bca08ed6d
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat/callback
google:
client-id: 716518501517-6dbdkapivhia806vqcjjh9nttj320ie3.apps.googleusercontent.com
client-secret: 9IBornd7w1ALXhxZiDwEf7-E
redirect-uri: http://oauth.xkcoding.com/demo/oauth/google/callback

Loading…
Cancel
Save