springboot中使用filter
通过注解的方式实现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的更多相关文章
- SpringBoot系列:三、SpringBoot中使用Filter
在springboot中要使用Filter首先要实现Filter接口,添加@WebFilter注解 然后重写三个方法,下图示例是在Filter中过滤上一届中拿配置的接口,如果是这个接口会自动跳转到/P ...
- springboot中使用Filter、Interceptor和aop拦截REST服务
在springboot中使用rest服务时,往往需要对controller层的请求进行拦截或者获取请求数据和返回数据,就需要过滤器.拦截器或者切片. 过滤器(Filter):对HttpServletR ...
- springboot中使用filter用配置类方式
在03-springboot-web的Filter包下,创建HeFilter类 代码示例: package com.bjpowernode.springboot.filter; import java ...
- 如何把web.xml中的context-param、Servlet、Listener和Filter定义添加到SpringBoot中
把传统的web项目迁移到SpringBoot中,少不了web.xml中的context-param.Servlet.Filter和Listener等定义的迁移. 对于Servlet.Filter和Li ...
- springboot中filter的用法
一.在spring的应用中我们存在两种过滤的用法,一种是拦截器.另外一种当然是过滤器.我们这里介绍过滤器在springboot的用法,在springmvc中的用法基本上一样,只是配置上面有点区别. 二 ...
- Springboot中关于跨域问题的一种解决方法
前后端分离开发中,跨域问题是很常见的一种问题.本文主要是解决 springboot 项目跨域访问的一种方法,其他 javaweb 项目也可参考. 1.首先要了解什么是跨域 由于前后端分离开发中前端页面 ...
- springboot中配置过滤器以及可能出现的问题
在springboot添加过滤器有两种方式: 1.通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加c ...
- 在Spring-Boot中实现通用Auth认证的几种方式
code[class*="language-"], pre[class*="language-"] { background-color: #fdfdfd; - ...
- springboot中使用拦截器、监听器、过滤器
拦截器.过滤器.监听器在web项目中很常见,这里对springboot中怎么去使用做一个总结. 1. 拦截器(Interceptor) 我们需要对一个类实现HandlerInterceptor接 ...
随机推荐
- python配置主机名
.准备hosts模板 mkdir -p /k8s/profile cat >/k8s/profile/hosts<<EOF 192.168.0.91 test1 192.168.0. ...
- Python浮点型数据小数点的取舍
python默认的是17位小数的精度 1.round()内置方法 π=3.1415926535 new_num=round(π,2) #四舍五入保留两位小数 print(new_num) ...
- springboot和springcloud版本冲突问题
最近搭建eureka项目,出现boot和cloud版本不匹配错误,记录下来 2019-12-06 14:00:20.043 ERROR 180780 --- [ main] o.s.boot.Spri ...
- eclipse 安装TestNg
通过eclipse安装TestNg,过程如下: 1.点击help-->Install New Software 2.打开如下窗口,点击add,name自定义输入,Location中输入http: ...
- 【HANA系列】SAP HANA LEFT/RIGHT字符串截取
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA LEFT/RI ...
- caoz的梦呓:信息安全常识科普
猫宁!!! 参考链接:https://mp.weixin.qq.com/s/cl4TfOodBGSjUuEU8e0rGA 对方公众号:caoz的梦呓 前天在新加坡IC咖啡做了一场关于信息安全的常识普及 ...
- Misc题目
@freebuff教程https://www.freebuf.com/column/196815.html @巅峰极客wp https://www.anquanke.com/post/id/18914 ...
- Tensorf实战第九课(自编码AutoEncoder)
本节我们将了解神经网络进行非监督形式的学习,即autoencoder自编码 假设图片经过神经网络后再输出的过程,我们看作是图片先被压缩然后解压的过程.那么在压缩的时候,原有的图片质量被缩减,解压时用信 ...
- python 爬虫 urllib模块 url编码处理
案例:爬取使用搜狗根据指定词条搜索到的页面数据(例如爬取词条为‘周杰伦’的页面数据) import urllib.request # 1.指定url url = 'https://www.sogou. ...
- POP与OOP编程模式对比
面向过程:(procedure oriented programming 即:POP) 代表:C/C++ 向过程程序设计,它是以功能为中心来进行思考和组织的一种编程方式,强调的是系统的数据被加工和处理 ...