Skip to content

Commit 702eb58

Browse files
committed
✨ 支持GitHub第三方授权
1 parent 74a2952 commit 702eb58

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

data/sql/base/twelvet_nacos.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ VALUES (1, 'twelvet-app-dev.yml', 'DEFAULT_GROUP',
4848
'', 'eeb43899-8a88-4f5b-b0e0-d7c8fd09b86e', '公共配置', 'null', 'null', 'yaml', 'null', '');
4949
INSERT INTO `config_info`
5050
VALUES (2, 'twelvet-auth-dev.yml', 'DEFAULT_GROUP',
51-
'spring: \n # 模板引擎配置\n freemarker:\n allow-request-override: false\n allow-session-override: false\n check-template-location: true\n expose-request-attributes: false\n expose-session-attributes: false\n expose-spring-macro-helpers: true\n prefer-file-system-access: true\n # 后缀名\n suffix: .ftl \n content-type: text/html\n enabled: true\n # 缓存配置\n cache: true \n # 模板加载路径 按需配置\n template-loader-path: classpath:/templates/ \n charset: UTF-8\n \nswagger:\n title: TwelveT Swagger API\n version: 2.7.0\n description: 授权中心服务\n license: Powered By TwelveT\n licenseUrl: https://twelvet.cn\n terms-of-service-url: https://twelvet.cn\n contact:\n name: TwelveT\n email: [email protected]\n url: https://twelvet.cn\n authorization:\n name: password\n token-url-list:\n - http://${GATEWAY_HOST:localhost}:${GATEWAY_PORT:88}/auth/oauth/token',
52-
'4d55950e5699c672d41046b042d268f2', '2020-06-07 19:45:01', '2024-11-25 23:04:32', 'nacos', '0:0:0:0:0:0:0:1',
51+
'spring: \n # 模板引擎配置\n freemarker:\n allow-request-override: false\n allow-session-override: false\n check-template-location: true\n expose-request-attributes: false\n expose-session-attributes: false\n expose-spring-macro-helpers: true\n prefer-file-system-access: true\n # 后缀名\n suffix: .ftl \n content-type: text/html\n enabled: true\n # 缓存配置\n cache: true \n # 模板加载路径 按需配置\n template-loader-path: classpath:/templates/ \n charset: UTF-8\n \nswagger:\n title: TwelveT Swagger API\n version: 2.7.0\n description: 授权中心服务\n license: Powered By TwelveT\n licenseUrl: https://twelvet.cn\n terms-of-service-url: https://twelvet.cn\n contact:\n name: TwelveT\n email: [email protected]\n url: https://twelvet.cn\n authorization:\n name: password\n token-url-list:\n - http://${GATEWAY_HOST:localhost}:${GATEWAY_PORT:88}/auth/oauth/token\n\n# 第三方登录授权配置\noauth2:\n github:\n clientId: twelvet\n clientSecret: twelvet\n redirectUri: http://localhost:8000/login/oauth2/github',
52+
'9e2f36f2ab4d5e53fa1cf1cea469145b', '2020-06-07 19:45:01', '2025-01-27 19:29:18', 'nacos', '0:0:0:0:0:0:0:1',
5353
'', 'eeb43899-8a88-4f5b-b0e0-d7c8fd09b86e', '认证服务器配置', 'null', 'null', 'yaml', 'null', '');
5454
INSERT INTO `config_info`
5555
VALUES (3, 'twelvet-server-system-dev.yml', 'DEFAULT_GROUP',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.twelvet.auth.config;
2+
3+
import me.zhyd.oauth.cache.AuthCacheConfig;
4+
import me.zhyd.oauth.cache.AuthStateCache;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.redis.core.RedisTemplate;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.concurrent.TimeUnit;
10+
11+
/**
12+
* 扩展Redis版的state缓存
13+
*
14+
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
15+
* @version 1.0
16+
* @date 2019/10/24 13:38
17+
* @since 1.8
18+
*/
19+
@Component
20+
public class AuthStateRedisCacheConfiguration implements AuthStateCache {
21+
22+
@Autowired
23+
private RedisTemplate<String, String> redisTemplate;
24+
25+
/**
26+
* 存入缓存,默认3分钟
27+
* @param key 缓存key
28+
* @param value 缓存内容
29+
*/
30+
@Override
31+
public void cache(String key, String value) {
32+
redisTemplate.opsForValue().set(key, value, AuthCacheConfig.timeout, TimeUnit.MILLISECONDS);
33+
}
34+
35+
/**
36+
* 存入缓存
37+
* @param key 缓存key
38+
* @param value 缓存内容
39+
* @param timeout 指定缓存过期时间(毫秒)
40+
*/
41+
@Override
42+
public void cache(String key, String value, long timeout) {
43+
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MILLISECONDS);
44+
}
45+
46+
/**
47+
* 获取缓存内容
48+
* @param key 缓存key
49+
* @return 缓存内容
50+
*/
51+
@Override
52+
public String get(String key) {
53+
return redisTemplate.opsForValue().get(key);
54+
}
55+
56+
/**
57+
* 是否存在key,如果对应key的value值已过期,也返回false
58+
* @param key 缓存key
59+
* @return true:存在key,并且value没过期;false:key不存在或者已过期
60+
*/
61+
@Override
62+
public boolean containsKey(String key) {
63+
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
64+
}
65+
66+
}

twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/auth/config/Oauth2LoginConfiguration.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.twelvet.auth.config.properties.Oauth2LoginProperties;
44
import com.xkcoding.http.config.HttpConfig;
5+
import me.zhyd.oauth.cache.AuthStateCache;
56
import me.zhyd.oauth.config.AuthConfig;
67
import me.zhyd.oauth.request.AuthGithubRequest;
78
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -27,16 +28,18 @@ public class Oauth2LoginConfiguration {
2728
*/
2829
@Bean
2930
@ConditionalOnMissingBean
30-
public AuthGithubRequest authGithubRequest(Oauth2LoginProperties oauth2LoginProperties) {
31+
public AuthGithubRequest authGithubRequest(Oauth2LoginProperties oauth2LoginProperties,
32+
AuthStateCache authStateCache) {
3133
return new AuthGithubRequest(AuthConfig.builder()
3234
.clientId(oauth2LoginProperties.getGithub().getClientId())
3335
.clientSecret(oauth2LoginProperties.getGithub().getClientSecret())
3436
.redirectUri(oauth2LoginProperties.getGithub().getRedirectUri())
3537
.httpConfig(HttpConfig.builder()
3638
.timeout(15000)
37-
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)))
39+
// .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1",
40+
// 7890)))
3841
.build())
39-
.build());
42+
.build(), authStateCache);
4043
}
4144

4245
}

twelvet/twelvet-framework/twelvet-framework-security/src/main/java/com/twelvet/framework/security/support/service/manager/TwTUserDetailsServiceImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public UserDetails loadUserByUsername(String username) {
7979
public UserDetails loadUserByOAuth2UserId(Oauth2GrantEnums oauth2GrantEnums, String oAuth2UserId)
8080
throws UsernameNotFoundException {
8181
if (Oauth2GrantEnums.GITHUB.equals(oauth2GrantEnums)) { // GitHub
82+
// TODO 实现通过GitHub uuid进行绑定用户信息
8283
return loadUserByUsername(oAuth2UserId);
8384
}
8485
log.info("Oauth2GrantEnums:{} 不存在.", oauth2GrantEnums);

0 commit comments

Comments
 (0)