Spring Web项目都需要用到Filter来实现各种不同的能力,比如鉴权,限流,日志等。 而且有的Filter实现还需要注入Spring其他bean,这样就需要一些特殊的实现
AccessFilter
简单地打印请求url
代码语言:javascript复制public class AccessFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
log.info("AccessFilter:doFilter - {}", ((HttpServletRequest)servletRequest).getRequestURI());
filterChain.doFilter(servletRequest, servletResponse);
}
}
1. Spring bean
比如使用@Component
注解
@Component
public class AccessFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
log.info("AccessFilter:doFilter - {}", ((HttpServletRequest)servletRequest).getRequestURI());
filterChain.doFilter(servletRequest, servletResponse);
}
}
Servlet扫描
使用注解@ServletComponentScan
和@WebFilter
,实际上就是生成一个类型是FilterRegistrationBean
,名字是accessFilter
的bean并作为Filter生效.
@WebFilter
public class AccessFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
log.info("AccessFilter:doFilter - {}", ((HttpServletRequest)servletRequest).getRequestURI());
filterChain.doFilter(servletRequest, servletResponse);
}
}
- SpringSimpleWebApplication
@ServletComponentScan
@SpringBootApplication
public class SpringSimpleWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSimpleWebApplication.class, args);
}
}
手工注册: FilterRegistrationBean
会生成一个类型是FilterRegistrationBean
,名字是accessFilterRegister
的bean并作为Filter生效.
@Bean
public FilterRegistrationBean<AccessFilter> accessFilterRegister(AccessFilter accessFilter) {
FilterRegistrationBean<AccessFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(accessFilter);
registration.addUrlPatterns(filterUrl);
registration.setName("accessFilter");
registration.setOrder(1);
return registration;
}
实践
- 如果urlPatterns需要从配置文件加载: 那么选择
FilterRegistrationBean
- 如果既要动态加载urlPatterns,还要依赖spring其他的bean: 那么生成bean的同时,再使用
FilterRegistrationBean
@Slf4j
@Configuration
public class WebConfiguration {
@Value("${urlPattens:/}")
private String urlPatterns;
@Bean
public AccessFilter accessFilter() {
return new AccessFilter();
}
@Bean
public FilterRegistrationBean<AccessFilter> accessFilterRegister(AccessFilter accessFilter) {
FilterRegistrationBean<AccessFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(accessFilter);
registration.addUrlPatterns(urlPatterns);
registration.setOrder(1);
return registration;
}
}
- 同名的filter只会有一个生效