mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor(auth/satoken): 优化 SaToken 持久层配置
This commit is contained in:
@@ -250,7 +250,6 @@ ContiNew Starter 的分支目前分为下个大版本的开发分支和上个大
|
||||
- 感谢 <a href="https://github.com/baomidou/mybatis-plus" target="_blank">MyBatis Plus</a>、<a href="https://github.com/dromara/sa-token" target="_blank">Sa-Token</a> 、<a href="https://github.com/alibaba/jetcache" target="_blank">JetCache</a>、<a href="https://github.com/opengoofy/crane4j" target="_blank">Crane4j</a>、<a href="https://github.com/xiaoymin/knife4j" target="_blank">Knife4j</a>、<a href="https://github.com/dromara/hutool" target="_blank">Hutool</a> 等开源组件作者为国内开源世界作出的贡献
|
||||
- 感谢 <a href="https://github.com/elunez/eladmin" target="_blank">ELADMIN</a>、<a href="https://github.com/dromara/RuoYi-Vue-Plus" target="_blank">RuoYi-Vue-Plus</a>、<a href="https://gitee.com/herodotus/dante-engine" target="_blank">Dante-Engine</a>,致敬各位作者为开源脚手架领域作出的贡献
|
||||
- e.g. 起源于 ELADMIN 项目开源的 QueryHelper 组件
|
||||
- e.g. 扩展于 RuoYi-Vue-Plus 项目封装的 SaToken 相关认证鉴权配置
|
||||
- e.g. 扩展于 Dante-Engine 项目封装的 Redisson 相关配置
|
||||
- 感谢项目使用或未使用到的每一款开源组件,致敬各位开源先驱 :fire:
|
||||
|
||||
|
@@ -19,10 +19,8 @@ package top.charles7c.continew.starter.auth.satoken.autoconfigure;
|
||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
|
||||
import cn.dev33.satoken.router.SaRouter;
|
||||
import cn.dev33.satoken.stp.StpInterface;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -60,27 +58,26 @@ public class SaTokenAutoConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 注册 Sa-Token 拦截器,校验规则为 StpUtil.checkLogin() 登录校验
|
||||
registry.addInterceptor(new SaInterceptor(handle -> SaRouter.match(StringConstants.PATH_PATTERN)
|
||||
.notMatch(properties.getSecurity().getExcludes())
|
||||
.check(r -> StpUtil.checkLogin()))).addPathPatterns(StringConstants.PATH_PATTERN);
|
||||
registry.addInterceptor(saInterceptor()).addPathPatterns(StringConstants.PATH_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义权限认证配置
|
||||
* SaToken 拦截器配置
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(prefix = "sa-token.extension.permission", name = PropertiesConstants.ENABLED, havingValue = "true")
|
||||
public StpInterface stpInterface() {
|
||||
return ReflectUtil.newInstance(properties.getPermission().getImpl());
|
||||
public SaInterceptor saInterceptor() {
|
||||
return new SaInterceptor(handle -> SaRouter.match(StringConstants.PATH_PATTERN)
|
||||
.notMatch(properties.getSecurity().getExcludes())
|
||||
.check(r -> StpUtil.checkLogin()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义持久层配置
|
||||
* 持久层配置
|
||||
*/
|
||||
@Configuration
|
||||
@Import({SaTokenDaoConfiguration.Redis.class, SaTokenDaoConfiguration.Custom.class})
|
||||
@Import({SaTokenDaoConfiguration.Default.class, SaTokenDaoConfiguration.Redis.class,
|
||||
SaTokenDaoConfiguration.Custom.class})
|
||||
protected static class SaTokenDaoAutoConfiguration {
|
||||
}
|
||||
|
||||
@@ -88,6 +85,7 @@ public class SaTokenAutoConfiguration implements WebMvcConfigurer {
|
||||
* 整合 JWT(简单模式)
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public StpLogic stpLogic() {
|
||||
return new StpLogicJwtForSimple();
|
||||
}
|
||||
|
@@ -40,12 +40,6 @@ public class SaTokenExtensionProperties {
|
||||
@NestedConfigurationProperty
|
||||
private SaTokenDaoProperties dao;
|
||||
|
||||
/**
|
||||
* 权限认证配置
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private SaTokenPermissionProperties permission;
|
||||
|
||||
/**
|
||||
* 安全配置
|
||||
*/
|
||||
@@ -68,14 +62,6 @@ public class SaTokenExtensionProperties {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
public SaTokenPermissionProperties getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
public void setPermission(SaTokenPermissionProperties permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public SaTokenSecurityProperties getSecurity() {
|
||||
return security;
|
||||
}
|
||||
@@ -86,6 +72,6 @@ public class SaTokenExtensionProperties {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SaTokenExtensionProperties{" + "enabled=" + enabled + ", dao=" + dao + ", permission=" + permission + ", security=" + security + '}';
|
||||
return "SaTokenExtensionProperties{" + "enabled=" + enabled + ", dao=" + dao + ", security=" + security + '}';
|
||||
}
|
||||
}
|
||||
|
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* 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.stp.StpInterface;
|
||||
|
||||
/**
|
||||
* SaToken 权限认证配置属性
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public class SaTokenPermissionProperties {
|
||||
|
||||
/**
|
||||
* 是否启用权限认证
|
||||
*/
|
||||
private boolean enabled = false;
|
||||
|
||||
/**
|
||||
* 自定义权限认证实现类(当 enabled 为 true 时必填)
|
||||
*/
|
||||
private Class<? extends StpInterface> impl;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public Class<? extends StpInterface> getImpl() {
|
||||
return impl;
|
||||
}
|
||||
|
||||
public void setImpl(Class<? extends StpInterface> impl) {
|
||||
this.impl = impl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SaTokenPermissionProperties{" + "enabled=" + enabled + ", impl=" + impl + '}';
|
||||
}
|
||||
}
|
@@ -17,17 +17,17 @@
|
||||
package top.charles7c.continew.starter.auth.satoken.autoconfigure.dao;
|
||||
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.dev33.satoken.dao.SaTokenDaoDefaultImpl;
|
||||
import org.redisson.client.RedisClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
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.autoconfigure.SaTokenExtensionProperties;
|
||||
import top.charles7c.continew.starter.auth.satoken.core.SaTokenDaoRedisImpl;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import top.charles7c.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration;
|
||||
|
||||
/**
|
||||
@@ -44,15 +44,33 @@ public class SaTokenDaoConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义持久层实现类-Redis
|
||||
* 自定义持久层实现-默认
|
||||
*/
|
||||
@ConditionalOnClass(RedisClient.class)
|
||||
@ConditionalOnMissingBean(SaTokenDao.class)
|
||||
@ConditionalOnClass(RedisClient.class)
|
||||
@AutoConfigureBefore(RedissonAutoConfiguration.class)
|
||||
@ConditionalOnProperty(name = "sa-token.extension.dao.type", havingValue = "default", matchIfMissing = true)
|
||||
public static class Default {
|
||||
static {
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'SaToken-Dao-Default' completed initialization.");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SaTokenDao saTokenDao() {
|
||||
return new SaTokenDaoDefaultImpl();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义持久层实现-Redis
|
||||
*/
|
||||
@ConditionalOnMissingBean(SaTokenDao.class)
|
||||
@ConditionalOnClass(RedisClient.class)
|
||||
@AutoConfigureBefore(RedissonAutoConfiguration.class)
|
||||
@ConditionalOnProperty(name = "sa-token.extension.dao.type", havingValue = "redis")
|
||||
public static class Redis {
|
||||
static {
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'SaToken-SaTokenDao-Redis' completed initialization.");
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'SaToken-Dao-Redis' completed initialization.");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -62,18 +80,18 @@ public class SaTokenDaoConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义持久层实现类-自定义
|
||||
* 自定义持久层实现
|
||||
*/
|
||||
@ConditionalOnMissingBean(SaTokenDao.class)
|
||||
@ConditionalOnProperty(name = "sa-token.extension.dao.type", havingValue = "custom")
|
||||
public 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());
|
||||
@ConditionalOnMissingBean
|
||||
public SaTokenDao saTokenDao() {
|
||||
if (log.isErrorEnabled()) {
|
||||
log.error("Consider defining a bean of type '{}' in your configuration.", ResolvableType
|
||||
.forClass(SaTokenDao.class));
|
||||
}
|
||||
throw new NoSuchBeanDefinitionException(SaTokenDao.class);
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,7 +16,6 @@
|
||||
|
||||
package top.charles7c.continew.starter.auth.satoken.autoconfigure.dao;
|
||||
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import top.charles7c.continew.starter.auth.satoken.enums.SaTokenDaoType;
|
||||
|
||||
/**
|
||||
@@ -30,12 +29,7 @@ public class SaTokenDaoProperties {
|
||||
/**
|
||||
* 持久层类型
|
||||
*/
|
||||
private SaTokenDaoType type;
|
||||
|
||||
/**
|
||||
* 自定义持久层实现类(当 type 为 CUSTOM 时必填)
|
||||
*/
|
||||
private Class<? extends SaTokenDao> impl;
|
||||
private SaTokenDaoType type = SaTokenDaoType.DEFAULT;
|
||||
|
||||
public SaTokenDaoType getType() {
|
||||
return type;
|
||||
@@ -45,16 +39,8 @@ public class SaTokenDaoProperties {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Class<? extends SaTokenDao> getImpl() {
|
||||
return impl;
|
||||
}
|
||||
|
||||
public void setImpl(Class<? extends SaTokenDao> impl) {
|
||||
this.impl = impl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SaTokenDaoProperties{" + "type=" + type + ", impl=" + impl + '}';
|
||||
return "SaTokenDaoProperties{" + "type=" + type + '}';
|
||||
}
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.continew.starter.auth.satoken.core;
|
||||
package top.charles7c.continew.starter.auth.satoken.autoconfigure.dao;
|
||||
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
@@ -24,6 +24,11 @@ package top.charles7c.continew.starter.auth.satoken.enums;
|
||||
*/
|
||||
public enum SaTokenDaoType {
|
||||
|
||||
/**
|
||||
* 默认
|
||||
*/
|
||||
DEFAULT,
|
||||
|
||||
/**
|
||||
* Redis
|
||||
*/
|
||||
|
Reference in New Issue
Block a user