1、实现Filter接口或者继承Filter接口的实现类, 使用@Component或者@Configuration注解注册到Spring中
import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;
import java.util.Date;
/**
* 使用@Component将其注册到Spring
*/
@Component
public class MyFilter implements Filter {
//创建之后,马上执行;Filter会在服务器启动时就创建!
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("MyFilter.init");
}
/**
* 过滤器的主要逻辑在doFilter上
* 每次过滤时都会执行
*
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("MyFilter start");
Long startTime = new Date().getTime();
chain.doFilter(request, response);
System.out.println("MyFilter 耗时:" + (new Date().getTime() - startTime));
System.out.println("MyFilter end");
}
//销毁之前执行!在服务器关闭时销毁
@Override
public void destroy() {
System.out.println("MyFilter.destroy>>>>>>>>>");
}
}
2、实现Filter接口或者继承Filter接口的实现类, 使用@Configuration注解,用配置bean的方式装配
import javax.servlet.*;
import java.io.IOException;
import java.util.Date;
public class MyFilter implements Filter {
//创建之后,马上执行;Filter会在服务器启动时就创建!
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("MyFilter.init");
}
/**
* 过滤器的主要逻辑在doFilter上
* 每次过滤时都会执行
*
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("MyFilter start");
Long startTime = new Date().getTime();
chain.doFilter(request, response);
System.out.println("MyFilter 耗时:" + (new Date().getTime() - startTime));
System.out.println("MyFilter end");
}
//销毁之前执行!在服务器关闭时销毁
@Override
public void destroy() {
System.out.println("MyFilter.destroy>>>>>>>>>");
}
}
增加配置类
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public FilterRegistrationBean registrationBean(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.addUrlPatterns("/*");//过滤请求路径 所有请求
registrationBean.setName("myFilter");
registrationBean.setOrder(1);//值越小优先级越高
return registrationBean;
}
}
3、实现Filter接口或者继承Filter接口的实现类,使用@WebFilter注解+启动类@ServletComponentScan
import org.springframework.core.annotation.Order;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.Date;
@Order(value = 1)//值越小优先级越高
@WebFilter(urlPatterns = "/*" ,filterName = "myFilter")
public class MyFilter implements Filter {
//创建之后,马上执行;Filter会在服务器启动时就创建!
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("MyFilter.init");
}
/**
* 过滤器的主要逻辑在doFilter上
* 每次过滤时都会执行
*
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("MyFilter start");
Long startTime = new Date().getTime();
chain.doFilter(request, response);
System.out.println("MyFilter 耗时:" + (new Date().getTime() - startTime));
System.out.println("MyFilter end");
}
//销毁之前执行!在服务器关闭时销毁
@Override
public void destroy() {
System.out.println("MyFilter.destroy>>>>>>>>>");
}
}
注意!注意!注意!
1、WebFilter注解不要和@Component或@Configuration一同使用,@Component本身就会将filter注册到spring容器中,同时使用WebFilter会同时在生成一个filter,如果bean的名称不同,两个过滤器都会执行,导致webfilter无效拦截url,名称相同,spring默认是不允许定义相同名称bean的,可以通过 spring.main.allow-bean-definition-overriding=true重写bean
2、使用WebFilter时,启动类上要加入@ServletComponentScan注解才会生效。
@ServletComponentScan作用:
启动类上使用@ServletComponentScan 注解后
1、Servlet可以直接通过@WebServlet注解自动注册
2、Filter可以直接通过@WebFilter注解自动注册
3、Listener可以直接通过@WebListener 注解自动注册