Spring Boot Filter实现的三种方式
一 使用@WebFilter注解
@WebFilter写在类上
@WebFilter常用属性
过滤器实现方法
- 写filter类实现javax.servlet.Filter接口,加@WebFilter注解,不可以设置filter执行顺序
package com.jsong.wiki.blog.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "testfilter",urlPatterns ={"/blog/*"})
public class AnnotatedRequestFitler implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.printf("filter");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
- 在有@Configuration的配置类上添加@ServletComponentScan,也可以直接加在启动类上
@ServletComponentScan没有参数时默认自动扫描当前类所在的包,也可以指定扫描的包
package com.jsong.wiki.blog.filter;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Configuration;
@ServletComponentScan(value = "com.jsong.wiki.blog.filter")
@Configuration
public class TestConfig {
}
- 过滤器配置好了,当访问http://localhost:8080/blog/test时,控制台就会打印出来filter
二 配置Bean
- 写一个filter类实现javax.servlet.Filter接口
package com.jsong.wiki.blog.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;
public class RequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.printf("hello");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
- 在配置类的bean中实例化这个Filter
package com.jsong.wiki.blog.config;
import com.jsong.wiki.blog.filter.RequestFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
@Configuration
public class FilterConfig {
@Bean
public Filter registerRequestFilter() {
return new RequestFilter();
}
}
三 FilterRegistrationBean(推荐),可以配置filter顺序
- 和方式差不多,首先创建一个filter类实现javax.servlet.Filter接口
注意要在类上写一个@Component注解,把这个类交给容器托管
package com.jsong.wiki.blog.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;
@Component
public class RequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.printf("hello");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
- 在配置类中,通过FilterRegistrationBean管理这个filter
package com.jsong.wiki.blog.config;
import com.jsong.wiki.blog.filter.RequestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
@Configuration
public class FilterConfig {
// @Bean
// public Filter registerRequestFilter() {
// return new RequestFilter();
// }
@Bean
public FilterRegistrationBean registration(RequestFilter filter) {
FilterRegistrationBean bean = new FilterRegistrationBean(filter);
bean.addUrlPatterns("/blog/*");
// 排序
bean.setOrder(1);
return bean;
}
}
- bean.setOrder(1); 设置过滤器的执行顺序,越小越先执行
注意事项
一定要设置过滤器的urlParten,否者过滤器会走两次或多次,不正确的请求地址也会走过滤器。
栗子
当把urlParten注掉后
在doFilter方法里打断点,在请求一下
http://localhost:8080/blog/test
- 第一个断点的url /blog/test
- 第二个断点的url /favicon.ico 图标的请求,也会走过滤器