|
@@ -1,53 +1,53 @@
|
|
|
-//package com.bizmatics.service.config;
|
|
|
-//
|
|
|
-//
|
|
|
-//import com.bizmatics.common.core.exception.BusinessException;
|
|
|
-//import com.bizmatics.model.User;
|
|
|
-//import com.bizmatics.service.util.SessionLocal;
|
|
|
-//import lombok.extern.slf4j.Slf4j;
|
|
|
-//import org.springframework.stereotype.Component;
|
|
|
-//
|
|
|
-//import javax.servlet.*;
|
|
|
-//import javax.servlet.annotation.WebFilter;
|
|
|
-//import javax.servlet.http.HttpServletRequest;
|
|
|
-//import javax.servlet.http.HttpServletResponse;
|
|
|
-//import javax.servlet.http.HttpSession;
|
|
|
-//import java.io.IOException;
|
|
|
-//import java.util.Arrays;
|
|
|
-//import java.util.Collections;
|
|
|
-//import java.util.HashSet;
|
|
|
-//import java.util.Set;
|
|
|
-//
|
|
|
-///**
|
|
|
-// * @author yq
|
|
|
-// * @date 2021/7/8 10:35
|
|
|
-// */
|
|
|
-//@Component
|
|
|
-//@Slf4j
|
|
|
-//@WebFilter(urlPatterns = "/*", filterName = "reqResFilter")
|
|
|
-//public class SessionFilter implements Filter {
|
|
|
-//
|
|
|
-// private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>(
|
|
|
-// Collections.singletonList("/user/login")));
|
|
|
-//
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
|
|
|
-// HttpServletRequest request = (HttpServletRequest)servletRequest;
|
|
|
-// String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
|
|
|
-// boolean allowedPath = ALLOWED_PATHS.contains(path);
|
|
|
-// if (!allowedPath){
|
|
|
-// HttpSession session = request.getSession();
|
|
|
-// if(session.getAttribute( "sessionuser") != null) {
|
|
|
-// if(SessionLocal.getUser() == null) {
|
|
|
-// log.info("【当前线程"+Thread.currentThread().getName()+"中用户信息为空,从session中set到ThreadLocal.】");
|
|
|
-// SessionLocal.setUser( (User)session.getAttribute( "sessionuser") );
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// throw new BusinessException("用户未登录请先登录");
|
|
|
-// }
|
|
|
-// }
|
|
|
-// chain.doFilter(servletRequest,servletResponse);
|
|
|
-// }
|
|
|
-//
|
|
|
-//}
|
|
|
+package com.bizmatics.service.config;
|
|
|
+
|
|
|
+
|
|
|
+import com.bizmatics.common.core.exception.BusinessException;
|
|
|
+import com.bizmatics.model.User;
|
|
|
+import com.bizmatics.service.util.SessionLocal;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.servlet.*;
|
|
|
+import javax.servlet.annotation.WebFilter;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author yq
|
|
|
+ * @date 2021/7/8 10:35
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+@WebFilter(urlPatterns = "/*", filterName = "reqResFilter")
|
|
|
+public class SessionFilter implements Filter {
|
|
|
+
|
|
|
+ private static final Set<String> ALLOWED_PATHS = Collections.unmodifiableSet(new HashSet<>(
|
|
|
+ Collections.singletonList("/user/login")));
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
|
|
|
+ HttpServletRequest request = (HttpServletRequest)servletRequest;
|
|
|
+ String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
|
|
|
+ boolean allowedPath = ALLOWED_PATHS.contains(path);
|
|
|
+ if (!allowedPath){
|
|
|
+ HttpSession session = request.getSession();
|
|
|
+ if(session.getAttribute( "sessionuser") != null) {
|
|
|
+ if(SessionLocal.getUser() == null) {
|
|
|
+ log.info("【当前线程"+Thread.currentThread().getName()+"中用户信息为空,从session中set到ThreadLocal.】");
|
|
|
+ SessionLocal.setUser( (User)session.getAttribute( "sessionuser") );
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new BusinessException("用户未登录请先登录");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ chain.doFilter(servletRequest,servletResponse);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|