Spring Boot中过滤器Filter具体实现
1.通过 @Bean 注解来配置
实现Filter接口
package com.lay.spring.filter.demo01;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @Description:
* @Author: lay
* @Date: Created in 10:00 2019/1/14
* @Modified By:IntelliJ IDEA
*/
@Component
public class MyFilter1 implements Filter {
private static final Logger log= LoggerFactory.getLogger(MyFilter1.class);
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request= (HttpServletRequest) servletRequest;
log.info("MyFilter1过滤器,url:{}",request.getRequestURI());
filterChain.doFilter(servletRequest,servletResponse);
}
}
配置类
package com.lay.spring.filter.demo01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
/**
* @Description: filter过滤器注册,如没有FilterRegistrationBean进行注册,则默认全部路径
* @Author: lay
* @Date: Created in 10:06 2019/1/14
* @Modified By:IntelliJ IDEA
*/
@Configuration
@ServletComponentScan(basePackages = "com.lay.spring.filter.demo01")
public class MyFilterConfig {
/**
*
* @Description: 采用spring注入的方式,myFilter1添加@Component注解
* @param:
* @param myFilter1
* @return: org.springframework.boot.web.servlet.FilterRegistrationBean
* @auther: lay
* @date: 11:04 2019/1/14
*/
@Bean
public FilterRegistrationBean filterRegistrationBean(@Autowired Filter myFilter1){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(myFilter1);//注册过滤器
filterRegistrationBean.setOrder(0);//设置过滤器顺序
filterRegistrationBean.addUrlPatterns("/filter/*");//匹配路径/filter/*
return filterRegistrationBean;
}
/**
*
* @Description: 采用new方式进行注册
* @param:
* @return: org.springframework.boot.web.servlet.FilterRegistrationBean
* @auther: lay
* @date: 11:05 2019/1/14
*/
@Bean
public FilterRegistrationBean filterRegistrationBean2(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter2());//注册过滤器
filterRegistrationBean.setOrder(1);//设置过滤器顺序
filterRegistrationBean.addUrlPatterns("/*");//匹配路径,全部
return filterRegistrationBean;
}
}
配置FilterRegistrationBean
类来进行Filter
过滤器的注册,addUrlPatterns
方法匹配路径,如果有多个过滤器,可以使用setOrder
方法进行顺序设置,数字越小优先级越高。
注:如果没有对FilterRegistrationBean
进行配置注册,但是过滤器添加了@Component
组件注解 ,同时启动类开启了@ServletComponentScan
组件扫描,那么过滤器也会生效,匹配路径默认是/*
全部。
2.通过 @WebFilter来配置
同样实现Filter接口
package com.lay.spring.filter.demo02;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @Description:
* @Author: lay
* @Date: Created in 10:49 2019/1/14
* @Modified By:IntelliJ IDEA
*/
@WebFilter(urlPatterns = "/*",filterName = "my-filter3",displayName = "my-filter3")
public class MyFilter3 implements Filter {
private static final Logger log= LoggerFactory.getLogger(MyFilter3.class);
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request= (HttpServletRequest) servletRequest;
log.info("MyFilter3过滤器,url:{}",request.getRequestURI());
filterChain.doFilter(servletRequest,servletResponse);
}
}
@WebFilter 的属性
属性名 | 类型 | 描述 |
---|---|---|
filterName | String | 指定过滤器的 name 属性,等价于 |
value | String[] | 该属性等价于 urlPatterns 属性。但是两者不应该同时使用。 |
urlPatterns | String[] | 指定一组过滤器的 URL 匹配模式。等价于 标签。 |
servletNames | String[] | 指定过滤器将应用于哪些 Servlet。取值是 @WebServlet 中的 name 属性的取值,或者是 web.xml 中 的取值。 |
dispatcherTypes | DispatcherType | 指定过滤器的转发模式。具体取值包括: ASYNC、ERROR、FORWARD、INCLUDE、REQUEST。 |
initParams | WebInitParam[] | 指定一组过滤器初始化参数,等价于 标签。 |
asyncSupported | boolean | 声明过滤器是否支持异步操作模式,等价于 标签。 |
description | String | 该过滤器的描述信息,等价于 标签。 |
displayName | String | 该过滤器的显示名,通常配合工具使用,等价于 标签。 |
然后在启动类上添加@ServletComponentScan
注解使@WebFilter
注解生效
package com.lay.spring.filter.demo02;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @Description:
* @Author: lay
* @Date: Created in 11:16 2019/1/14
* @Modified By:IntelliJ IDEA
*/
@Configuration
@ServletComponentScan(basePackages = "com.lay.spring.filter.demo02")
public class MyFilterConfig2 {
}
对于多个由@WebFilter
注解注册的过滤器,其顺序是由类名决定的,根据A~Z
进行排序。