refactor: 优化 SaToken 自动配置(认证模块),新增持久层 Redis 实现

This commit is contained in:
2023-11-26 11:43:19 +08:00
parent c59323e611
commit bcd9f91152
9 changed files with 367 additions and 44 deletions

View File

@@ -26,11 +26,5 @@
<groupId>com.xkcoding.justauth</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
</dependency>
<!-- 缓存模块 - Redisson -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-cache-redisson</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -16,7 +16,6 @@
package top.charles7c.continew.starter.auth.satoken.autoconfigure;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.stp.StpInterface;
@@ -31,6 +30,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -60,24 +61,7 @@ public class SaTokenAutoConfiguration implements WebMvcConfigurer {
}
/**
* 整合 JWT简单模式
*/
@Bean
public StpLogic stpLogic() {
return new StpLogicJwtForSimple();
}
/**
* 自定义缓存实现
*/
@Bean
@ConditionalOnMissingBean
public SaTokenDao saTokenDao() {
return ReflectUtil.newInstance(properties.getDaoImpl());
}
/**
* 权限认证实现
* 权限认证实现类
*/
@Bean
@ConditionalOnMissingBean
@@ -85,6 +69,21 @@ public class SaTokenAutoConfiguration implements WebMvcConfigurer {
return ReflectUtil.newInstance(properties.getPermissionImpl());
}
/**
* 自定义持久层配置
*/
@Configuration
@Import({SaTokenDaoConfiguration.Redis.class, SaTokenDaoConfiguration.Custom.class})
protected static class SaTokenDaoAutoConfiguration {}
/**
* 整合 JWT简单模式
*/
@Bean
public StpLogic stpLogic() {
return new StpLogicJwtForSimple();
}
@PostConstruct
public void postConstruct() {
log.info("[ContiNew Starter] - Auto Configuration 'SaToken' completed initialization.");

View File

@@ -0,0 +1,75 @@
/*
* 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.charles7c.continew.starter.auth.satoken.autoconfigure;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.hutool.core.util.ReflectUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.client.RedisClient;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
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 top.charles7c.continew.starter.auth.satoken.impl.SaTokenDaoRedisImpl;
import top.charles7c.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration;
/**
* SaToken 持久层配置
*
* @author Charles7c
* @since 1.0.0
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public abstract class SaTokenDaoConfiguration {
/**
* 自定义持久层实现类-Redis
*/
@ConditionalOnClass(RedisClient.class)
@ConditionalOnMissingBean(SaTokenDao.class)
@AutoConfigureBefore(RedissonAutoConfiguration.class)
@ConditionalOnProperty(name = "sa-token.extension.dao.type", havingValue = "redis")
static class Redis {
static {
log.debug("[ContiNew Starter] - Auto Configuration 'SaToken-SaTokenDao-Redis' completed initialization.");
}
@Bean
public SaTokenDao saTokenDao() {
return new SaTokenDaoRedisImpl();
}
}
/**
* 自定义持久层实现类-自定义
*/
@ConditionalOnProperty(name = "sa-token.extension.dao.type", havingValue = "custom")
static class Custom {
static {
log.debug("[ContiNew Starter] - Auto Configuration 'SaToken-SaTokenDao-Custom' completed initialization.");
}
@Bean
public SaTokenDao saTokenDao(SaTokenExtensionProperties properties) {
return ReflectUtil.newInstance(properties.getDao().getImpl());
}
}
}

View File

@@ -16,10 +16,12 @@
package top.charles7c.continew.starter.auth.satoken.autoconfigure;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.stp.StpInterface;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import top.charles7c.continew.starter.auth.satoken.properties.SaTokenDaoProperties;
import top.charles7c.continew.starter.auth.satoken.properties.SaTokenSecurityProperties;
/**
@@ -37,30 +39,20 @@ public class SaTokenExtensionProperties {
*/
private boolean enabled = false;
/**
* 自定义缓存实现
*/
private Class<? extends SaTokenDao> daoImpl;
/**
* 权限认证实现
*/
private Class<? extends StpInterface> permissionImpl;
/**
* 安全配置
* 持久层配置
*/
private SecurityProperties security;
@NestedConfigurationProperty
private SaTokenDaoProperties dao;
/**
* 安全配置属性
* 安全配置
*/
@Data
public static class SecurityProperties {
/**
* 排除(放行)路径配置
*/
private String[] excludes = new String[0];
}
@NestedConfigurationProperty
private SaTokenSecurityProperties security;
}

View File

@@ -0,0 +1,36 @@
/*
* 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.charles7c.continew.starter.auth.satoken.enums;
/**
* SaToken 持久层类型枚举
*
* @author Charles7c
* @since 1.0.0
*/
public enum SaTokenDaoTypeEnum {
/**
* Redis
*/
REDIS,
/**
* 自定义
*/
CUSTOM
}

