通过注解的方式实现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. JAVA处理链表经典问题

    定义链表节点Node class Node { private int Data;// 数据域 private Node Next;// 指针域 public Node(int Data) { // ...

  2. SPSS学习笔记之——Kaplan-Meier生存分析

    SPSS学习笔记之--Kaplan-Meier生存分析 一.概述 关于生存分析的相关概念,Kaplan-Meier用于估计生存函数,允许有一个分组变量进行生存率的组间比较,还容许一个分层变量.若不考虑 ...

  3. golang(10)interface应用和复习

    原文链接 http://www.limerence2017.com/2019/10/11/golang15/ interface 意义? golang 为什么要创造interface这种机制呢?我个人 ...

  4. This application's application-identifier entitlement does not match that of the installed application. These values must match for an upgrade to be allowed.

    真机运行测试的时候Xcode会报这样的错误: 原因: 你的手机上已经安装了此项目. 解决办法: 把你以前安装的卸掉, 或者把这个项目的 bunldID 改了,再次运行即可.

  5. GitLab 架构

    GitLab 架构官方文档 GitLab 中文文档 版本 一般使用的是社区版(Community Edition,CE),此外还有企业版(Enterprise Edition,EE)可以使用. EE ...

  6. python基础(代码规范、命名规范、代码缩进、注释)

    代码规范 PEP8(python增强建议书第8版) 每个import语句只导入一个模块 不要在行尾添加分号";" 建议每行不超过80个字符   超出部分可以用()来进行换行例如: ...

  7. 【AMAD】salabim -- Python中进行离散事件模拟

    简介 用法 个人评分 简介 salabim1是用来定义离散事件模拟(DES2),以及转换为动画的一个python库. 用法 请看官方文档3. 个人评分 实用性是基于对平均群众的,大多数人还是接触不到这 ...

  8. 【CUDA开发】__syncthreads的理解

    __syncthreads()是cuda的内建函数,用于块内线程通信. __syncthreads() is you garden variety thread barrier. Any thread ...

  9. MessageBox显示位置

    假设存在2个窗口类CImDlg与CChatDlg,如果希望MessageBox跟随CChatDlg,方法是 CChatDlg *pDlg = xxx; pDlg->MessageBox();

  10. 【Spring 源码】ApplicationContext源码

    ApplicationConetxt体系