mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-12-07 21:00:10 +08:00
feat(auth/justauth): 新增 JustAuth 自动配置
This commit is contained in:
@@ -28,10 +28,5 @@
|
||||
<groupId>me.zhyd.oauth</groupId>
|
||||
<artifactId>JustAuth</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.xkcoding.justauth</groupId>
|
||||
<artifactId>justauth-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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 <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
|
||||
* @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<String, JustAuthExtendProperties.ExtendRequestConfig> extendConfig = properties.getExtend().getConfig();
|
||||
Map<String, JustAuthExtendProperties.ExtendRequestConfig> 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<? extends AuthRequest> 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<String, JustAuthHttpProperties.JustAuthProxyConfig> 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());
|
||||
}
|
||||
}
|
||||
@@ -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 <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
|
||||
* @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
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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 <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
|
||||
* @author Charles7c
|
||||
* @since 2.15.0
|
||||
*/
|
||||
public class JustAuthCacheProperties {
|
||||
|
||||
/**
|
||||
* 缓存类型
|
||||
*/
|
||||
private CacheType type = CacheType.DEFAULT;
|
||||
|
||||
/**
|
||||
* 缓存前缀
|
||||
* <p>
|
||||
* 目前仅 {@link #type CacheType.REDIS} 缓存生效(默认:{@code JUSTAUTH::STATE::})
|
||||
* </p>
|
||||
*/
|
||||
private String prefix = "JUSTAUTH::STATE::";
|
||||
|
||||
/**
|
||||
* 超时时长
|
||||
* <p>
|
||||
* 目前仅 {@link #type CacheType.REDIS} 缓存生效(默认:3分钟)
|
||||
* </p>
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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 <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
|
||||
* @author Charles7c
|
||||
* @since 2.15.0
|
||||
*/
|
||||
public class JustAuthExtendProperties {
|
||||
|
||||
/**
|
||||
* 枚举类全路径
|
||||
*/
|
||||
private Class<? extends AuthSource> enumClass;
|
||||
|
||||
/**
|
||||
* 扩展请求配置
|
||||
*/
|
||||
private Map<String, ExtendRequestConfig> config = new HashMap<>();
|
||||
|
||||
public Class<? extends AuthSource> getEnumClass() {
|
||||
return enumClass;
|
||||
}
|
||||
|
||||
public void setEnumClass(Class<? extends AuthSource> enumClass) {
|
||||
this.enumClass = enumClass;
|
||||
}
|
||||
|
||||
public Map<String, ExtendRequestConfig> getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setConfig(Map<String, ExtendRequestConfig> config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 扩展请求配置
|
||||
*/
|
||||
public static class ExtendRequestConfig extends AuthConfig {
|
||||
|
||||
/**
|
||||
* 平台对应的 AuthRequest 实现类
|
||||
*/
|
||||
private Class<? extends AuthRequest> requestClass;
|
||||
|
||||
public Class<? extends AuthRequest> getRequestClass() {
|
||||
return requestClass;
|
||||
}
|
||||
|
||||
public void setRequestClass(Class<? extends AuthRequest> requestClass) {
|
||||
this.requestClass = requestClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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 <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
|
||||
* @author Charles7c
|
||||
* @since 2.15.0
|
||||
*/
|
||||
public class JustAuthHttpProperties {
|
||||
|
||||
/**
|
||||
* 超时时间(单位:毫秒)
|
||||
*/
|
||||
private int timeout;
|
||||
|
||||
/**
|
||||
* 代理配置
|
||||
*/
|
||||
private Map<String, JustAuthProxyConfig> proxy;
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public Map<String, JustAuthProxyConfig> getProxy() {
|
||||
return proxy;
|
||||
}
|
||||
|
||||
public void setProxy(Map<String, JustAuthProxyConfig> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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 <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
|
||||
* @author Charles7c
|
||||
* @since 2.15.0
|
||||
*/
|
||||
@ConfigurationProperties(PropertiesConstants.AUTH_JUSTAUTH)
|
||||
public class JustAuthProperties {
|
||||
|
||||
/**
|
||||
* 是否启用
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* 第三方平台配置
|
||||
*/
|
||||
private Map<String, AuthConfig> 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<String, AuthConfig> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Map<String, AuthConfig> 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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 <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a>
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1 @@
|
||||
top.continew.starter.auth.justauth.autoconfigure.JustAuthAutoConfiguration
|
||||
com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration
|
||||
top.continew.starter.auth.justauth.autoconfigure.JustAuthAutoConfiguration
|
||||
@@ -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";
|
||||
|
||||
/**
|
||||
* 加密配置
|
||||
*/
|
||||
|
||||
@@ -144,17 +144,6 @@
|
||||
<artifactId>JustAuth</artifactId>
|
||||
<version>${justauth.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.xkcoding.justauth</groupId>
|
||||
<artifactId>justauth-spring-boot-starter</artifactId>
|
||||
<version>1.4.0</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>me.zhyd.oauth</groupId>
|
||||
<artifactId>JustAuth</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- MyBatis Plus(MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,简化开发、提高效率) -->
|
||||
<dependency>
|
||||
|
||||
Reference in New Issue
Block a user