注册filter(配置的过滤链)
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean (){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new FilterModel());
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
@Bean
public FilterRegistrationBean filterRegistrationBean2 (){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new FilterModel2());
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}
实现接口
注意import
import javax.servlet.*; import javax.servlet.FilterConfig;
public class FilterModel implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init1");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("doFilter1");
String name = servletRequest.getParameter("name");
System.out.println("servletRequest = " + name);
if(name.equals("qyc")){
System.out.println("过滤1");
filterChain.doFilter(servletRequest,servletResponse);
}
}
@Override
public void destroy() {
System.out.println("destroy1");
}
}
public class FilterModel2 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init2");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
System.out.println("doFilter2");
System.out.println("httpServletRequest.getRequestURI() = " + httpServletRequest.getRequestURI());
System.out.println("httpServletRequest.getRequestURL() = " + httpServletRequest.getRequestURL());
System.out.println("httpServletRequest.getQueryString() = " + httpServletRequest.getQueryString());
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("destroy2");
}
}
执行顺序 过滤器Filter 拦截器Interceptor AOP
有关AOP和拦截器的内容请看上一篇博客
https://blog.csdn.net/qq_41835813/article/details/108501750 拦截器
https://blog.csdn.net/qq_41835813/article/details/108488261 AOP
init2
init1日志
doFilter1
servletRequest = qyc
过滤1
doFilter2
httpServletRequest.getRequestURI() = /login
httpServletRequest.getRequestURL() = http://localhost:8889/login
httpServletRequest.getPathInfo() = null
httpServletRequest.getQueryString() = name=qyc
username => qyc
controller + http://localhost:8889/login
around 环绕通知的目标方法名:test03
before 通过
URL : http://localhost:8889/login
HTTP_METHOD : GET
IP : 127.0.0.1
CLASS_METHOD : com.qyc.service.impl.TestServiceImpl.test03
ARGS : []
service环绕通知结束 7
@doAfter
@doAfterReturning
postHandle
拦截之后