PC 2 лет назад
Родитель
Сommit
656c96a214

+ 2 - 0
sckw-auth/src/main/java/com/sckw/auth/AuthApplication.java

@@ -3,6 +3,7 @@ package com.sckw.auth;
 import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.web.servlet.ServletComponentScan;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.openfeign.EnableFeignClients;
 
@@ -14,6 +15,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
 @EnableFeignClients({"com.sckw.*.api.feign"})
 @EnableDiscoveryClient
 @SpringBootApplication(scanBasePackages = {"com.sckw.core.*", "com.sckw.redis","com.sckw.auth.*"})
+@ServletComponentScan("com.sckw.core.filter")
 public class AuthApplication {
     public static void main(String[] args) {
         // 关闭nacos日志

+ 7 - 5
sckw-auth/src/main/java/com/sckw/auth/service/impl/IndexServiceImpl.java

@@ -5,6 +5,7 @@ import com.sckw.auth.model.vo.req.LoginReqVo;
 import com.sckw.auth.model.vo.res.DeptInfoResVo;
 import com.sckw.auth.model.vo.res.EntInfoResVo;
 import com.sckw.auth.model.vo.res.LoginResVo;
+import com.sckw.core.model.constant.CacheGroup;
 import com.sckw.system.api.model.dto.res.KwsRoleResDto;
 import com.sckw.core.exception.SystemException;
 import com.sckw.core.model.constant.Global;
@@ -89,16 +90,17 @@ public class IndexServiceImpl implements IIndexService {
     }
 
     /**
-     * @param  loginResDto 登录的返参
+     * @param  loginResVo 登录的返参
      * @desc: 生成token,存redis
      * @author: czh
      * @date: 2023/6/12
      */
-    private void saveToCache(LoginResVo loginResDto) {
+    private void saveToCache(LoginResVo loginResVo) {
         try {
-            String token = EncryUtil.encry(Global.PRI_KEY, JSON.toJSONString(loginResDto));
-            loginResDto.setToken(token);
-            redissonUtils.add(token, JSON.toJSONString(loginResDto));
+            String account = loginResVo.getAccount();
+            String token = EncryUtil.encry(Global.PRI_KEY, account);
+            loginResVo.setToken(token);
+            redissonUtils.add(CacheGroup.LOGININFO + account, JSON.toJSONString(loginResVo));
         } catch (Exception e) {
             throw new SystemException(HttpStatus.GLOBAL_EXCEPTION_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
         }

+ 16 - 3
sckw-common/sckw-common-core/src/main/java/com/sckw/core/filter/LoginFilter.java

@@ -1,6 +1,7 @@
 package com.sckw.core.filter;
 
 import com.sckw.core.exception.SystemException;
+import com.sckw.core.model.constant.CacheGroup;
 import com.sckw.core.model.constant.Global;
 import com.sckw.core.utils.EncryUtil;
 import com.sckw.core.utils.StringUtils;
@@ -12,13 +13,24 @@ import jakarta.servlet.*;
 import jakarta.servlet.annotation.WebFilter;
 import jakarta.servlet.http.HttpServletRequest;
 import lombok.Data;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Objects;
 
-@WebFilter(urlPatterns = "/*")
+@Order(1)
+@Component
 public class LoginFilter implements Filter {
 
+    private static final List<String> excludePath = new ArrayList<>();
+
+    static {
+        excludePath.add("/auth/login");
+    }
+
     @Resource
     private RedissonUtils redissonUtils;
 
@@ -27,7 +39,7 @@ public class LoginFilter implements Filter {
         //将形参servletRequest强制转换为HttpServletRequest类型,以便获取请求的URL地址和请求头
         HttpServletRequest request = (HttpServletRequest) servletRequest;
         String requestURI = request.getRequestURI();
-        if (requestURI.contains("/auth/login")) {
+        if (excludePath.contains(requestURI)) {
             // 如果是登录请求直接放行
             filterChain.doFilter(servletRequest, servletResponse);
             return;
@@ -44,10 +56,11 @@ public class LoginFilter implements Filter {
         } catch (Exception e) {
             throw new SystemException(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.TOKEN_ERROR);
         }
-        Object object = redissonUtils.get(key);
+        Object object = redissonUtils.get(CacheGroup.LOGININFO + key);
         if (Objects.isNull(object)) {
             throw new SystemException(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.TOKEN_INVAILD);
         }
         filterChain.doFilter(servletRequest, servletResponse);
     }
+
 }

+ 0 - 31
sckw-common/sckw-common-core/src/main/java/com/sckw/core/interceptor/CustomWebMvcConfigurer.java

@@ -1,31 +0,0 @@
-package com.sckw.core.interceptor;
-
-import org.springframework.context.annotation.Configuration;
-import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * @author czh
- * @desc TODO
- * @date 2023/6/12
- */
-@Configuration
-public class CustomWebMvcConfigurer implements WebMvcConfigurer {
-
-    /**
-     * @param * registry
-     * @desc: 配置拦截规则与注入拦截器
-     * @author: czh
-     * @date: 2023/6/12
-     */
-    @Override
-    public void addInterceptors(InterceptorRegistry registry) {
-        // addPathPattern 添加拦截规则 /** 拦截所有包括静态资源
-        // excludePathPattern 排除拦截规则 所以我们需要放开静态资源的拦截
-        registry.addInterceptor(new LoginInterceptor())
-                .addPathPatterns("/**");
-    }
-}

+ 0 - 72
sckw-common/sckw-common-core/src/main/java/com/sckw/core/interceptor/LoginInterceptor.java

@@ -1,72 +0,0 @@
-package com.sckw.core.interceptor;
-
-import com.sckw.core.exception.SystemException;
-import com.sckw.core.model.constant.Global;
-import com.sckw.core.utils.EncryUtil;
-import com.sckw.core.utils.StringUtils;
-import com.sckw.core.web.constant.HttpStatus;
-import com.sckw.core.web.constant.RequestConstant;
-import com.sckw.redis.utils.RedissonUtils;
-import jakarta.annotation.Resource;
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.web.method.HandlerMethod;
-import org.springframework.web.servlet.HandlerInterceptor;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-
-/**
- * @author czh
- * @desc 拦截器
- * @date 2023/6/12
- */
-@Slf4j
-public class LoginInterceptor implements HandlerInterceptor {
-
-    private static final List<String> excludePath = new ArrayList<>();
-
-    static {
-        excludePath.add("/auth/login");
-    }
-
-    @Resource
-    private RedissonUtils redissonUtils;
-
-    @Override
-    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
-
-        //1、定义不拦截的接口
-        log.info("==============>getRequestURI:{} ", request.getRequestURI());
-        if (excludePath.contains(request.getRequestURI())) {
-            return true;
-        }
-
-//        if(handler instanceof HandlerMethod){
-//            return true;
-//        }
-
-        //2、非登录接口,校验token是否过期
-        String token = request.getHeader(RequestConstant.TOKEN);
-        if (StringUtils.isBlank(token)) {
-            throw new SystemException(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.TOKEN_MISSING);
-        }
-
-        String key = null;
-        try {
-            key = EncryUtil.descry(Global.PRI_KEY, token);
-        } catch (Exception e) {
-            throw new SystemException(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.TOKEN_ERROR);
-        }
-        Object object = redissonUtils.get(key);
-        if (Objects.isNull(object)) {
-            throw new SystemException(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.TOKEN_INVAILD);
-        }
-
-        return true;
-    }
-
-
-}

+ 12 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/model/constant/CacheGroup.java

@@ -0,0 +1,12 @@
+package com.sckw.core.model.constant;
+
+/**
+ * @author czh
+ * @desc redis的分组
+ * @date 2023/6/13
+ */
+public class CacheGroup {
+
+    public static final String LOGININFO = "LOGIN:";
+
+}

+ 1 - 1
sckw-common/sckw-common-redis/src/main/java/com/sckw/redis/utils/RedissonUtils.java

@@ -40,7 +40,7 @@ public class RedissonUtils {
             //log.info("update data");
         }
         //添加缓存,若已存在,则替换,设置缓存超时时间
-        bucket.set(value, 60, TimeUnit.SECONDS);
+        bucket.set(value, 60, TimeUnit.MINUTES);
     }
 
 

+ 2 - 0
sckw-modules/sckw-system/src/main/java/com/sckw/system/SystemApplication.java

@@ -4,6 +4,7 @@ import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.web.servlet.ServletComponentScan;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.openfeign.EnableFeignClients;
 
@@ -12,6 +13,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
 @EnableDiscoveryClient
 @SpringBootApplication(scanBasePackages = {"com.sckw.core.*", "com.sckw.core.aspect","com.sckw.system","com.sckw.redis"})
 @MapperScan(basePackages="com.sckw.system.dao")
+@ServletComponentScan("com.sckw.core.filter")
 public class SystemApplication {
 
     public static void main(String[] args) {