一、代码方式注册
需要使用 Spring Boot 提供的 FilterRegistrationBean 注册 Filter
1. 创建一个自定义Filter
@Slf4j
public class TestFilter implements Filter {
private String loginUrl;
@Override
public void init(FilterConfig config) {
this.loginUrl = config.getInitParameter("edu.yale.its.tp.cas.client.filter.loginUrl");
log.info("TestFilter初始化,URL:{}",this.loginUrl);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("TestFilter过滤处理");
chain.doFilter(request, response);
}
@Override
public void destroy() {
log.info("TestFilter销毁");
}
}
2. 在@Configuration 配置类内实现一个 FilterRegistrationBean 对象,且完成自定义的TestFilter注册,这里和xml效果是一样的。
@Configuration public class FilterConfiguration { @Bean public FilterRegistrationBean testFilterFilterRegistrationBean() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new TestFilter()); registration.addUrlPatterns("/*"); // 传入参数 Map<String, String> initParameters = new HashMap<String, String>(); initParameters.put("edu.yale.its.tp.cas.client.filter.loginUrl", "123456789"); registration.setInitParameters(initParameters); // 优先级 registration.setOrder(2); return registration; } }
二、注解方式注册
1. 使用@WebFilter注解进行修饰,表示该类是一个Filter类
@Slf4j @WebFilter(urlPatterns = "/*", filterName = "annotationFilter") public class AnnotationFilter implements Filter { @Override public void init(FilterConfig config) { log.info("Filter初始化中"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { log.info("进行过滤处理"); chain.doFilter(request, response); } @Override public void destroy() { log.info("Filter销毁"); } }
2. 在启动类上使用@ServletComponentScan 注解
(使用@ServletComponentScan注解后,Servlet、Filter、Listener 才可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,且无需其它代码)
@SpringBootApplication
@ServletComponentScan("filter") // 解释下:括号内是要扫描的包路径,你可能不需要,我是因为filter包在启动类外层
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
缺点:有多个Filter时不能指定优先级,默认是按类名排列优先级
传说中的方法:直接在Filter类上添加下面两句注解,也可简单的使用过滤器,无需其它配置 @Order(Ordered.LOWEST_PRECEDENCE -1) @Component 缺点:不能指定拦截路径,默认就是 (/*)
@Order(Ordered.HIGHEST_PRECEDENCE) 级别最高、@Order(Ordered.LOWEST_PRECEDENCE) 级别最低
总结:
简单的Filter直接用注解
第三方 jar 包内的 Filter 可能没有注解,也没法修改,就需要通过代码方式注册,如CAS统一身份认证
public FilterRegistrationBean casFilter() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new CASFilter()); registration.addUrlPatterns("/*"); Map<String, String> initParameters = new HashMap<String, String>(); initParameters.put("edu.yale.its.tp.cas.client.filter.loginUrl", "https://xxx.xxx.xxx.cn/authserver/login"); initParameters.put("edu.yale.its.tp.cas.client.filter.validateUrl", "https://xxx.xxx.xxx.cn/authserver/serviceValidate"); initParameters.put("edu.yale.its.tp.cas.client.filter.serverName", "http://localhost:8080"); initParameters.put("edu.yale.its.tp.cas.client.filter.needSSOLogout", "true"); registration.setInitParameters(initParameters); //设置优先级别 registration.setOrder(2); return registration; }