通过注解的方式实现filter过滤器。

创建Filter包,并在该包下创建MyFilter

示例代码:

package com.bjpowernode.springboot.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException; @WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("您的请求已经进入MyFilter过滤器……");
filterChain.doFilter(servletRequest,servletResponse);
}
}

注意需要加注解,配置需要拦截哪些路径。过滤的逻辑在输出语句处实现。并且如果不阻断请求,需要让请求过滤链继续。

需要在启动类上加包扫描注解,代码如下:

package com.bjpowernode.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication
@MapperScan("com.bjpowernode.springboot.mapper") //取代在mapper接口上配置的@Mapper
@ServletComponentScan(basePackages = {"com.bjpowernode.springboot.servlet",
"com.bjpowernode.springboot.filter"})//扫描Servlet包 扫描filter包
@EnableTransactionManagement //开启事务支持
public class WebApplication { public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
} }

注解:@ServletComponentScan是扫描包的。

springboot中使用filter的更多相关文章

  1. SpringBoot系列:三、SpringBoot中使用Filter

    在springboot中要使用Filter首先要实现Filter接口,添加@WebFilter注解 然后重写三个方法,下图示例是在Filter中过滤上一届中拿配置的接口,如果是这个接口会自动跳转到/P ...

  2. springboot中使用Filter、Interceptor和aop拦截REST服务

    在springboot中使用rest服务时,往往需要对controller层的请求进行拦截或者获取请求数据和返回数据,就需要过滤器.拦截器或者切片. 过滤器(Filter):对HttpServletR ...

  3. springboot中使用filter用配置类方式

    在03-springboot-web的Filter包下,创建HeFilter类 代码示例: package com.bjpowernode.springboot.filter; import java ...

  4. 如何把web.xml中的context-param、Servlet、Listener和Filter定义添加到SpringBoot中

    把传统的web项目迁移到SpringBoot中,少不了web.xml中的context-param.Servlet.Filter和Listener等定义的迁移. 对于Servlet.Filter和Li ...

  5. springboot中filter的用法

    一.在spring的应用中我们存在两种过滤的用法,一种是拦截器.另外一种当然是过滤器.我们这里介绍过滤器在springboot的用法,在springmvc中的用法基本上一样,只是配置上面有点区别. 二 ...

  6. Springboot中关于跨域问题的一种解决方法

    前后端分离开发中,跨域问题是很常见的一种问题.本文主要是解决 springboot 项目跨域访问的一种方法,其他 javaweb 项目也可参考. 1.首先要了解什么是跨域 由于前后端分离开发中前端页面 ...

  7. springboot中配置过滤器以及可能出现的问题

    在springboot添加过滤器有两种方式: 1.通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加c ...

  8. 在Spring-Boot中实现通用Auth认证的几种方式

    code[class*="language-"], pre[class*="language-"] { background-color: #fdfdfd; - ...

  9. springboot中使用拦截器、监听器、过滤器

     拦截器.过滤器.监听器在web项目中很常见,这里对springboot中怎么去使用做一个总结. 1. 拦截器(Interceptor)   我们需要对一个类实现HandlerInterceptor接 ...

随机推荐

  1. 性能测试之JMeter远程模式

    性能测试之JMeter远程模式 事实上,你的JMeter客户端机器是不能表现出完美的压力请求,来模拟足够多的用户或由于网络限制去向服务施加压力,一种解决方法是通过一个JMeter去控制多个/远程JMe ...

  2. 关于ckeditor粘贴图片自动上传

    在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案. 其原理为一下步骤: 监听粘贴事件:[用于插入图片] 获取光标位置:[记录图片插入位置] 获取剪切板内容: ...

  3. python高级 之(五) --- 文件操作

    文件操作 """ 在程序中操作的文件内容: 1. 读取文件中的内容 2. 向文件中写入内容 首先: 在程序中与文件建立一个通道,通过通道操作文件指针,达到所要的结果 向文 ...

  4. web题

    @php绕过https://www.cnblogs.com/leixiao-/p/9786496.html @step1:F12 step2:抓包 @任何url  http://118.25.14.4 ...

  5. Git_mergetool_tutorial(转载)

    原文链接:https://gist.github.com/karenyyng/f19ff75c60f18b4b8149#table-of-content Table Of Content Skip t ...

  6. PHP中的接口

    对象接口 使用接口(interface),可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容. 接口是通过 interface 关键字来定义的,就像定义一个标准的类一样,但其中定义所有的方 ...

  7. 【面试向】hihoCoder 1994 树与落叶

    题目链接 Implementation int n, q; scan(n,q); vi p(n + 1); vi nson(n + 1); up (i, 1, n) { scan(p[i]); nso ...

  8. HDU-5238 Calculator

    题目描述 给定一个关于 \(x\) 的表达式,形如下例:\(×4+2^3+8×6\) 按如下方法计算:\((((x×4)+2)^3+8)×6\) 运算符只有 加号,乘号,幂运算三种,给定的式子中有 \ ...

  9. requests库爬取豆瓣热门国产电视剧数据并保存到本地

    首先要做的就是去豆瓣网找对应的接口,这里就不赘述了,谷歌浏览器抓包即可,然后要做的就是分析返回的json数据的结构: https://movie.douban.com/j/search_subject ...

  10. 如何實現输入字符串This is an Apple on eBay 输出 Siht si na Elppa no yAbe

    <?php $str = "This is an Apple on eBay"; //定义字符串 $len = strlen($str); //字符串长度 $sup = [] ...