Skip to content

Commit 9a86e0d

Browse files
committed
✨ spring-boot-demo-oauth 完成
1 parent 1b6fa68 commit 9a86e0d

File tree

6 files changed

+56
-51
lines changed

6 files changed

+56
-51
lines changed

spring-boot-demo-oauth/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<dependency>
5050
<groupId>me.zhyd.oauth</groupId>
5151
<artifactId>JustAuth</artifactId>
52-
<version>1.2.0</version>
52+
<version>1.3.2</version>
5353
</dependency>
5454

5555
<dependency>

spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/SpringBootDemoOauthApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.GetMapping;
56

67
/**
78
* <p>

spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/CommonProperties.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/config/props/OAuthProperties.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,23 @@
2222
@Component
2323
@ConfigurationProperties(prefix = "oauth")
2424
public class OAuthProperties {
25+
/**
26+
* QQ 配置
27+
*/
28+
private AuthConfig qq;
29+
2530
/**
2631
* github 配置
2732
*/
28-
private CommonProperties github;
29-
private CommonProperties wechat;
33+
private AuthConfig github;
34+
35+
/**
36+
* 微信 配置
37+
*/
38+
private AuthConfig wechat;
39+
40+
/**
41+
* Google 配置
42+
*/
43+
private AuthConfig google;
3044
}

spring-boot-demo-oauth/src/main/java/com/xkcoding/oauth/controller/OauthController.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.xkcoding.oauth.controller;
22

3-
import com.xkcoding.oauth.config.props.CommonProperties;
3+
import cn.hutool.core.lang.Dict;
44
import com.xkcoding.oauth.config.props.OAuthProperties;
55
import lombok.RequiredArgsConstructor;
6-
import me.zhyd.oauth.config.AuthConfig;
76
import me.zhyd.oauth.model.AuthResponse;
87
import me.zhyd.oauth.model.AuthSource;
9-
import me.zhyd.oauth.request.AuthGithubRequest;
10-
import me.zhyd.oauth.request.AuthRequest;
11-
import me.zhyd.oauth.request.AuthWeChatRequest;
8+
import me.zhyd.oauth.request.*;
129
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.web.bind.annotation.GetMapping;
1311
import org.springframework.web.bind.annotation.PathVariable;
1412
import org.springframework.web.bind.annotation.RequestMapping;
1513
import org.springframework.web.bind.annotation.RestController;
@@ -36,6 +34,18 @@
3634
public class OauthController {
3735
private final OAuthProperties properties;
3836

37+
/**
38+
* 登录类型
39+
*/
40+
@GetMapping
41+
public Dict loginType() {
42+
return Dict.create()
43+
.set("QQ登录", "http://oauth.xkcoding.com/demo/oauth/login/qq")
44+
.set("GitHub登录", "http://oauth.xkcoding.com/demo/oauth/login/github")
45+
.set("微信登录", "http://oauth.xkcoding.com/demo/oauth/login/wechat")
46+
.set("Google登录", "http://oauth.xkcoding.com/demo/oauth/login/google");
47+
}
48+
3949
/**
4050
* 登录
4151
*
@@ -65,28 +75,32 @@ public AuthResponse login(@PathVariable String oauthType, String code) {
6575
private AuthRequest getAuthRequest(String oauthType) {
6676
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase());
6777
switch (authSource) {
78+
case QQ:
79+
return getQqAuthRequest();
6880
case GITHUB:
6981
return getGithubAuthRequest();
7082
case WECHAT:
7183
return getWechatAuthRequest();
84+
case GOOGLE:
85+
return getGoogleAuthRequest();
7286
default:
7387
throw new RuntimeException("暂不支持的第三方登录");
7488
}
7589
}
7690

91+
private AuthRequest getQqAuthRequest() {
92+
return new AuthQqRequest(properties.getQq());
93+
}
94+
7795
private AuthRequest getGithubAuthRequest() {
78-
return new AuthGithubRequest(buildAuthConfig(properties.getGithub()));
96+
return new AuthGithubRequest(properties.getGithub());
7997
}
8098

8199
private AuthRequest getWechatAuthRequest() {
82-
return new AuthWeChatRequest(buildAuthConfig(properties.getWechat()));
100+
return new AuthWeChatRequest(properties.getWechat());
83101
}
84102

85-
private AuthConfig buildAuthConfig(CommonProperties properties) {
86-
return AuthConfig.builder()
87-
.clientId(properties.getClientId())
88-
.clientSecret(properties.getClientSecret())
89-
.redirectUri(properties.getRedirectUri())
90-
.build();
103+
private AuthRequest getGoogleAuthRequest() {
104+
return new AuthGoogleRequest(properties.getGoogle());
91105
}
92106
}

spring-boot-demo-oauth/src/main/resources/application.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ server:
44
context-path: /demo
55

66
oauth:
7+
qq:
8+
client-id: 101577785
9+
client-secret: 1f7d08df5576671a5b799e73cc2d629e
10+
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
711
github:
812
client-id: 2d25a70d12e3d5f01086
913
client-secret: 5a2919b5fe911567343aea2ccc7a5ad7871306d1
1014
redirect-uri: http://oauth.xkcoding.com/demo/oauth/github/callback
1115
wechat:
12-
client-id:
13-
client-secret:
14-
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat/callback
16+
client-id: wxdcb31cd7f1794ff4
17+
client-secret: b4e9dc6841ef7d2f520d449bca08ed6d
18+
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat/callback
19+
google:
20+
client-id: 716518501517-6dbdkapivhia806vqcjjh9nttj320ie3.apps.googleusercontent.com
21+
client-secret: 9IBornd7w1ALXhxZiDwEf7-E
22+
redirect-uri: http://oauth.xkcoding.com/demo/oauth/google/callback

0 commit comments

Comments
 (0)