SpringBoot2.1中添加过滤器配置
1:构造一个实现 Filter 接口的过滤器,并在类上添加@component注释:
notice1:若不添加,则需在spring中注入该bean,不然会报错。
package com.dev.filter; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*; /**
* Created by zgq7 on 2019/6/6.
*/
@Component
public class BaseFilter implements Filter {
private static final Logger log = LoggerFactory.getLogger(BaseFilter.class); @Override
public void init(FilterConfig filterConfig) throws ServletException {
Map<Object, Object> map = new LinkedHashMap<>(10);
map.put("filterName", filterConfig.getFilterName()); log.info("filter config indclude :{}", map);
} @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse; String uri = request.getRequestURI(); Map<Object, Object> map = new HashMap<>(10);
map.put("url", request.getRequestURL());
map.put("uri", uri);
map.put("requestType", request.getMethod());
log.info("{}", map); filterChain.doFilter(request, response);
} @Override
public void destroy() {
log.info("filter destroyed ...");
}
}
2:编写过滤器配置类,并在类上添加@Configuration注释,告诉spring声明这是一个配置类:
package com.dev.config; import com.dev.filter.BaseFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.*;
/**
* Created by zgq7 on 2019/6/6.
*/
@Configuration
public class SpringConfig { @Bean
public FilterRegistrationBean<BaseFilter> filterFilterRegistrationBean(BaseFilter baseFilter) {
FilterRegistrationBean<BaseFilter> filterFilterRegistrationBean = new FilterRegistrationBean<>(); //拦截路径配置
List<String> uriList = new ArrayList<>(10);
uriList.add("/dev/*"); filterFilterRegistrationBean.setFilter(baseFilter);
filterFilterRegistrationBean.setEnabled(true);
filterFilterRegistrationBean.setUrlPatterns(uriList);
filterFilterRegistrationBean.setName("baseFilter");
filterFilterRegistrationBean.setOrder(1); return filterFilterRegistrationBean;
} }
3:编写一个controller进行测试:
package com.dev.controller; import com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.*; import java.util.Map; /**
* Created by zgq7 on 2019/6/6.
*/
@RestController
@RequestMapping(value = "/dev")
public class BaseController { @GetMapping(value = "")
public Map<Object, Object> get() {
return ImmutableMap.of("code", "get");
} @PutMapping(value = "t")
public Map<Object, Object> t() {
return ImmutableMap.of("code", "put");
} @PostMapping(value = "s")
public Map<Object, Object> s() {
return ImmutableMap.of("code", "post");
} }
4:使用psotMan进行测试:

5:控制台如下:

输出该日志是因为过滤器拦截到了 /dev 路径下的请求才输出的。可自行测试哦~~~
SpringBoot2.1中添加过滤器配置的更多相关文章
- 在web.Config文件中添加数据库连接配置
新建一个网站,打开web.config文件,在connectionString配置节点添加add节点进行数据库进行数据库连接配置代码如下: <connectionStrings> < ...
- vue中添加less配置,用于计算div高度
需求:左边垂直的菜单栏高度设置为 100% - 导航栏的高度(3.6rem) 首先,从vue-cli脚手架里的安装的webpack模板中并没有less的依赖配置,得自己手动添加安装 安装命令::npm ...
- AngularJS 表达式中添加过滤器实例
过滤器可以通过一个管道字符(|)和一个过滤器添加到表达式中 历练实例: <!DOCTYPE html><html><head><meta http-equiv ...
- jsp中添加过滤器,实现校验用户身份
我现在需要实现一个功能,就是用户登录前不允许访问系统,我使用的是jsp的过滤器来实现的. 先把filter过滤器的代码粘出来: package com.day8.filter; import java ...
- 解决springmvc 中文post请求乱码的过滤器配置
在web.xml中添加如下配置 <!-- 过滤器 解决post乱码 --> <filter> <filter-name>characterEncodingFilte ...
- asp.net中web.config配置节点大全详解
最近网上找了一些关于Web.config配置节点的文章,发现很多都写的都比较零散,而且很少有说明各个配置节点的作用和用法.搜索了一下发现有一篇写的不错,这里引用一下 原文地址 http://www.c ...
- asp.net中web.config配置节点大全详解【转】
web.config 文件查找规则: (1)如果在当前页面所在目录下存在web.config文件,查看是否存在所要查找的结点名称,如果存在返回结果并停止查找. (2)如果当前页面所在目录下不存在web ...
- (转)如何在maven的pom.xml中添加本地jar包
1 maven本地仓库认识 maven本地仓库中的jar目录一般分为三层:图中的1 2 3分别如下所示: 1 groupId 2 artifactId 3 version 4 jar包的依赖 如果要将 ...
- (转)如何在maven的pom.xml中添加本地jar包
转载自: https://www.cnblogs.com/lixuwu/p/5855031.html 1 maven本地仓库认识 maven本地仓库中的jar目录一般分为三层:图中的1 2 3分别如下 ...
随机推荐
- docker学习一
由于本人学习精力有限,不能像大学时那样,每个笔记都认真的手敲记录,一些地方会截图展示,并会添加自己的理解和备注,建议大家也做自己的笔记,看别人的可以,但是自己动手并思考记忆更深刻. 1.什么是虚拟化 ...
- Codeforces Round #651 (Div. 2) A. Maximum GCD(数论)
题目链接:https://codeforces.com/contest/1370/problem/A 题意 有 $n$ 个数大小分别为 $1$ 到 $n$,找出两个数间最大的 $gcd$ . 题解 若 ...
- Codeforces Round #651 (Div. 2) C. Number Game(数论)
题目链接:https://codeforces.com/contest/1370/problem/C 题意 给出一个正整数 $n$,Ashishgup 和 FastestFinger 依次选择执行以下 ...
- hdu1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- MIT 6.S081 聊聊xv6的文件系统(中)日志层与事务
前言 我本想把上篇中没讲完的剩余层全部在本篇中讲完,但没想到越写越多.日志层的代码不多,其思想和解决问题的手段也不算难以理解,但其背后涉及的原理和思想还是非常值得回味的,因此我打算用一整篇完整的blo ...
- win7 & centos7 双系统安装方法
1.准备 1)Centos7镜像 官方:https://www.centos.org/ 阿里镜像:http://mirrors.aliyun.com/centos/ 2)安装windows7系统的电脑 ...
- 【原创】Linux虚拟化KVM-Qemu分析(九)之virtio设备
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...
- 智能社讲解js基础
一,Javascript的组成ECMAScript:翻译,核心,解释器,DOM:文档对象模型,Document Object Model 操作HTML的能力,document对象BOM:浏览器对象模型 ...
- Leetcode(213)-打家劫舍II
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在 ...
- 操作系统 part4
1.操作系统的启动 CPU加电后,执行BIOS(基本IO处理系统).BIOS会进行硬件的自检和初始化,然后把加载程序(BootLoader)从磁盘上的引导扇区中加载到指定位置0x7c00.然后控制权交 ...