【Springboot】过滤器
Springboot实现过滤器
实现过滤器方式有两种:
- Filter过滤器具体实现类
- 通过@WebFilter 注解来配置
1、Filter过滤器具体实现类
1.1 实现Filter
@Component
@Slf4j
public class MyTestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("[ {} ] 创建啦...", this.getClass().getSimpleName());
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("[ {} ] 执行啦...", this.getClass().getSimpleName());
chain.doFilter(request, response);
}
@Override
public void destroy() {
log.info("[ {} ] 被摧毁啦...", this.getClass().getSimpleName());
}
}
1.2 向spring容器注册filter
@Configuration
public class FilterConfig {
@Resource
private MyTestFilter myTestFilter;
@Bean
public FilterRegistrationBean testFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean(myTestFilter);
registration.addUrlPatterns("/filter/*");
registration.setName("myTestFilter");
registration.setOrder(1);
return registration;
}
}
测试

2、通过@WebFilter 注解来配置
@Component
@WebFilter(urlPatterns = "/filter/*", filterName = "MyTestFilterByAnnotation")
@Slf4j
public class MyTestFilterByAnnotation implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("注解 [ {} ] 创建啦...", this.getClass().getSimpleName());
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
log.info("注解 [ {} ] 执行啦...", this.getClass().getSimpleName());
chain.doFilter(request, response);
}
@Override
public void destroy() {
log.info("注解 [ {} ] 被摧毁啦...", this.getClass().getSimpleName());
}
}
测试

【Springboot】过滤器的更多相关文章
- springboot过滤器的实现
springboot过滤器的实现 如下所示: import javax.servlet.*; import javax.servlet.annotation.WebFilter;import java ...
- 寻找写代码感觉(八)之SpringBoot过滤器的使用
一.什么是过滤器? 过滤器是对数据进行过滤,预处理过程,当我们访问网站时,有时候会发布一些敏感信息,发完以后有的会用*替代,还有就是登陆权限控制等,一个资源,没有经过授权,肯定是不能让用户随便访问的, ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第6节 SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener_24、深入SpringBoot过滤器和Servlet配置过滤器
笔记 1.深入SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter实战(核心知识) 简介:讲解SpringBoot里面Filter讲解和使用Servle ...
- SpringBoot 过滤器和拦截器
过滤器 实现过滤器需要实现 javax.servlet.Filter 接口.重写三个方法.其中 init() 方法在服务启动时执行,destroy() 在服务停止之前执行. 可用两种方式注册过滤器: ...
- springBoot 过滤器去除请求参数前后空格(附源码)
背景 : 用户在前端页面中不小心输入的前后空格,为了防止因为前后空格原因引起业务异常,所以我们需要去除参数的前后空格! 如果我们手动去除参数前后空格,我们可以这样做 @GetMapping(value ...
- SpringBoot(8) SpringBoot过滤器Filter
1.SpringBoot启动默认加载的Filter characterEncodingFilter hiddenHttpMethodFilter httpPutFormContentFilter re ...
- SpringBoot 过滤器, 拦截器, 监听器 对比及使用场景
1. 过滤器 (实现 javax.servlet.Filter 接口) ① 过滤器是在web应用启动的时候初始化一次, 在web应用停止的时候销毁. ② 可以对请求的URL进行过滤, 对敏感词过滤, ...
- SpringBoot过滤器过滤get及post请求中的XSS和SQL注入
1.创建XssAndSqlHttpServletRequestWrapper包装器,这是实现XSS过滤的关键,在其内重写了getParameter,getParameterValues,getHead ...
- Springboot 过滤器和拦截器详解及使用场景
一.过滤器和拦截器的区别 1.过滤器和拦截器触发时机不一样,过滤器是在请求进入容器后,但请求进入servlet之前进行预处理的.请求结束返回也是,是在servlet处理完后,返回给前端之前. 2.拦截 ...
- Springboot过滤器注解简笔
对多个过滤的注解 @WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.js ...
随机推荐
- sql 时间函数
计算时间间隔 day datediff(大日期, 小日期) SELECT datediff('2009-07-31', '2009-07-30') month, year, second timest ...
- 【树莓派】Docker安装calibre-web搭建在线书城
一.下载docker镜像 sudo docker pull johngong/calibre-web 二.创建calibre-web镜像的映射目录,存放配置文件&书籍 mkdir /home/ ...
- MDC轻量化日志链路跟踪的若干种应用场景
"If debugging is the process of removing software bugs, then programming must be the process of ...
- Node.js躬行记(27)——接口管理
在页面发生线上问题时,你要做的事情就是去查接口,响应数据是否正确,查接口的方法有两种: 第一种是在浏览器中打开地址,但是你必须得知道详细的 URL,并且有些页面还需要附带参数. 第二种是打开编辑器,启 ...
- ARL:资产侦察灯塔系统
资产灯塔,不仅仅是域名收集 功能简介 "挖洞神器"资产安全灯塔(ARL),旨在快速侦察与目标关联的互联网资产,构建基础资产信息库. 协助甲方安全团队或者渗透测试人员有效侦察和检索资 ...
- [OpenCV-Python] 9 图像的基础操作
文章目录 OpenCV-Python: 核心操作 9 图像的基础操作 9.1 获取并修改像素值 9.2 获取图像属性 9.3 图像 ROI 9.4 拆分及合并图像通道 9.5 为图像扩边(填充) Op ...
- Python-BeautifulReport的简单使用
一.简介 BeautifulReport.report report ( filename -> 测试报告名称, 如果不指定默认文件名为report.html description -> ...
- 快速求popcount的和
前置知识 \(\text{popcount}(n)\) 表示将 \(n\) 转为二进制后的数中 \(1\) 的个数. 结论 \[\sum_{i=1}^{n} \text{ popcount}(i)=\ ...
- 【解决方法】windos server 2019 在批量创建DNS的正向与反向记录时,提示报错: >Command failed: ERROR_ACCESS_DENIED 5 0x5
目录-快速跳转 问题描述 原因分析: 解决方案: 附言: 问题描述 操作环境与场景: 在 VM 内 windos server 2019 在批量创建DNS的正向与反向记录时,提示报错: Command ...
- 【学习笔记】【题解】树形依赖 DP 选做
地址:https://www.cnblogs.com/FReQuenter5156/p/shuxingyilaidp.html/ 简介 这类背包本质上是分组背包问题. 将一个节点的每一棵子树看作一组, ...