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. NoSQL数据库一Redis基本使用

    基本操作 参考教程:https://www.yiibai.com/redis/Redis 是 Key-Value 内存数据库,操作是通过各种指令进行的,比如 SET 指令可以设置键值对,而 GET 指 ...

  2. Mathtype 问题汇总(3)

    1. 调整行间距 2. 调整字号 在 MathType 中,选择「大小 > 定义」将对话框中「完整」所对应的值改为和文字大小匹配的pt(磅值),这样便可以解决在文字编写的 Word 文档中某一行 ...

  3. Django多对多关系建立及Form组件

    目录 Django多对多关系 1.创建方式一全自动 2.创建方式二纯手撸 3.半自动(推荐使用) forms校验组件 使用forms组件实现注册功能 form常用字段和插件 数据校验 钩子函数 HOO ...

  4. 1190: 零起点学算法97——A == B ?(Java)

    WUSTOJ 1190: 零起点学算法97--A == B ? Description Give you two integer numbers A and B, if A is equal to B ...

  5. harbor上传镜像

    在harbor服务器 1. 下载测试上传使用的镜像docker pull hello-world2. 打tagdocker tag docker.io/hello-world:latest 172.1 ...

  6. Qt中的常用容器类(解释比较全面,有插图)

    在Qt库中为我们提供了一系列的基于模板的容器类.这些类可以被用来存储特定类型的项.例如,如果你需要一个大小可以变得QString数组,那么可以使用QVector<QString>. 这些容 ...

  7. 15-MySQL DBA笔记-运维管理

    第15章 运维管理 随着各种技术的快速发展,现今的DBA可以比以前的DBA维护多得多的数据库实例.DBA已经越来越像一个资源的管理者,而不是简单的操作步骤执行人.本章将为读者介绍规模化运维之道.首先, ...

  8. 数据库及MYSQL基础(2)

    数据库及MySQL基础(1) SQL进阶及查询练习 1,单表的查询练习 SELECT * FROM emp WHERE deptno=30; SELECT ename,empno,deptno FRO ...

  9. springboot启动流程(九)ioc依赖注入

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 在前面的几篇文章中,我们多次提到这么一个转化过程: Bean配置 --> Bean ...

  10. 关于el-select 单选与多选切换的时候报错的解决办法

    错误: 出错原因: 估计是单选切换到多选的时候元素没有刷新的原因,猜测 解决办法: 1.在el-select上面加上一个条件判断, 条件判断中绑定一个变量值 例如 :multiple="is ...