2 Commits

Author SHA1 Message Date
cuijiawang
ee41e544f5 refactor(config):重构配置文件并优化认证异常处理
- 移除各模块中重复的 spring.config.import 配置
- 新增 bootstrap.yml 文件用于 wol-module-codegen 模块配置加载
- 在网关模块新增 codegen 路由规则
- 将通用依赖提升至父级 pom 统一管理
- 为 SaToken 认证失败添加详细日志记录- 优化认证异常处理逻辑,区分登录异常与其他认证异常
- 移除系统基础模块和代码生成模块中的冗余依赖声明
2025-10-27 12:56:08 +08:00
cuijiawang
08accdfdd4 feat(config): 统一Sa-Token配置并优化Bean定义覆盖策略
- 在网关模块启用allow-bean-definition-overriding解决redisTemplate冲突
- 移除auth模块中的Sa-Token本地配置
- 将Sa-Token配置统一迁移至common-core模块
- 新增common-satoken模块支持外部化配置
- 网关模块引入wol-common-redis依赖
- 修复SaTokenExceptionHandler中认证失败提示文案错误
2025-10-27 11:32:09 +08:00
15 changed files with 75 additions and 45 deletions

View File

@@ -16,6 +16,12 @@ wol:
password: ${NACOS_PASSWORD:nacos}
namespace: ${NACOS_NAMESPACE:public}
group: ${NACOS_GROUP:DEFAULT_GROUP}
satoken:
tokenName: ${SATOKEN_TOKENNAME:Authorization}
isConcurrent: ${SATOKEN_ISCONCURRENT:true}
isShare: ${SATOKEN_ISSHARE:true}
isLog: ${SATOKEN_ISLOG:true}
jwtSecretKey: ${SATOKEN_JWTSECRETKEY:abcdefghijklmnopqrstuvwxyz}
jasypt:
encryptor:
password: ${JASYPT_ENCRYPTOR_PASSWORD:}

View File

@@ -1,11 +1,13 @@
package com.agileboot.common.satoken.config;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.agileboot.common.core.constant.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
@@ -17,6 +19,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
*
* @author Lion Li
*/
@Slf4j
@AutoConfiguration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class SaTokenMvcConfiguration implements WebMvcConfigurer {
@@ -41,7 +44,13 @@ public class SaTokenMvcConfiguration implements WebMvcConfigurer {
.setAuth(obj -> {
SaRouter.match("/**", "/auth/login", StpUtil::checkLogin);
})
.setError(e -> SaResult.error("认证失败,无法访问系统资源").setCode(HttpStatus.UNAUTHORIZED));
.setError(e -> {
if (e instanceof NotLoginException) {
return SaResult.error(e.getMessage()).setCode(HttpStatus.UNAUTHORIZED);
}
log.error("认证失败'{}',无法访问系统资源", e.getMessage());
return SaResult.error("认证失败,无法访问系统资源").setCode(HttpStatus.UNAUTHORIZED);
});
}
}

View File

@@ -46,7 +46,7 @@ public class SaTokenExceptionHandler {
public R<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "2认证失败,无法访问系统资源");
return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
}
}

View File

@@ -13,3 +13,14 @@ sa-token:
check-same-token: false
# token前缀
token-prefix: "Bearer"
# token名称 (同时也是cookie名称)
token-name: ${wol.satoken.tokenName}
# 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
is-concurrent: ${wol.satoken.isConcurrent}
# 在多人登录同一账号时是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: ${wol.satoken.isShare}
# 是否输出操作日志
is-log: ${wol.satoken.isLog}
# jwt秘钥
jwt-secret-key: ${wol.satoken.jwtSecretKey}

View File

@@ -27,18 +27,3 @@
# acquire-timeout: 3000
# # 分布式锁的超时时间,默认为 30 秒
# expire: 30000
# Sa-Token配置
sa-token:
# token名称 (同时也是cookie名称)
token-name: Authorization
# 开启内网服务调用鉴权(不允许越过gateway访问内网服务 保障服务安全)
check-same-token: false
# 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
is-concurrent: true
# 在多人登录同一账号时是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: true
# 是否输出操作日志
is-log: true
# jwt秘钥
jwt-secret-key: abcdefghijklmnopqrstuvwxyz

View File

@@ -5,7 +5,5 @@ server:
spring:
application:
name: @application.name@
config:
import: classpath:base.yml,classpath:nacos.yml
profiles:
active: dev

View File

@@ -21,7 +21,10 @@
<groupId>com.agileboot</groupId>
<artifactId>wol-common-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-redis</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-satoken</artifactId>

View File

@@ -8,6 +8,7 @@ import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.agileboot.common.core.constant.HttpStatus;
import com.agileboot.common.satoken.utils.LoginHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -18,6 +19,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
*
* @author Lion Li
*/
@Slf4j
@Configuration
public class SaTokenConfig {
@@ -56,6 +58,7 @@ public class SaTokenConfig {
if (e instanceof NotLoginException) {
return SaResult.error(e.getMessage()).setCode(HttpStatus.UNAUTHORIZED);
}
log.error("认证失败'{}',无法访问系统资源", e.getMessage());
return SaResult.error("认证失败,无法访问系统资源").setCode(HttpStatus.UNAUTHORIZED);
});
}

View File

@@ -9,3 +9,7 @@ spring:
uri: lb://wol-auth
predicates:
- Path=/auth/**
- id: wol-module-codegen
uri: lb://wol-module-codegen
predicates:
- Path=/codegen/**

View File

@@ -7,10 +7,13 @@ server:
spring:
application:
name: @application.name@
config:
import: classpath:base.yml,classpath:nacos.yml
profiles:
active: dev
main:
# 允许Bean定义覆盖解决redisTemplate冲突
# 响应式环境GatewayRedisson 的 Bean 和自定义 Bean 同时加载,产生冲突
# 在 WebFluxGateway和 ServletAuth环境中自动配置的策略不同
allow-bean-definition-overriding: true
#logging:
# level:
# com.alibaba.cloud.nacos: DEBUG

View File

@@ -12,18 +12,6 @@
<artifactId>agileboot-system-base</artifactId>
<dependencies>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-web</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-mybatis</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-domain</artifactId>
</dependency>
<!-- 获取系统信息 -->
<dependency>
<groupId>com.github.oshi</groupId>

View File

@@ -15,5 +15,31 @@
<module>wol-module-ai</module>
</modules>
<dependencies>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-satoken</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-web</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-mybatis</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-redis</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-domain</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -17,15 +17,6 @@
</properties>
<dependencies>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-web</artifactId>
</dependency>
<dependency>
<groupId>com.agileboot</groupId>
<artifactId>wol-common-mybatis</artifactId>
</dependency>
<!-- Freemarker模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -3,5 +3,3 @@ server:
spring:
application:
name: @application.name@
config:
import: classpath:base.yml

View File

@@ -0,0 +1,5 @@
spring:
application:
name: @application.name@
config:
import: classpath:base.yml,classpath:nacos.yml