方式一(不可排序)
- 新建过滤器实现javax.servlet.Filter.
- 应用添加@ServletComponentScan.
- 过滤器添加注解@WebFilter(name, urlPattern).
方式二(可排序)
- 新建过滤器实现javax.servlet.Filter.
- 新建配置类并注入FilterRegistrationBean,使用该对象的setOrder(),addUrlPattern()等方法实现。
应用启动
package com.guofengzhi.helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication //@ServletComponentScan方式一添加 public class HelloworldApplication { public static void main(String[] args) { SpringApplication.run(HelloworldApplication.class, args); } }
过滤器类
package com.guofengzhi.helloworld.filter; import org.springframework.core.annotation.Order; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import java.io.IOException;
//@WebFilter(filterName = "a", urlPatterns = "/*")方式一需要添加,方式二不需要
public class AFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init a filter...");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("do a filter...");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
System.out.println("destory a filter...");
}
}
FilterRegistrationBean配置类
package com.guofengzhi.helloworld.filter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FilterRegistrationBeanConf { @Bean public FilterRegistrationBean<AFilter> aFilter() { FilterRegistrationBean<AFilter> filterRegistrationBean = new FilterRegistrationBean<>(); filterRegistrationBean.setFilter(new AFilter()); filterRegistrationBean.setOrder(1); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } @Bean public FilterRegistrationBean<BaseFilter> baseFilter() { FilterRegistrationBean<BaseFilter> filterRegistrationBean = new FilterRegistrationBean<>(); filterRegistrationBean.setFilter(new BaseFilter()); filterRegistrationBean.setOrder(2); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } @Bean public FilterRegistrationBean<WorkFilter> workFilter() { FilterRegistrationBean<WorkFilter> filterRegistrationBean = new FilterRegistrationBean<>(); filterRegistrationBean.setFilter(new WorkFilter()); filterRegistrationBean.setOrder(3); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } @Bean public FilterRegistrationBean<CompleteFilter> completeFilter() { FilterRegistrationBean<CompleteFilter> filterRegistrationBean = new FilterRegistrationBean<>(); filterRegistrationBean.setFilter(new CompleteFilter()); filterRegistrationBean.setOrder(4); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } }