这里介绍两种几种种自定义filter的方式
- 通过
@WebFilter
+@ServletComponentScan
的方式 - 通过配置
FilterRegistrationBean
的方式 - 通过加注解
@component
等注入的方式
1.通过@WebFilter
+@ServletComponentScan
的方式
filter类
package com.d4c.custombean.filter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@Slf4j
@Order(1)
@WebFilter(filterName = "firstName",urlPatterns = "/hello/*")
public class FirstFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("FirstFilter init--------");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("FirstFilter doFilter--------");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
log.info("FirstFilter destroy--------");
}
}
启动类上要加@ServletComponentScan
package com.d4c.custombean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@SpringBootApplication
public class CustomBeanApplication {
public static void main(String[] args) {
SpringApplication.run(CustomBeanApplication.class, args);
}
}
@WebFilter常用属性
属性 | 类型 | 是否必需 | 说明 |
---|---|---|---|
asyncSupported | boolean | 否 | 指定Filter是否支持异步模式 |
dispatcherTypes | DispatcherType[] | 否 | 指定Filter对哪种方式的请求进行过滤。 支持的属性:ASYNC、ERROR、FORWARD、INCLUDE、REQUEST; 默认过滤所有方式的请求 |
filterName | String | 否 | Filter名称 |
initParams | WebInitParam[] | 否 | 配置参数 |
displayName | String | 否 | Filter显示名 |
servletNames | String[] | 否 | 指定对哪些Servlet进行过滤 |
urlPatterns/value | String[] | 否 | 两个属性作用相同,指定拦截的路径 |
过滤器的urlPatterns的过滤路径规则:
- 1.全路径匹配: /hello/do.html
- 2.部分路径匹配: /hello/*
- 3.通配符匹配 :/*
- 4.后缀名匹配 :*.html(注意:前面不加/)
@Order
@Orde注解是决定着执行顺序,而不是类的初始化顺序。order数字越小,优先级越高。最好,统一用一种方式定义filter,否则@Order可能不起作用(混乱)。
2.通过配置FilterRegistrationBean
的方式
filter类
package com.d4c.custombean.filter;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import java.io.IOException;
@Slf4j
public class SecondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("SecondFilter init--------");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("SecondFilter doFilter--------");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
log.info("SecondFilter destroy--------");
}
}
配置类
package com.d4c.custombean.config;
import com.d4c.custombean.filter.SecondFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyRegisterBean {
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean();
//注册自定义过滤器
bean.setFilter(new SecondFilter());
//过滤器名称
bean.setName("second filter bean");
//过滤所有路径
bean.addUrlPatterns("/do/*");
//优先级,数字越小,优先级越高
bean.setOrder(2);
return bean;
}
}
3.通过加注解@Component
等注入的方式
虽然这种方式能实现filter,但是不推荐。最好第一种或第二种方式选其一
filter类
package com.d4c.custombean.filter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@Slf4j
@Order(3)
//或者用@Configuration
@Component
public class ThirdFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("ThirdFilter init--------");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("ThirdFilter doFilter--------");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
log.info("ThirdFilter destroy--------");
}
}
网友评论