Skip to content

Commit

Permalink
✨ spring-boot-demo-oauth 完成
Browse files Browse the repository at this point in the history
  • Loading branch information
xkcoding committed May 23, 2019
1 parent 1b6fa68 commit 9a86e0d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 51 deletions.
2 changes: 1 addition & 1 deletion spring-boot-demo-oauth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>1.2.0</version>
<version>1.3.2</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

/**
* <p>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,23 @@
@Component
@ConfigurationProperties(prefix = "oauth")
public class OAuthProperties {
/**
* QQ 配置
*/
private AuthConfig qq;

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

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

/**
* Google 配置
*/
private AuthConfig google;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
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 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 me.zhyd.oauth.request.AuthWeChatRequest;
import me.zhyd.oauth.request.*;
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;
Expand All @@ -36,6 +34,18 @@
public class OauthController {
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");
}

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

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

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

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());
}
}
14 changes: 11 additions & 3 deletions spring-boot-demo-oauth/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ server:
context-path: /demo

oauth:
qq:
client-id: 101577785
client-secret: 1f7d08df5576671a5b799e73cc2d629e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
github:
client-id: 2d25a70d12e3d5f01086
client-secret: 5a2919b5fe911567343aea2ccc7a5ad7871306d1
redirect-uri: http://oauth.xkcoding.com/demo/oauth/github/callback
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

0 comments on commit 9a86e0d

Please sign in to comment.