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】过滤器的更多相关文章

  1. springboot过滤器的实现

    springboot过滤器的实现 如下所示: import javax.servlet.*; import javax.servlet.annotation.WebFilter;import java ...

  2. 寻找写代码感觉(八)之SpringBoot过滤器的使用

    一.什么是过滤器? 过滤器是对数据进行过滤,预处理过程,当我们访问网站时,有时候会发布一些敏感信息,发完以后有的会用*替代,还有就是登陆权限控制等,一个资源,没有经过授权,肯定是不能让用户随便访问的, ...

  3. 小D课堂 - 零基础入门SpringBoot2.X到实战_第6节 SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener_24、深入SpringBoot过滤器和Servlet配置过滤器

    笔记 1.深入SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter实战(核心知识)     简介:讲解SpringBoot里面Filter讲解和使用Servle ...

  4. SpringBoot 过滤器和拦截器

    过滤器 实现过滤器需要实现 javax.servlet.Filter 接口.重写三个方法.其中 init() 方法在服务启动时执行,destroy() 在服务停止之前执行. 可用两种方式注册过滤器: ...

  5. springBoot 过滤器去除请求参数前后空格(附源码)

    背景 : 用户在前端页面中不小心输入的前后空格,为了防止因为前后空格原因引起业务异常,所以我们需要去除参数的前后空格! 如果我们手动去除参数前后空格,我们可以这样做 @GetMapping(value ...

  6. SpringBoot(8) SpringBoot过滤器Filter

    1.SpringBoot启动默认加载的Filter characterEncodingFilter hiddenHttpMethodFilter httpPutFormContentFilter re ...

  7. SpringBoot 过滤器, 拦截器, 监听器 对比及使用场景

    1. 过滤器 (实现 javax.servlet.Filter 接口) ① 过滤器是在web应用启动的时候初始化一次, 在web应用停止的时候销毁. ② 可以对请求的URL进行过滤, 对敏感词过滤, ...

  8. SpringBoot过滤器过滤get及post请求中的XSS和SQL注入

    1.创建XssAndSqlHttpServletRequestWrapper包装器,这是实现XSS过滤的关键,在其内重写了getParameter,getParameterValues,getHead ...

  9. Springboot 过滤器和拦截器详解及使用场景

    一.过滤器和拦截器的区别 1.过滤器和拦截器触发时机不一样,过滤器是在请求进入容器后,但请求进入servlet之前进行预处理的.请求结束返回也是,是在servlet处理完后,返回给前端之前. 2.拦截 ...

  10. Springboot过滤器注解简笔

    对多个过滤的注解      @WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.js ...

随机推荐

  1. ORA-12154: TNS:could not resolve the connect identifier specified--sys密码包含@符号

    问题描述:在操作系统登录数据库时,由于忘记了sys密码,重新修改的sys密码包含@符号,登录时报错, ORA-12154: TNS:could not resolve the connect iden ...

  2. Redis(三)jedis与锁

    1 Jedis 引入依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis ...

  3. Mysql8.0为什么取消了缓存查询的功能

    首先我们介绍一下MySQL的缓存机制 [MySQL缓存机制]简单的说就是缓存sql文本及查询结果,如果运行完全相同的SQL,服务器直接从缓存中取到结果,而不需要再去解析和执行SQL. 但如果表中任何数 ...

  4. IE盒模型和标准盒模型之间的差别

    1.W3C标准盒子模型 w3c盒子模型的范围包括margin.border.padding.content,并且content部分不包含其他部分 2.IE盒子模型 IE盒子模型的范围包括margin. ...

  5. [人脸活体检测] 论文:Searching Central Difference Convolutional Networks for Face Anti-Spoofing

    文章目录 Searching Central Difference Convolutional Networks for Face Anti-Spoofing 1 导读 2 简介 3 方法 中心差分卷 ...

  6. NC54585 小魂和他的数列

    题目链接 题目 题目描述 一天,小魂正和一个数列玩得不亦乐乎. 小魂的数列一共有n个元素,第i个数为Ai. 他发现,这个数列的一些子序列中的元素是严格递增的. 他想知道,这个数列一共有多少个长度为K的 ...

  7. Python数据分析中 melt()函数的一些用法

    melt()函数是一个数据重塑工具,用于将宽格式数据转换为长格式数据(Unpivot a DataFrame from wide to long format, optionally leaving ...

  8. 2023-05-10:给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表 如果在二叉树中,存在一条一直向下的路径 且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值,

    2023-05-10:给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表 如果在二叉树中,存在一条一直向下的路径 且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值, ...

  9. bean的作用域和@scope注解

    bean的作用域由@scope注解来修改,该注解有五个不同的取值,分别是:singleton.prototype.request.session.global-session. singleton,在 ...

  10. 2022-09-03:n块石头放置在二维平面中的一些整数坐标点上 每个坐标点上最多只能有一块石头 如果一块石头的 同行或者同列 上有其他石头存在,那么就可以移除这块石头。 给你一个长度为 n 的数组

    2022-09-03:n块石头放置在二维平面中的一些整数坐标点上 每个坐标点上最多只能有一块石头 如果一块石头的 同行或者同列 上有其他石头存在,那么就可以移除这块石头. 给你一个长度为 n 的数组 ...