diff --git a/ebike-user/pom.xml b/ebike-user/pom.xml index 1e5d6f7..7b5c9a8 100644 --- a/ebike-user/pom.xml +++ b/ebike-user/pom.xml @@ -171,18 +171,9 @@ ${jackson.version} - - cn.dev33 - sa-token-spring-boot3-starter - ${satoken.version} - - - - - cn.dev33 - sa-token-redis-jackson - ${satoken.version} + org.springframework.boot + spring-boot-starter-data-redis diff --git a/ebike-user/src/main/java/com/cdzy/user/component/TenantInterceptor.java b/ebike-user/src/main/java/com/cdzy/user/component/TenantInterceptor.java deleted file mode 100644 index 99d1722..0000000 --- a/ebike-user/src/main/java/com/cdzy/user/component/TenantInterceptor.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.cdzy.user.component; - -import cn.dev33.satoken.stp.StpUtil; -import com.cdzy.common.model.response.CommonStaffInfo; -import com.cdzy.user.config.AuthProperties; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import jakarta.annotation.Resource; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import org.jetbrains.annotations.NotNull; -import org.springframework.stereotype.Component; -import org.springframework.util.AntPathMatcher; -import org.springframework.web.servlet.HandlerInterceptor; - -import java.util.TimeZone; - -/** - * @author yanglei - * @since 2026-02-10 11:10 - */ -@Component -public class TenantInterceptor implements HandlerInterceptor { - - @Resource - private AuthProperties authProperties; - - private final AntPathMatcher pathMatcher = new AntPathMatcher(); - - @Override - public boolean preHandle(@NotNull HttpServletRequest request, - @NotNull HttpServletResponse response, - @NotNull Object handler) { - - String uri = request.getRequestURI(); - // 判断当前路径是否在配置的路径 "名单" 列表中 - boolean needAuth = authProperties.getRequiredPaths().stream() - .anyMatch(pattern -> pathMatcher.match(pattern, uri)); - - if (!needAuth) { - return true; - } - StpUtil.checkLogin(); - - - Long tenantId = getTenantIdByReuqest(request); - request.setAttribute("tenantId", tenantId); - - return true; - } - - Long getTenantIdByReuqest(HttpServletRequest request) { - String token = request.getHeader("Authorization"); - String id = (String) StpUtil.getLoginIdByToken(token); - Object object = StpUtil.getSessionByLoginId(id).get(id); - ObjectMapper objectMapper = new ObjectMapper() - .registerModule(new JavaTimeModule()) - .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) - .setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); - CommonStaffInfo staffInfo = objectMapper.convertValue(object, CommonStaffInfo.class); - return staffInfo.getOperatorId(); - } -} \ No newline at end of file diff --git a/ebike-user/src/main/java/com/cdzy/user/config/AuthProperties.java b/ebike-user/src/main/java/com/cdzy/user/config/AuthProperties.java deleted file mode 100644 index bf9f491..0000000 --- a/ebike-user/src/main/java/com/cdzy/user/config/AuthProperties.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.cdzy.user.config; - -import lombok.Data; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.List; - -/** - * user模块接口拦截列表(从配置文件获取) - * - * @author yanglei - * @since 2026-02-10 11:10 - */ - -@ConfigurationProperties(prefix = "cdzy.auth") -@Component -@Data -public class AuthProperties { - - private List requiredPaths = new ArrayList<>(); -} diff --git a/ebike-user/src/main/java/com/cdzy/user/config/GlobalExceptionHandler.java b/ebike-user/src/main/java/com/cdzy/user/config/GlobalExceptionHandler.java index 33108bd..a3af61c 100644 --- a/ebike-user/src/main/java/com/cdzy/user/config/GlobalExceptionHandler.java +++ b/ebike-user/src/main/java/com/cdzy/user/config/GlobalExceptionHandler.java @@ -1,6 +1,5 @@ package com.cdzy.user.config; -import cn.dev33.satoken.exception.NotLoginException; import com.alibaba.nacos.shaded.com.google.gson.JsonSyntaxException; import com.cdzy.common.ex.EbikeException; import com.cdzy.common.model.response.JsonResult; @@ -22,9 +21,9 @@ import java.sql.SQLSyntaxErrorException; /** * 全局异常处理 + * * @author attiya * @since 2023-6-14 - * */ @RestControllerAdvice(annotations = RestController.class) @Slf4j @@ -105,14 +104,6 @@ public class GlobalExceptionHandler { return JsonResult.failed(e.getMessage()); } - @ResponseBody - @ExceptionHandler - public JsonResult exp(HttpServletRequest request, NotLoginException e) { -// LogFactory.getLog(super.getClass()).error(e.getMessage(), e); - log.error(request.getServletPath(), e); - return JsonResult.failed(e.getMessage()); - } - @ResponseBody @ExceptionHandler public JsonResult exp(HttpServletRequest request, EbikeException e) { @@ -123,8 +114,9 @@ public class GlobalExceptionHandler { /** * 处理其他类型异常 + * * @param request 请求 - * @param e 异常 + * @param e 异常 * @return 结果 */ @ResponseBody diff --git a/ebike-user/src/main/java/com/cdzy/user/config/WebConfig.java b/ebike-user/src/main/java/com/cdzy/user/config/WebConfig.java deleted file mode 100644 index e7c61d2..0000000 --- a/ebike-user/src/main/java/com/cdzy/user/config/WebConfig.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cdzy.user.config; - -import com.cdzy.user.component.TenantInterceptor; -import jakarta.annotation.Resource; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -@Configuration -public class WebConfig implements WebMvcConfigurer { - - @Resource - private TenantInterceptor tenantInterceptor; - - @Override - public void addInterceptors(InterceptorRegistry registry) { - // 添加拦截器,拦截所有请求(由拦截器内部判断是否需要校验) - registry.addInterceptor(tenantInterceptor) - .addPathPatterns("/**"); - } -} \ No newline at end of file diff --git a/ebike-user/src/main/resources/application-dev.yml b/ebike-user/src/main/resources/application-dev.yml index 23b321e..1100f6f 100644 --- a/ebike-user/src/main/resources/application-dev.yml +++ b/ebike-user/src/main/resources/application-dev.yml @@ -63,29 +63,6 @@ spring: max-idle: 10 # 连接池中的最小空闲连接 min-idle: 0 -############## Sa-Token 配置 (文档: https://sa-token.cc) ############## -sa-token: - # token 名称(同时也是 cookie 名称) - token-name: Authorization - # token 有效期(单位:秒) 默认30天,-1 代表永久有效 - timeout: 2592000 - # token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结 - active-timeout: -1 - # 是否允许同一账号多地同时登录 (为 true 时允许一起登录, 为 false 时新登录挤掉旧登录) - is-concurrent: true - # 在多人登录同一账号时,是否共用一个 token (为 true 时所有登录共用一个 token, 为 false 时每次登录新建一个 token) - is-share: false - # token 风格(默认可取值:uuid、simple-uuid、random-32、random-64、random-128、tik) - token-style: random-32 - # 是否输出操作日志 - is-log: true - -# 需要被拦截的接口列表 -cdzy: - auth: - # 需要 token 校验的路径列表 - required-paths: - - /ebikeOrder/api/updateOrderAmount mybatis-flex: mapper-locations: classpath:mapper/*.xml diff --git a/ebike-user/src/main/resources/application-prod.yml b/ebike-user/src/main/resources/application-prod.yml index d77490a..d5dcacb 100644 --- a/ebike-user/src/main/resources/application-prod.yml +++ b/ebike-user/src/main/resources/application-prod.yml @@ -63,29 +63,6 @@ spring: max-idle: 10 # 连接池中的最小空闲连接 min-idle: 0 -############## Sa-Token 配置 (文档: https://sa-token.cc) ############## -sa-token: - # token 名称(同时也是 cookie 名称) - token-name: Authorization - # token 有效期(单位:秒) 默认30天,-1 代表永久有效 - timeout: 2592000 - # token 最低活跃频率(单位:秒),如果 token 超过此时间没有访问系统就会被冻结,默认-1 代表不限制,永不冻结 - active-timeout: -1 - # 是否允许同一账号多地同时登录 (为 true 时允许一起登录, 为 false 时新登录挤掉旧登录) - is-concurrent: true - # 在多人登录同一账号时,是否共用一个 token (为 true 时所有登录共用一个 token, 为 false 时每次登录新建一个 token) - is-share: false - # token 风格(默认可取值:uuid、simple-uuid、random-32、random-64、random-128、tik) - token-style: random-32 - # 是否输出操作日志 - is-log: true - -# 需要被拦截的接口列表 -cdzy: - auth: - # 需要 token 校验的路径列表 - required-paths: - - /ebikeOrder/api/updateOrderAmount mybatis-flex: mapper-locations: classpath:mapper/*.xml