Spring Boot 之FilterRegistrationBean  --支持web Filter 排序的使用
Spring 提供了FilterRegistrationBean类,此类提供setOrder方法,可以为filter设置排序值,
让spring在注册web filter之前排序后再依次注册。

写一个普通的filter:

package com.sdcuike.practice.web2;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.sdcuike.practice.config.CommonConfig; public class FilterDemo3 implements Filter {
private final Logger log = LoggerFactory.getLogger(getClass()); @Resource
private CommonConfig commonConfig; @Override
public void destroy() {
log.info("" + getClass() + " destroy"); } @Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
log.info("" + getClass() + " doFilter " + commonConfig);
arg2.doFilter(arg0, arg1); } @Override
public void init(FilterConfig arg0) throws ServletException {
log.info("" + getClass() + " init"); } }

配置如下:

package com.sdcuike.practice.web2;

import javax.servlet.Filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.sdcuike.spring.extend.web.MvcConfigurerAdapter; /**
* web 组件配置
*
* @author sdcuike
* <p>
* Created on 2017-02-10
* <p>
* 自定义注入,并支持依赖注入,组件排序
*/
@Configuration
public class WebComponent2Config { @Bean
public FilterRegistrationBean filterDemo3Registration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(filterDemo3());
registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("filterDemo3");
registration.setOrder(6);
return registration;
} @Bean
public FilterRegistrationBean filterDemo4Registration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(filterDemo4());
registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("filterDemo4");
registration.setOrder(7);
return registration;
} @Bean
public Filter filterDemo3() {
return new FilterDemo3();
} @Bean
public Filter filterDemo4() {
return new FilterDemo4();
} }
利用这种方式,我们可以对filter排序,可自行测试,源码:

————————————————
版权声明:本文为CSDN博主「A_Beaver」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/doctor_who2004/article/details/56055505

Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)的更多相关文章

  1. Spring Boot 2.X 对 web 的开发支持(二)

    Spring Boot 2.X 对 web 的支持开发 上章节的 Spring Boot 的入门案例,我们感受到 Spring Boot 简单的配置即可运行项目. 今天了解 Spring Boot 对 ...

  2. 从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

    在Spring Boot中整合Servlet.Filter.Listener的方式 写在前面 从零开始的Spring Boot(1.搭建一个Spring Boot项目Hello World):http ...

  3. Spring Boot 添加JSP支持【转】

    Spring Boot 添加JSP支持 大体步骤: (1)            创建Maven web project: (2)            在pom.xml文件添加依赖: (3)     ...

  4. [原创]Spring boot 框架构建jsp web应用

    说明 Spring boot支持将web项目打包成一个可执行的jar包,内嵌tomcat服务器,独立部署 为支持jsp,则必须将项目打包为war包 pom.xml中设置打包方式 <packagi ...

  5. Spring Boot Security And JSON Web Token

    Spring Boot Security And JSON Web Token 说明 流程说明 何时生成和使用jwt,其实我们主要是token更有意义并携带一些信息 https://github.co ...

  6. Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知

    1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...

  7. SpringBoot实战(十)之使用Spring Boot Actuator构建RESTful Web服务

    一.导入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  8. 初入spring boot(四 )web项目

    1. 模板引擎 spring boot提供了大量的模板引擎,包括FreeMark.Groovy.Thymeleaf.Velocity等,但spring boot中推荐用Thymeleaf,因为Thym ...

  9. 使用Spring Boot来加速Java web项目的开发

    我想,现在企业级的Java web项目应该或多或少都会使用到Spring框架的. 回首我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用 ...

随机推荐

  1. Spring (2)框架

    Spring第二天笔记 1. 使用注解配置Spring入门 1.1. 说在前面 学习基于注解的IoC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦 ...

  2. Python 的 Mixin 类(转)

    转1:https://www.cnblogs.com/aademeng/articles/7262520.html 转2:https://blog.csdn.net/u010377372/articl ...

  3. 应用人员反馈报错,ORA-03137: TTC protocol internal error : [12333]

    一.报错现象 应用人员反馈连接不上数据库,连接报错. 我们使用PLSQL发现可以连接数据库,但是数据库DB Alert存在如下报错信息 DB AlertFri Oct :: Errors ): ORA ...

  4. 怎样获取当前对象的原型对象prototype

    1. 使用 Object.getPrototypeOf(); function Person(name){ this.name = name; } var lilei = new Person(&qu ...

  5. springboot mvc自动配置(三)初始化mvc的组件

    所有文章 https://www.cnblogs.com/lay2017/p/11775787.html 正文 在springboot mvc自动配置的时候,获得了DispatcherServlet和 ...

  6. Unity 宽度适配 NGUI

    这是很久之前写的一篇Note,现在移到Blog上来,可能有些参数,NGUI插件等等不和现在版本相同.不过大概的思路应该不会错. ps: 可能有部分內容是摘抄自其他作者,没办法考证了,如有请务必联系我. ...

  7. CSS最常用的三种选择器

    标签选择器 样式的名称和标签的名称相同,如示例中的p标签,则对应名称为p的样式,若页面中有多个p标签,则这些p标签共同享用该样式 p{ color:blue; } <p>标签选择器< ...

  8. css图片上加文字

    第一种方法: 添加一个DIV,采用绝对定位,图片所属DIV为基准 <div style="position:relative;width:100px;height:100px;&quo ...

  9. XML文件解析之SAX解析

    使用DOM解析的时候是需要把文档的所有内容读入内存然后建立一个DOM树结构,然后通过DOM提供的接口来实现XML文件的解析,如果文件比较小的时候肯定是很方便的.但是如果是XML文件很大的话,那么这种方 ...

  10. redis集群1

    redis-trib.rb命令详解   redis-trib.rb是官方提供的Redis Cluster的管理工具,无需额外下载,默认位于源码包的src目录下,但因该工具是用ruby开发的,所以需要准 ...