View File

@@ -0,0 +1,152 @@
/*
* 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.charles7c.continew.starter.auth.satoken.impl;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.util.SaFoxUtil;
import top.charles7c.continew.starter.cache.redisson.util.RedisUtils;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Sa-Token 持久层 Redis 实现参考Sa-Token/sa-token-plugin/sa-token-dao-redisx/SaTokenDaoOfRedis.java
*
* @author Charles7c
* @since 1.0.0
*/
public class SaTokenDaoRedisImpl implements SaTokenDao {
@Override
public String get(String key) {
return RedisUtils.get(key);
}
@Override
public void set(String key, String value, long timeout) {
if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {
return;
}
// 判断是否为永不过期
if (timeout == SaTokenDao.NEVER_EXPIRE) {
RedisUtils.set(key, value);
} else {
RedisUtils.set(key, value, Duration.ofSeconds(timeout));
}
}
@Override
public void update(String key, String value) {
long expire = getTimeout(key);
// -2无此键
if (expire == SaTokenDao.NOT_VALUE_EXPIRE) {
return;
}
this.set(key, value, expire);
}
@Override
public void delete(String key) {
RedisUtils.delete(key);
}
@Override
public long getTimeout(String key) {
long timeout = RedisUtils.getTimeToLive(key);
return timeout < 0 ? timeout : timeout / 1000;
}
@Override
public void updateTimeout(String key, long timeout) {
// 判断是否想要设置为永久
if (timeout == SaTokenDao.NEVER_EXPIRE) {
long expire = getTimeout(key);
if (expire == SaTokenDao.NEVER_EXPIRE) {
// 如果其已经被设置为永久,则不作任何处理
} else {
// 如果尚未被设置为永久,那么再次 set 一次
this.set(key, this.get(key), timeout);
}
return;
}
RedisUtils.expire(key, Duration.ofSeconds(timeout));
}
@Override
public Object getObject(String key) {
return RedisUtils.get(key);
}
@Override
public void setObject(String key, Object object, long timeout) {
if (0 == timeout || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {
return;
}
// 判断是否为永不过期
if (timeout == SaTokenDao.NEVER_EXPIRE) {
RedisUtils.set(key, object);
} else {
RedisUtils.set(key, object, Duration.ofSeconds(timeout));
}
}
@Override
public void updateObject(String key, Object object) {
long expire = getObjectTimeout(key);
// -2无此键
if (expire == SaTokenDao.NOT_VALUE_EXPIRE) {
return;
}
this.setObject(key, object, expire);
}
@Override
public void deleteObject(String key) {
RedisUtils.delete(key);
}
@Override
public long getObjectTimeout(String key) {
long timeout = RedisUtils.getTimeToLive(key);
return timeout < 0 ? timeout : timeout / 1000;
}
@Override
public void updateObjectTimeout(String key, long timeout) {
// 判断是否想要设置为永久
if (timeout == SaTokenDao.NEVER_EXPIRE) {
long expire = getObjectTimeout(key);
if (expire == SaTokenDao.NEVER_EXPIRE) {
// 如果其已经被设置为永久,则不作任何处理
} else {
// 如果尚未被设置为永久,那么再次 set 一次
this.setObject(key, this.getObject(key), timeout);
}
return;
}
RedisUtils.expire(key, Duration.ofSeconds(timeout));
}
@Override
public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
Collection<String> keys = RedisUtils.keys(String.format("%s*%s*", prefix, keyword));
List<String> list = new ArrayList<>(keys);
return SaFoxUtil.searchList(list, start, size, sortType);
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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.charles7c.continew.starter.auth.satoken.properties;
import cn.dev33.satoken.dao.SaTokenDao;
import lombok.Data;
import top.charles7c.continew.starter.auth.satoken.enums.SaTokenDaoTypeEnum;
/**
* SaToken 持久层配置属性
*
* @author Charles7c
* @since 1.0.0
*/
@Data
public class SaTokenDaoProperties {
/**
* 持久层类型
*/
private SaTokenDaoTypeEnum type;
/**
* 自定义持久层实现类(当 type 为 CUSTOM 时必填)
*/
private Class<? extends SaTokenDao> impl;
}

View File

@@ -0,0 +1,34 @@
/*
* 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.charles7c.continew.starter.auth.satoken.properties;
import lombok.Data;
/**
* SaToken 安全配置属性
*
* @author Charles7c
* @since 1.0.0
*/
@Data
public class SaTokenSecurityProperties {
/**
* 排除(放行)路径配置
*/
private String[] excludes = new String[0];
}

View File

@@ -21,10 +21,10 @@
</modules>
<dependencies>
<!-- 核心模块 -->
<!-- 缓存模块 - Redisson -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-core</artifactId>
<artifactId>continew-starter-cache-redisson</artifactId>
</dependency>
</dependencies>
</project>