Springboot添加filter方法
在springboot添加filter有两种方式:
(1)、通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加cookie可能会存在写不进前端情况)
(2)、通过注解@WebFilter的方式
一、通过创建FilterRegistrationBean的方式创建多个filter以及设置执行顺序:
1、创建两个实现Filter接口的类TestFilter1 、TestFilter2
package com.aoxun.core.filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class TestFilter1 implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException {} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
} @Override
public void destroy() {} }
package com.aoxun.core.filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class TestFilter2 implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException {} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
} @Override
public void destroy() {} }
2、创建配置类
有多个filter就创建多个FilterRegistrationBean ,若需注明filter的执行顺序,可通过registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE - 1)配置,值越大,执行顺序越靠后
package com.aoxun.config.web; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.aoxun.core.filter.TestFilter1;
import com.aoxun.core.filter.TestFilter2; /**
* filter配置
* @author zcj
*
*/
@Configuration
public class FilterConfig2 { @Bean
public FilterRegistrationBean companyUrlFilterRegister() {
FilterRegistrationBean registration = new FilterRegistrationBean();
//注入过滤器
registration.setFilter(new TestFilter1());
//拦截规则
registration.addUrlPatterns("/*");
//过滤器名称
registration.setName("testFilter1");
//过滤器顺序
registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
return registration;
} @Bean
public FilterRegistrationBean outLinkSecurityFilterRegister() {
FilterRegistrationBean registration = new FilterRegistrationBean();
//注入过滤器
registration.setFilter(new TestFilter2());
//拦截规则
registration.addUrlPatterns("/*");
//过滤器名称
registration.setName("testFilter2");
//过滤器顺序
registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE - 1);
return registration;
} }
二、通过注解@WebFilter的方式
1、在启动类上增加@ServletComponentScan注解,自动扫描带有过滤器注解的包
2、在类上使用@WebFilter和@Order组合实现
package com.aoxun.core.filter; import java.io.IOException; 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 javax.servlet.annotation.WebFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.core.annotation.Order; @WebFilter(filterName="testFilter1",urlPatterns= {"/*"})
@Order(FilterRegistrationBean.LOWEST_PRECEDENCE)
public class TestFilter1 implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub } @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException { chain.doFilter(request, response); } @Override
public void destroy() {
// TODO Auto-generated method stub } }
(转自:https://www.cnblogs.com/begin2016/p/8947887.html)
Springboot添加filter方法的更多相关文章
- 对actuator的管理端点进行ip白名单限制(springBoot添加filter)
在我们的SpringCloud应用中,我们会引入actuator来进行管理和监控我们的应用 常见的有:http://www.cnblogs.com/yangzhilong/p/8378152.html ...
- Spring boot下添加filter
https://www.cnblogs.com/OnlyCT/p/7133639.html ****************************************************** ...
- 02-02:springboot 整合filter
1.通过注解扫描完成Filter组件的注册 1.1编写filter (添加拦截的servlet) //@WebFilter(filterName = "FirstFilter",u ...
- SpringBoot自定义Filter
SpringBoot自定义Filter SpringBoot自动添加了OrderedCharacterEncodingFilter和HiddenHttpMethodFilter,当然我们可以自定 义F ...
- 【SpringBoot】04.SpringBoot整合Filter的两种方式
SpringBoot整合Filter过滤器的两种方式: 1.通过扫描注解完成Filter组件注册 创建一个类,实现Filter接口,实现doFilter()方法 在该类使用注解@WebFilter,设 ...
- Asp.net MVC3 中,动态添加filter
Asp.net MVC3 中,动态添加filter filter是attribute,不支持泛型,传入的参数必须是固定的值.总之很受attribute本身的限制. 发现一篇老外的文章,动态设置filt ...
- Jetty添加Filter过滤器
1.Jetty嵌入到Spring项目 try { Server server = new Server(8080); WebAppContext context = new WebAppContext ...
- springboot添加邮件发送及压缩功能
springboot添加邮件发送及文件压缩功能 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190233.html 先来一段诗 ``` 就这样吧 忍受折磨 ...
- springboot添加多数据源连接池并配置Mybatis
springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018 ...
随机推荐
- redis-dump实现redis库迁移
最近公司有切换redis库的需求,找了个简单的redis迁移方法,不过也有缺点.就是对于实时性要求很高的redis库迁移无法做到数据的实时同步.不过对于简单的redis库备份和迁移还是可以的,各位看官 ...
- 常见的磁盘I/O和网络I/O优化技巧
磁盘I/O 优化 性能检测 应用程序通过访问磁盘来读取数据,而磁盘I/O 通常都是很耗时间的,所以一般我们来判断I/O是否有瓶颈的时候,就需要一些参数指标来参考. WAIT 指标 :压测应用程序,查看 ...
- Windows 2008 打开声音重定向来听到远程主机音频
Windows 2008 Server在默认情况下,是禁止使用桌面桌面连接后播放声音的(提示音频服务未启用).可以通过下面的方法启用音频: 管理工具->远程桌面服务->远程桌面会话主机配置 ...
- Windows server 1709(不含UI)模板部署
1.系统安装 在虚拟机导入安装镜像,客户端操作系统选择” windows server 2012”,虚拟磁盘类型选择”SCSI”:依照安装向导正确安装操作系统 2.安装vmware tools 选择虚 ...
- Windows -- cmd命令: ipconfig 和 nbtstat
1. ipconfig 命令格式及参数如下: 2. nbtstat 命令格式及参数如下:
- July 05th. 2018, Week 27th. Thursday
Pleasure in the job puts perfection in the work. 乐于工作才能有完美表现. From Aristole. Do you want promotion? ...
- MySQL 数据库死锁
数据库死锁 死锁的解决办法(1) 执行下面SQL,先查看哪些表被锁住了: select b.owner,b.object_name,a.session_id,a.locked_mode from v$ ...
- python-memcached学习笔记
介绍: memcached是免费.开源.高性能.分布式内存对象的缓存系统(键/值字典),旨在通过减轻数据库负载加快动态web应用程序的使用. 数据类型:只用一种字符串类型 1:安装 sudo apt- ...
- Jason Wang:记录自己所想所得(第一次作业)
这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 我在这个课程的目标是 学会现代软件工程的思想,运用到实际中去 这个作业在哪个具体方面帮助我实现目标 ...
- 关于pip安装时提示"pkg_resources.DistributionNotFound"错误
使用pip install --upgrade pip升级pip中途失败,再次安装pip,完成后出现如下错误: 尝试重新安装pip也不行,同样会出现上述问题. 此时我们查看/usr/bin/pip文件 ...