springboot @WebFilter过滤器的使用
过滤器的用法就不多说了
新建Filter的继承类:MemberFilter(放置包需要注意)
@WebFilter(urlPatterns = "/*")
@Order(1)
public class MemberFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException { } @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.err.println("自定义过滤器MemberFilter->doFilter");
} @Override
public void destroy() { }
}
配合@order使用,生命排序。
过滤器不在启动类扫描范围时,需要注意的是在启动类需要添加@ServletComponentScan扫描,其中包名自定义
@ServletComponentScan(basePackages = {"com.example.controller.filter"})
使用bean的情况下,jar打包是可以直接@Autowired使用的,转换war打包方式部署,获取的bean为null,这时候只能手动添加了
新建@Configuration注解类:FilterRegistrationConfig
@Configuration
public class FilterRegistrationConfig { @Bean
public MemberFilter memberFilter() {
return new MemberFilter();
} @Bean
public FilterRegistrationBean filterProxy() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
DelegatingFilterProxy httpBasicFilter = new DelegatingFilterProxy();
registrationBean.setFilter(httpBasicFilter);
Map<String, String> m = new HashMap<String, String>();
m.put("targetBeanName", "memberFilter");
m.put("targetFilterLifecycle", "true");
registrationBean.setInitParameters(m);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/*");
registrationBean.setUrlPatterns(urlPatterns);
return registrationBean;
}
}
这时候还需要注意假若使用了shiro,况且上述注册的过滤器有使用到的情况下,shiroFiler的过滤必须定义在前(解决shiro报错找不到原因)
@Bean
public FilterRegistrationBean delegatingFilterProxy() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
DelegatingFilterProxy proxy = new DelegatingFilterProxy();
proxy.setTargetFilterLifecycle(true);
proxy.setTargetBeanName("shiroFilter");
filterRegistrationBean.setFilter(proxy);
return filterRegistrationBean;
}
springboot @WebFilter过滤器的使用的更多相关文章
- SpringBoot学习(八)-->SpringBoot之过滤器、监听器
本文将直接使用@WebFilter和@WebListener的方式,完成一个Filter 和一个 Listener. 过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet ...
- SpringBoot图文教程6—SpringBoot中过滤器的使用
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文系列教程技术大纲 鹿老师的Java笔记 SpringBo ...
- springboot 增加过滤器方法
在访问服务器时,我们需要控制用户是否允许权限,这个时候可以使用过滤器. 在springboot 配置过滤器的方法如下: 编写过滤器代码: package com.neo.filter; import ...
- springboot jsp,过滤器,拦截器
springboot使用jsp,过滤器,拦截器(拦截器与过滤器区别重点) jsp使用配置 一 创建springboot项目在maven中暂时只添加两个Dependencies :devtools(热部 ...
- Springboot @webfilter @order filter过滤器
我们使用@WebFilter注解的时候发现注解里面没有提供可以控制执行顺序的参数 @WebFilter 的属性 属性名 类型 描述 filterName String 指定过滤器的 name 属性,等 ...
- springboot(五)过滤器和拦截器
前言 过滤器和拦截器二者都是AOP编程思想的提现,都能实现诸如权限检查.日志记录等.二者有一定的相似之处,不同的地方在于: Filter是servlet规范,只能用在Web程序中,而拦截器是Sprin ...
- springBoot(6)---过滤器,监听器,拦截器
过滤器,监听器,拦截器 一.理解它们 看里十几篇博客,总算有点小明白,总的来讲,两张图可以让我看明白点. 通过两幅图我们可以理解拦截器和过滤器的特点 1.过滤器 过滤器是在请求进入tomcat容器后, ...
- Springboot自定义过滤器Filter
前言:自己写了个Springboot项目,最近写的功能越来越多,结合业务已经要写过滤器Filter来过滤处理一些请求. 在网上看了几篇博客,总结如下: 过滤器配置方式有两种: 1.通过@WebFilt ...
- SpringBoot 的过滤器
在Springboot里面读封装的一些常用的API,当然对过滤器也不类外了. 首先讲下Spring中的AOP的理解: AOP不是一种具体的技术,而是一种编程思想.在面向对象编程的过程中,我们很容易通过 ...
随机推荐
- 关于c# winform使用FidderCore.dll 遇到的一些问题,请求支援
小弟最近再研究winform用fidder抓取包的过程.开始都很顺利,并且成功开启了代理网络.同时手机也设置代理,并且手机可以上网,而且电脑也能抓到手机的请求. 但是遇到两个问题. 1.,这里的关闭代 ...
- ANN:ML方法与概率图模型
一.ML方法分类: 产生式模型和判别式模型 假定输入x,类别标签y - 产生式模型(生成模型)估计联合概率P(x,y),因可以根据联合概率来生成样本:HMMs ...
- 用VS Code Debug Python
- 用cmd查看win8版本 激活等详细信息命令
Win+x===>选择以管理员身份运行,输入: slmgr /dlv 显示:最为详尽的激活信息,包括:激活ID.安装ID.激活截止日期slmgr /dli 显示:操作系统版本.部分产品密钥. ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(14)--自动生成图标样式文件和图标的选择操作
http://www.cnblogs.com/wuhuacong/p/4093778.html 在很多Web系统中,一般都可能提供一些图标的选择,方便配置按钮,菜单等界面元素的图标,从而是Web系统界 ...
- Entity FrameWork 操作使用详情
Entity FrameWork 是以ADO.net为基础发展的ORM解决方案. 一.安装Entity FrameWork框架 二.添加ADO.Net实体数据模型 三.EF插入数据 using Sys ...
- springMvc学习地址新
http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar ...
- luoguP4719 【模板】动态 DP 线段树+树链剖分+矩阵乘法+动态DP
题目描述 给定一棵n个点的树,点带点权. 有m次操作,每次操作给定x,y,表示修改点x的权值为y. 你需要在每次操作之后求出这棵树的最大权独立集的权值大小. 输入输出格式 输入格式: 第一行,n,m分 ...
- [置顶]
Every Programmer Should Know These Latency Numbers
转自: https://dzone.com/articles/every-programmer-should-know Every Programmer Should Know These Lat ...
- C#第十一节课
类 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Thr ...