Skip to content

Commit 0798424

Browse files
committed
feat(auth/justauth): 新增 JustAuth 自动配置
1 parent be17196 commit 0798424

File tree

12 files changed

+685
-39
lines changed

12 files changed

+685
-39
lines changed

continew-starter-auth/continew-starter-auth-justauth/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,5 @@
2828
<groupId>me.zhyd.oauth</groupId>
2929
<artifactId>JustAuth</artifactId>
3030
</dependency>
31-
32-
<dependency>
33-
<groupId>com.xkcoding.justauth</groupId>
34-
<artifactId>justauth-spring-boot-starter</artifactId>
35-
</dependency>
3631
</dependencies>
3732
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.auth.justauth;
18+
19+
import cn.hutool.core.collection.CollUtil;
20+
import cn.hutool.core.util.EnumUtil;
21+
import cn.hutool.core.util.ReflectUtil;
22+
import cn.hutool.core.util.StrUtil;
23+
import com.xkcoding.http.config.HttpConfig;
24+
import me.zhyd.oauth.AuthRequestBuilder;
25+
import me.zhyd.oauth.cache.AuthStateCache;
26+
import me.zhyd.oauth.config.AuthConfig;
27+
import me.zhyd.oauth.config.AuthDefaultSource;
28+
import me.zhyd.oauth.config.AuthSource;
29+
import me.zhyd.oauth.enums.AuthResponseStatus;
30+
import me.zhyd.oauth.exception.AuthException;
31+
import me.zhyd.oauth.request.*;
32+
import top.continew.starter.auth.justauth.autoconfigure.JustAuthExtendProperties;
33+
import top.continew.starter.auth.justauth.autoconfigure.JustAuthHttpProperties;
34+
import top.continew.starter.auth.justauth.autoconfigure.JustAuthProperties;
35+
36+
import java.net.InetSocketAddress;
37+
import java.net.Proxy;
38+
import java.util.HashMap;
39+
import java.util.Map;
40+
41+
/**
42+
* AuthRequest 工厂类
43+
*
44+
* @author <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
45+
* @author Charles7c
46+
* @since 2.15.0
47+
*/
48+
public class AuthRequestFactory {
49+
50+
private final JustAuthProperties properties;
51+
private final AuthStateCache stateCache;
52+
53+
public AuthRequestFactory(JustAuthProperties properties, AuthStateCache stateCache) {
54+
this.properties = properties;
55+
this.stateCache = stateCache;
56+
}
57+
58+
/**
59+
* 获取 AuthRequest
60+
*
61+
* @param source {@link AuthSource}
62+
* @return {@link AuthRequest}
63+
*/
64+
public AuthRequest getAuthRequest(String source) {
65+
if (StrUtil.isBlank(source)) {
66+
throw new AuthException(AuthResponseStatus.NO_AUTH_SOURCE);
67+
}
68+
69+
// 获取内置 AuthRequest
70+
AuthRequest authRequest = this.getDefaultAuthRequest(source);
71+
72+
// 获取自定义 AuthRequest
73+
if (authRequest == null) {
74+
authRequest = this.getExtendAuthRequest(properties.getExtend().getEnumClass(), source);
75+
}
76+
77+
if (authRequest == null) {
78+
throw new AuthException(AuthResponseStatus.UNSUPPORTED);
79+
}
80+
return authRequest;
81+
}
82+
83+
/**
84+
* 获取自定义 AuthRequest
85+
*
86+
* @param clazz 枚举类 {@link AuthSource}
87+
* @param source {@link AuthSource}
88+
* @return {@link AuthRequest}
89+
*/
90+
@SuppressWarnings({"unchecked", "rawtypes"})
91+
private AuthRequest getExtendAuthRequest(Class clazz, String source) {
92+
String upperSource = source.toUpperCase();
93+
try {
94+
EnumUtil.fromString(clazz, upperSource);
95+
} catch (IllegalArgumentException e) {
96+
// 无自定义匹配
97+
return null;
98+
}
99+
100+
Map<String, JustAuthExtendProperties.ExtendRequestConfig> extendConfig = properties.getExtend().getConfig();
101+
Map<String, JustAuthExtendProperties.ExtendRequestConfig> upperConfig = new HashMap<>(6);
102+
extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v));
103+
JustAuthExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(upperSource);
104+
if (extendRequestConfig != null) {
105+
// 配置 HTTP
106+
this.configureHttpConfig(upperSource, extendRequestConfig, properties.getHttp());
107+
108+
Class<? extends AuthRequest> requestClass = extendRequestConfig.getRequestClass();
109+
110+
if (requestClass != null) {
111+
// 反射获取 Request 对象,所以必须实现 2 个参数的构造方法
112+
return ReflectUtil.newInstance(requestClass, extendRequestConfig, stateCache);
113+
}
114+
}
115+
return null;
116+
}
117+
118+
/**
119+
* 获取内置 AuthRequest
120+
*
121+
* @param source {@link AuthSource}
122+
* @return {@link AuthRequest}
123+
*/
124+
private AuthRequest getDefaultAuthRequest(String source) {
125+
AuthDefaultSource authSource;
126+
try {
127+
authSource = EnumUtil.fromString(AuthDefaultSource.class, source.toUpperCase());
128+
} catch (IllegalArgumentException e) {
129+
// 无自定义匹配
130+
return null;
131+
}
132+
133+
AuthConfig config = properties.getType().get(authSource.name());
134+
// 找不到对应关系,直接返回空
135+
if (config == null) {
136+
return null;
137+
}
138+
139+
// 配置 HTTP
140+
this.configureHttpConfig(authSource.name(), config, properties.getHttp());
141+
return AuthRequestBuilder.builder().source(source).authConfig(config).build();
142+
}
143+
144+
/**
145+
* 配置 HTTP 相关的配置
146+
*
147+
* @param authSource {@link AuthSource}
148+
* @param authConfig {@link AuthConfig}
149+
* @param httpConfig {@link JustAuthHttpProperties}
150+
*/
151+
private void configureHttpConfig(String authSource, AuthConfig authConfig, JustAuthHttpProperties httpConfig) {
152+
if (null == httpConfig) {
153+
return;
154+
}
155+
156+
Map<String, JustAuthHttpProperties.JustAuthProxyConfig> proxyConfigMap = httpConfig.getProxy();
157+
if (CollUtil.isEmpty(proxyConfigMap)) {
158+
return;
159+
}
160+
161+
JustAuthHttpProperties.JustAuthProxyConfig proxyConfig = proxyConfigMap.get(authSource);
162+
if (null == proxyConfig) {
163+
return;
164+
}
165+
166+
authConfig.setHttpConfig(HttpConfig.builder()
167+
.timeout(httpConfig.getTimeout())
168+
.proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig
169+
.getHostname(), proxyConfig.getPort())))
170+
.build());
171+
}
172+
}

continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,46 @@
1818

1919
import jakarta.annotation.PostConstruct;
2020
import me.zhyd.oauth.cache.AuthStateCache;
21-
import org.redisson.client.RedisClient;
2221
import org.slf4j.Logger;
2322
import org.slf4j.LoggerFactory;
2423
import org.springframework.boot.autoconfigure.AutoConfiguration;
25-
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2624
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
25+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2726
import org.springframework.context.annotation.Bean;
28-
import top.continew.starter.auth.justauth.core.AuthStateCacheRedisDefaultImpl;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.context.annotation.Import;
29+
import top.continew.starter.auth.justauth.AuthRequestFactory;
2930
import top.continew.starter.core.constant.PropertiesConstants;
3031

3132
/**
3233
* JustAuth 自动配置
3334
*
3435
* @author Charles7c
36+
* @author <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
3537
* @since 1.0.0
3638
*/
37-
@AutoConfiguration(before = com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration.class)
38-
@ConditionalOnProperty(prefix = "justauth", name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
39+
@AutoConfiguration
40+
@EnableConfigurationProperties(JustAuthProperties.class)
41+
@ConditionalOnProperty(prefix = PropertiesConstants.AUTH_JUSTAUTH, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true)
3942
public class JustAuthAutoConfiguration {
4043

4144
private static final Logger log = LoggerFactory.getLogger(JustAuthAutoConfiguration.class);
4245

4346
/**
44-
* State 缓存 Redis 实现(默认)
47+
* AuthRequest 工厂配置
4548
*/
4649
@Bean
47-
@ConditionalOnClass(RedisClient.class)
48-
@ConditionalOnProperty(prefix = "justauth.cache", name = "type", havingValue = "redis")
49-
public AuthStateCache authStateCache() {
50-
AuthStateCacheRedisDefaultImpl impl = new AuthStateCacheRedisDefaultImpl();
51-
log.debug("[ContiNew Starter] - Auto Configuration 'JustAuth-AuthStateCache-Redis' completed initialization.");
52-
return impl;
50+
public AuthRequestFactory authRequestFactory(JustAuthProperties properties, AuthStateCache stateCache) {
51+
return new AuthRequestFactory(properties, stateCache);
52+
}
53+
54+
/**
55+
* 缓存自动配置
56+
*/
57+
@Configuration
58+
@Import({JustAuthStateCacheConfiguration.Default.class, JustAuthStateCacheConfiguration.Redis.class,
59+
JustAuthStateCacheConfiguration.Custom.class})
60+
protected static class AuthStateCacheAutoConfiguration {
5361
}
5462

5563
@PostConstruct
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.auth.justauth.autoconfigure;
18+
19+
import java.time.Duration;
20+
21+
/**
22+
* JustAuth 缓存配置属性
23+
*
24+
* @author <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
25+
* @author Charles7c
26+
* @since 2.15.0
27+
*/
28+
public class JustAuthCacheProperties {
29+
30+
/**
31+
* 缓存类型
32+
*/
33+
private CacheType type = CacheType.DEFAULT;
34+
35+
/**
36+
* 缓存前缀
37+
* <p>
38+
* 目前仅 {@link #type CacheType.REDIS} 缓存生效(默认:{@code JUSTAUTH::STATE::})
39+
* </p>
40+
*/
41+
private String prefix = "JUSTAUTH::STATE::";
42+
43+
/**
44+
* 超时时长
45+
* <p>
46+
* 目前仅 {@link #type CacheType.REDIS} 缓存生效(默认:3分钟)
47+
* </p>
48+
*/
49+
private Duration timeout = Duration.ofMinutes(3);
50+
51+
public CacheType getType() {
52+
return type;
53+
}
54+
55+
public void setType(CacheType type) {
56+
this.type = type;
57+
}
58+
59+
public String getPrefix() {
60+
return prefix;
61+
}
62+
63+
public void setPrefix(String prefix) {
64+
this.prefix = prefix;
65+
}
66+
67+
public Duration getTimeout() {
68+
return timeout;
69+
}
70+
71+
public void setTimeout(Duration timeout) {
72+
this.timeout = timeout;
73+
}
74+
75+
/**
76+
* 缓存类型枚举
77+
*/
78+
public enum CacheType {
79+
80+
/**
81+
* 使用 JustAuth 内置缓存
82+
*/
83+
DEFAULT,
84+
85+
/**
86+
* 使用 Redis 缓存
87+
*/
88+
REDIS,
89+
90+
/**
91+
* 自定义缓存
92+
*/
93+
CUSTOM
94+
}
95+
}

0 commit comments

Comments
 (0)