|
@@ -0,0 +1,53 @@
|
|
|
|
|
+package com.sckw.core.filter;
|
|
|
|
|
+
|
|
|
|
|
+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.*;
|
|
|
|
|
+import jakarta.servlet.annotation.WebFilter;
|
|
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
+import lombok.Data;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+
|
|
|
|
|
+@WebFilter(urlPatterns = "/*")
|
|
|
|
|
+public class LoginFilter implements Filter {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private RedissonUtils redissonUtils;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
|
|
|
|
+ //将形参servletRequest强制转换为HttpServletRequest类型,以便获取请求的URL地址和请求头
|
|
|
|
|
+ HttpServletRequest request = (HttpServletRequest) servletRequest;
|
|
|
|
|
+ String requestURI = request.getRequestURI();
|
|
|
|
|
+ if (requestURI.contains("/auth/login")) {
|
|
|
|
|
+ // 如果是登录请求直接放行
|
|
|
|
|
+ filterChain.doFilter(servletRequest, servletResponse);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ //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);
|
|
|
|
|
+ }
|
|
|
|
|
+ filterChain.doFilter(servletRequest, servletResponse);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|