spring boot 配置Filter过滤器
程序员文章站
2022-04-28 16:49:43
...
首先要准备一个实现了Filter的接口的类 SessionFilter:
import org.slf4j.LoggerFactory;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by mazhenhua on 2016/12/27.
*
* 过滤器
*/
public class SessionFilter implements Filter {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SessionFilter.class);
/**
* 封装,不需要过滤的list列表
*/
protected static List<Pattern> patterns = new ArrayList<Pattern>();
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
logger.info("aaaaaaaaaa");
String url = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
if (url.startsWith("/") && url.length() > 1) {
url = url.substring(1);
}
if (isInclude(url)){
chain.doFilter(httpRequest, httpResponse);
return;
} else {
HttpSession session = httpRequest.getSession();
if (session.getAttribute("") != null){
// session存在
chain.doFilter(httpRequest, httpResponse);
return;
} else {
// session不存在 准备跳转失败
/* RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);*/
chain.doFilter(httpRequest, httpResponse);
return;
}
}
}
@Override
public void destroy() {
}
/**
* 是否需要过滤
* @param url
* @return
*/
private boolean isInclude(String url) {
for (Pattern pattern : patterns) {
Matcher matcher = pattern.matcher(url);
if (matcher.matches()) {
return true;
}
}
return false;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
实际开发中往往有很多请求要直接请求进来,不需要鉴权登陆的,所以代码中过滤掉这种请求的代码,装进list就好了。
/**
* 配置过滤器
* @return
*/
@Bean
public FilterRegistrationBean someFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(sessionFilter());
registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("sessionFilter");
return registration;
}
/**
* 创建一个bean
* @return
*/
@Bean(name = "sessionFilter")
public Filter sessionFilter() {
return new SessionFilter();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
经过上面这俩步的配置,过滤器基本上就可以了。
配置多个filter,并指定顺序,请参考: https://blog.csdn.net/mzh1992/article/details/66970924
推荐阅读
-
JSP过滤器Filter配置过滤类型全部汇总
-
Spring Boot打包部署和环境配置详解
-
Spring Boot 配置 IDEA和DevTools 热部署的方法
-
Spring Boot + Vue 前后端分离开发之前端网络请求封装与配置
-
Spring Boot配置AOP打印日志的全过程
-
JSP spring boot / cloud 使用filter防止XSS
-
Spring Boot 编写Servlet、Filter、Listener、Interceptor的方法
-
spring boot Logging的配置以及使用详解
-
Spring Boot Security配置教程
-
Spring Boot 配置MySQL数据库重连的操作方法