From 0798424dc86887711f2708d4c84cc399b46781d1 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Tue, 2 Dec 2025 22:21:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth/justauth):=20=E6=96=B0=E5=A2=9E=20Jus?= =?UTF-8?q?tAuth=20=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../continew-starter-auth-justauth/pom.xml | 5 - .../auth/justauth/AuthRequestFactory.java | 172 ++++++++++++++++++ .../JustAuthAutoConfiguration.java | 32 ++-- .../JustAuthCacheProperties.java | 95 ++++++++++ .../JustAuthExtendProperties.java | 79 ++++++++ .../autoconfigure/JustAuthHttpProperties.java | 101 ++++++++++ .../autoconfigure/JustAuthProperties.java | 103 +++++++++++ .../JustAuthStateCacheConfiguration.java | 96 ++++++++++ .../RedisAuthStateCache.java} | 22 ++- ...ot.autoconfigure.AutoConfiguration.imports | 3 +- .../core/constant/PropertiesConstants.java | 5 + continew-starter-dependencies/pom.xml | 11 -- 12 files changed, 685 insertions(+), 39 deletions(-) create mode 100644 continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/AuthRequestFactory.java create mode 100644 continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthCacheProperties.java create mode 100644 continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthExtendProperties.java create mode 100644 continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthHttpProperties.java create mode 100644 continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthProperties.java create mode 100644 continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthStateCacheConfiguration.java rename continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/{core/AuthStateCacheRedisDefaultImpl.java => state/RedisAuthStateCache.java} (68%) diff --git a/continew-starter-auth/continew-starter-auth-justauth/pom.xml b/continew-starter-auth/continew-starter-auth-justauth/pom.xml index db2199a7..fcdeaf81 100644 --- a/continew-starter-auth/continew-starter-auth-justauth/pom.xml +++ b/continew-starter-auth/continew-starter-auth-justauth/pom.xml @@ -28,10 +28,5 @@ me.zhyd.oauth JustAuth - - - com.xkcoding.justauth - justauth-spring-boot-starter - \ No newline at end of file diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/AuthRequestFactory.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/AuthRequestFactory.java new file mode 100644 index 00000000..075b1b95 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/AuthRequestFactory.java @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.auth.justauth; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.EnumUtil; +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; +import com.xkcoding.http.config.HttpConfig; +import me.zhyd.oauth.AuthRequestBuilder; +import me.zhyd.oauth.cache.AuthStateCache; +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthDefaultSource; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.enums.AuthResponseStatus; +import me.zhyd.oauth.exception.AuthException; +import me.zhyd.oauth.request.*; +import top.continew.starter.auth.justauth.autoconfigure.JustAuthExtendProperties; +import top.continew.starter.auth.justauth.autoconfigure.JustAuthHttpProperties; +import top.continew.starter.auth.justauth.autoconfigure.JustAuthProperties; + +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.util.HashMap; +import java.util.Map; + +/** + * AuthRequest 工厂类 + * + * @author yangkai.shen + * @author Charles7c + * @since 2.15.0 + */ +public class AuthRequestFactory { + + private final JustAuthProperties properties; + private final AuthStateCache stateCache; + + public AuthRequestFactory(JustAuthProperties properties, AuthStateCache stateCache) { + this.properties = properties; + this.stateCache = stateCache; + } + + /** + * 获取 AuthRequest + * + * @param source {@link AuthSource} + * @return {@link AuthRequest} + */ + public AuthRequest getAuthRequest(String source) { + if (StrUtil.isBlank(source)) { + throw new AuthException(AuthResponseStatus.NO_AUTH_SOURCE); + } + + // 获取内置 AuthRequest + AuthRequest authRequest = this.getDefaultAuthRequest(source); + + // 获取自定义 AuthRequest + if (authRequest == null) { + authRequest = this.getExtendAuthRequest(properties.getExtend().getEnumClass(), source); + } + + if (authRequest == null) { + throw new AuthException(AuthResponseStatus.UNSUPPORTED); + } + return authRequest; + } + + /** + * 获取自定义 AuthRequest + * + * @param clazz 枚举类 {@link AuthSource} + * @param source {@link AuthSource} + * @return {@link AuthRequest} + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + private AuthRequest getExtendAuthRequest(Class clazz, String source) { + String upperSource = source.toUpperCase(); + try { + EnumUtil.fromString(clazz, upperSource); + } catch (IllegalArgumentException e) { + // 无自定义匹配 + return null; + } + + Map extendConfig = properties.getExtend().getConfig(); + Map upperConfig = new HashMap<>(6); + extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v)); + JustAuthExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(upperSource); + if (extendRequestConfig != null) { + // 配置 HTTP + this.configureHttpConfig(upperSource, extendRequestConfig, properties.getHttp()); + + Class requestClass = extendRequestConfig.getRequestClass(); + + if (requestClass != null) { + // 反射获取 Request 对象,所以必须实现 2 个参数的构造方法 + return ReflectUtil.newInstance(requestClass, extendRequestConfig, stateCache); + } + } + return null; + } + + /** + * 获取内置 AuthRequest + * + * @param source {@link AuthSource} + * @return {@link AuthRequest} + */ + private AuthRequest getDefaultAuthRequest(String source) { + AuthDefaultSource authSource; + try { + authSource = EnumUtil.fromString(AuthDefaultSource.class, source.toUpperCase()); + } catch (IllegalArgumentException e) { + // 无自定义匹配 + return null; + } + + AuthConfig config = properties.getType().get(authSource.name()); + // 找不到对应关系,直接返回空 + if (config == null) { + return null; + } + + // 配置 HTTP + this.configureHttpConfig(authSource.name(), config, properties.getHttp()); + return AuthRequestBuilder.builder().source(source).authConfig(config).build(); + } + + /** + * 配置 HTTP 相关的配置 + * + * @param authSource {@link AuthSource} + * @param authConfig {@link AuthConfig} + * @param httpConfig {@link JustAuthHttpProperties} + */ + private void configureHttpConfig(String authSource, AuthConfig authConfig, JustAuthHttpProperties httpConfig) { + if (null == httpConfig) { + return; + } + + Map proxyConfigMap = httpConfig.getProxy(); + if (CollUtil.isEmpty(proxyConfigMap)) { + return; + } + + JustAuthHttpProperties.JustAuthProxyConfig proxyConfig = proxyConfigMap.get(authSource); + if (null == proxyConfig) { + return; + } + + authConfig.setHttpConfig(HttpConfig.builder() + .timeout(httpConfig.getTimeout()) + .proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig + .getHostname(), proxyConfig.getPort()))) + .build()); + } +} diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java index 3cbcd1be..42560a61 100644 --- a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java @@ -18,38 +18,46 @@ package top.continew.starter.auth.justauth.autoconfigure; import jakarta.annotation.PostConstruct; import me.zhyd.oauth.cache.AuthStateCache; -import org.redisson.client.RedisClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.AutoConfiguration; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import top.continew.starter.auth.justauth.core.AuthStateCacheRedisDefaultImpl; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import top.continew.starter.auth.justauth.AuthRequestFactory; import top.continew.starter.core.constant.PropertiesConstants; /** * JustAuth 自动配置 * * @author Charles7c + * @author yangkai.shen * @since 1.0.0 */ -@AutoConfiguration(before = com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration.class) -@ConditionalOnProperty(prefix = "justauth", name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true) +@AutoConfiguration +@EnableConfigurationProperties(JustAuthProperties.class) +@ConditionalOnProperty(prefix = PropertiesConstants.AUTH_JUSTAUTH, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true) public class JustAuthAutoConfiguration { private static final Logger log = LoggerFactory.getLogger(JustAuthAutoConfiguration.class); /** - * State 缓存 Redis 实现(默认) + * AuthRequest 工厂配置 */ @Bean - @ConditionalOnClass(RedisClient.class) - @ConditionalOnProperty(prefix = "justauth.cache", name = "type", havingValue = "redis") - public AuthStateCache authStateCache() { - AuthStateCacheRedisDefaultImpl impl = new AuthStateCacheRedisDefaultImpl(); - log.debug("[ContiNew Starter] - Auto Configuration 'JustAuth-AuthStateCache-Redis' completed initialization."); - return impl; + public AuthRequestFactory authRequestFactory(JustAuthProperties properties, AuthStateCache stateCache) { + return new AuthRequestFactory(properties, stateCache); + } + + /** + * 缓存自动配置 + */ + @Configuration + @Import({JustAuthStateCacheConfiguration.Default.class, JustAuthStateCacheConfiguration.Redis.class, + JustAuthStateCacheConfiguration.Custom.class}) + protected static class AuthStateCacheAutoConfiguration { } @PostConstruct diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthCacheProperties.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthCacheProperties.java new file mode 100644 index 00000000..faa49e2b --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthCacheProperties.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.auth.justauth.autoconfigure; + +import java.time.Duration; + +/** + * JustAuth 缓存配置属性 + * + * @author yangkai.shen + * @author Charles7c + * @since 2.15.0 + */ +public class JustAuthCacheProperties { + + /** + * 缓存类型 + */ + private CacheType type = CacheType.DEFAULT; + + /** + * 缓存前缀 + *

+ * 目前仅 {@link #type CacheType.REDIS} 缓存生效(默认:{@code JUSTAUTH::STATE::}) + *

+ */ + private String prefix = "JUSTAUTH::STATE::"; + + /** + * 超时时长 + *

+ * 目前仅 {@link #type CacheType.REDIS} 缓存生效(默认:3分钟) + *

+ */ + private Duration timeout = Duration.ofMinutes(3); + + public CacheType getType() { + return type; + } + + public void setType(CacheType type) { + this.type = type; + } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public Duration getTimeout() { + return timeout; + } + + public void setTimeout(Duration timeout) { + this.timeout = timeout; + } + + /** + * 缓存类型枚举 + */ + public enum CacheType { + + /** + * 使用 JustAuth 内置缓存 + */ + DEFAULT, + + /** + * 使用 Redis 缓存 + */ + REDIS, + + /** + * 自定义缓存 + */ + CUSTOM + } +} diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthExtendProperties.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthExtendProperties.java new file mode 100644 index 00000000..cf33f066 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthExtendProperties.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.auth.justauth.autoconfigure; + +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.request.AuthRequest; + +import java.util.HashMap; +import java.util.Map; + +/** + * JustAuth 扩展配置属性 + * + * @author yangkai.shen + * @author Charles7c + * @since 2.15.0 + */ +public class JustAuthExtendProperties { + + /** + * 枚举类全路径 + */ + private Class enumClass; + + /** + * 扩展请求配置 + */ + private Map config = new HashMap<>(); + + public Class getEnumClass() { + return enumClass; + } + + public void setEnumClass(Class enumClass) { + this.enumClass = enumClass; + } + + public Map getConfig() { + return config; + } + + public void setConfig(Map config) { + this.config = config; + } + + /** + * 扩展请求配置 + */ + public static class ExtendRequestConfig extends AuthConfig { + + /** + * 平台对应的 AuthRequest 实现类 + */ + private Class requestClass; + + public Class getRequestClass() { + return requestClass; + } + + public void setRequestClass(Class requestClass) { + this.requestClass = requestClass; + } + } +} diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthHttpProperties.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthHttpProperties.java new file mode 100644 index 00000000..8f4bb4ba --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthHttpProperties.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.auth.justauth.autoconfigure; + +import java.net.Proxy; +import java.util.Map; + +/** + * JustAuth HTTP 配置属性 + * + * @author yangkai.shen + * @author Charles7c + * @since 2.15.0 + */ +public class JustAuthHttpProperties { + + /** + * 超时时间(单位:毫秒) + */ + private int timeout; + + /** + * 代理配置 + */ + private Map proxy; + + public int getTimeout() { + return timeout; + } + + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + public Map getProxy() { + return proxy; + } + + public void setProxy(Map proxy) { + this.proxy = proxy; + } + + /** + * 代理配置 + */ + public static class JustAuthProxyConfig { + + /** + * 代理类型 + */ + private String type = Proxy.Type.HTTP.name(); + + /** + * 代理主机名 + */ + private String hostname; + + /** + * 代理端口号 + */ + private int port; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getHostname() { + return hostname; + } + + public void setHostname(String hostname) { + this.hostname = hostname; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + } +} diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthProperties.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthProperties.java new file mode 100644 index 00000000..c2f3f4a4 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthProperties.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.auth.justauth.autoconfigure; + +import me.zhyd.oauth.config.AuthConfig; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; +import top.continew.starter.core.constant.PropertiesConstants; + +import java.util.Map; + +/** + * JustAuth 配置属性 + * + * @author yangkai.shen + * @author Charles7c + * @since 2.15.0 + */ +@ConfigurationProperties(PropertiesConstants.AUTH_JUSTAUTH) +public class JustAuthProperties { + + /** + * 是否启用 + */ + private boolean enabled = true; + + /** + * 第三方平台配置 + */ + private Map type; + + /** + * 自定义配置 + */ + @NestedConfigurationProperty + private JustAuthExtendProperties extend; + + /** + * 缓存配置 + */ + @NestedConfigurationProperty + private JustAuthCacheProperties cache; + + /** + * HTTP 配置 + */ + @NestedConfigurationProperty + private JustAuthHttpProperties http; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public Map getType() { + return type; + } + + public void setType(Map type) { + this.type = type; + } + + public JustAuthExtendProperties getExtend() { + return extend; + } + + public void setExtend(JustAuthExtendProperties extend) { + this.extend = extend; + } + + public JustAuthCacheProperties getCache() { + return cache; + } + + public void setCache(JustAuthCacheProperties cache) { + this.cache = cache; + } + + public JustAuthHttpProperties getHttp() { + return http; + } + + public void setHttp(JustAuthHttpProperties http) { + this.http = http; + } +} diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthStateCacheConfiguration.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthStateCacheConfiguration.java new file mode 100644 index 00000000..9d2033e3 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/autoconfigure/JustAuthStateCacheConfiguration.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.auth.justauth.autoconfigure; + +import me.zhyd.oauth.cache.AuthDefaultStateCache; +import me.zhyd.oauth.cache.AuthStateCache; +import org.redisson.client.RedisClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.core.ResolvableType; +import top.continew.starter.auth.justauth.state.RedisAuthStateCache; +import top.continew.starter.core.constant.PropertiesConstants; + +/** + * JustAuth 缓存配置 + * + * @author yangkai.shen + * @author Charles7c + * @since 2.15.0 + */ +abstract class JustAuthStateCacheConfiguration { + + private static final Logger log = LoggerFactory.getLogger(JustAuthStateCacheConfiguration.class); + + /** + * Redis 缓存 + */ + @ConditionalOnClass(RedisClient.class) + @ConditionalOnMissingBean(AuthStateCache.class) + @ConditionalOnProperty(prefix = PropertiesConstants.AUTH_JUSTAUTH, name = "cache.type", havingValue = "redis") + static class Redis { + static { + log.debug("[ContiNew Starter] - Auto Configuration 'JustAuth-AuthStateCache-Redis' completed initialization."); + } + + @Bean + public AuthStateCache authStateCache(JustAuthProperties properties) { + return new RedisAuthStateCache(properties.getCache()); + } + } + + /** + * 默认缓存 + */ + @ConditionalOnMissingBean(AuthStateCache.class) + @ConditionalOnProperty(prefix = PropertiesConstants.AUTH_JUSTAUTH, name = "cache.type", havingValue = "default", matchIfMissing = true) + static class Default { + static { + log.debug("[ContiNew Starter] - Auto Configuration 'JustAuth-AuthStateCache-Default' completed initialization."); + } + + @Bean + public AuthStateCache authStateCache() { + return AuthDefaultStateCache.INSTANCE; + } + } + + /** + * 自定义缓存 + */ + @ConditionalOnProperty(prefix = PropertiesConstants.AUTH_JUSTAUTH, name = "cache.type", havingValue = "custom") + static class Custom { + static { + log.debug("[ContiNew Starter] - Auto Configuration 'JustAuth-AuthStateCache-Custom' completed initialization."); + } + + @Bean + @ConditionalOnMissingBean(AuthStateCache.class) + public AuthStateCache authStateCache() { + if (log.isErrorEnabled()) { + log.error("Consider defining a bean of type '{}' in your configuration.", ResolvableType + .forClass(AuthStateCache.class)); + } + throw new NoSuchBeanDefinitionException(AuthStateCache.class); + } + } +} diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/core/AuthStateCacheRedisDefaultImpl.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/state/RedisAuthStateCache.java similarity index 68% rename from continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/core/AuthStateCacheRedisDefaultImpl.java rename to continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/state/RedisAuthStateCache.java index 632f4583..3d40ce1f 100644 --- a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/core/AuthStateCacheRedisDefaultImpl.java +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/continew/starter/auth/justauth/state/RedisAuthStateCache.java @@ -14,22 +14,27 @@ * limitations under the License. */ -package top.continew.starter.auth.justauth.core; +package top.continew.starter.auth.justauth.state; import me.zhyd.oauth.cache.AuthStateCache; +import top.continew.starter.auth.justauth.autoconfigure.JustAuthCacheProperties; import top.continew.starter.cache.redisson.util.RedisUtils; import java.time.Duration; /** - * 默认 State 缓存 Redis 实现 + * Redis State 缓存实现 * * @author Charles7c * @since 1.0.0 */ -public class AuthStateCacheRedisDefaultImpl implements AuthStateCache { +public class RedisAuthStateCache implements AuthStateCache { - private static final String KEY_PREFIX = "SOCIAL_AUTH_STATE"; + public final JustAuthCacheProperties cacheProperties; + + public RedisAuthStateCache(JustAuthCacheProperties cacheProperties) { + this.cacheProperties = cacheProperties; + } /** * 存入缓存 @@ -39,8 +44,7 @@ public class AuthStateCacheRedisDefaultImpl implements AuthStateCache { */ @Override public void cache(String key, String value) { - // 参考:在 JustAuth 中,内置了一个基于 map 的 state 缓存器,默认缓存有效期为 3 分钟 - RedisUtils.set(RedisUtils.formatKey(KEY_PREFIX, key), value, Duration.ofMinutes(3)); + this.cache(key, value, cacheProperties.getTimeout().toMillis()); } /** @@ -52,7 +56,7 @@ public class AuthStateCacheRedisDefaultImpl implements AuthStateCache { */ @Override public void cache(String key, String value, long timeout) { - RedisUtils.set(RedisUtils.formatKey(KEY_PREFIX, key), value, Duration.ofMillis(timeout)); + RedisUtils.set(RedisUtils.formatKey(cacheProperties.getPrefix(), key), value, Duration.ofMillis(timeout)); } /** @@ -63,7 +67,7 @@ public class AuthStateCacheRedisDefaultImpl implements AuthStateCache { */ @Override public String get(String key) { - return RedisUtils.get(RedisUtils.formatKey(KEY_PREFIX, key)); + return RedisUtils.get(RedisUtils.formatKey(cacheProperties.getPrefix(), key)); } /** @@ -74,6 +78,6 @@ public class AuthStateCacheRedisDefaultImpl implements AuthStateCache { */ @Override public boolean containsKey(String key) { - return RedisUtils.exists(RedisUtils.formatKey(KEY_PREFIX, key)); + return RedisUtils.exists(RedisUtils.formatKey(cacheProperties.getPrefix(), key)); } } \ No newline at end of file diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-auth/continew-starter-auth-justauth/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 48a9ed1d..ae821c93 100644 --- a/continew-starter-auth/continew-starter-auth-justauth/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,2 +1 @@ -top.continew.starter.auth.justauth.autoconfigure.JustAuthAutoConfiguration -com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration \ No newline at end of file +top.continew.starter.auth.justauth.autoconfigure.JustAuthAutoConfiguration \ No newline at end of file diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java b/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java index 5eb3f7cf..5b2b6c6f 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/constant/PropertiesConstants.java @@ -49,6 +49,11 @@ public class PropertiesConstants { */ public static final String WEB_RESPONSE = WEB + StringConstants.DOT + "response"; + /** + * 认证-JustAuth 配置 + */ + public static final String AUTH_JUSTAUTH = CONTINEW_STARTER + StringConstants.DOT + "justauth"; + /** * 加密配置 */ diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 6f173371..5ff171a2 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -144,17 +144,6 @@ JustAuth ${justauth.version} - - com.xkcoding.justauth - justauth-spring-boot-starter - 1.4.0 - - - me.zhyd.oauth - JustAuth - - -