首先在Application文件中添加注解@ServletComponentScan自动扫描当前类的同包以及子包,这样才能将filter装入bean
package com.example.academy;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; @ServletComponentScan
@SpringBootApplication
@MapperScan("com.example.academy.dao")
public class AcademyApplication { public static void main(String[] args) {
SpringApplication.run(AcademyApplication.class, args);
} }
然后配置自己的filter
package com.example.academy.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException; @WebFilter(urlPatterns = "/*")
public class LoginFilter implements Filter {
private final String[] notFilterUrl = {"/index", "/login", "js", "css", "images", "fonts"};//不拦截列表 @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//判断是否为HttpServletRequest/HttpServletResponse请求
if (!(servletRequest instanceof HttpServletRequest) || !(servletResponse instanceof HttpServletResponse)) {
throw new ServletException("拦截器仅支持HTTP请求");
}
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String uri = request.getRequestURI();
boolean notFilter = false;
//遍历不拦截列表并查找是否存在指定值
for (String str : notFilterUrl) {
if (uri.contains(str)) {
notFilter = true;
break;
}
}
System.out.println("过滤器工作");
HttpSession session = request.getSession();
Object user = session.getAttribute("id");
if (user == null && !notFilter) {
response.sendRedirect("index");
return;
}
filterChain.doFilter(request, response); }
}

springboot 配置和使用过滤器的更多相关文章

  1. springboot配置监听器、过滤器和拦截器

    监听器:listener是servlet规范中定义的一种特殊类.用于监听servletContext.HttpSession和servletRequest等域对象的创建和销毁事件.监听域对象的属性发生 ...

  2. SQL盲注、SQL注入 - SpringBoot配置SQL注入过滤器

    1. SQL盲注.SQL注入   风险:可能会查看.修改或删除数据库条目和表.   原因:未对用户输入正确执行危险字符清理.   固定值:查看危险字符注入的可能解决方案. 2. pom.xml添加依赖 ...

  3. 跨站点脚本编制 - SpringBoot配置XSS过滤器(基于mica-xss)

    1. 简介   XSS,即跨站脚本编制,英文为Cross Site Scripting.为了和CSS区分,命名为XSS.   XSS是最普遍的Web应用安全漏洞.这类漏洞能够使得攻击者嵌入恶意脚本代码 ...

  4. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

  5. SpringBoot配置(1) 配置文件application&yml

    SpringBoot配置(1) 配置文件application&yml 一.配置文件 1.1 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application ...

  6. 最新学习springboot 配置注解

    一.概述      Spring Boot设计目的是用来简化新Spring应用的初始搭建以及开发过程.Spring Boot并不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式. ...

  7. SpringBoot配置属性之Server

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

  8. SpringBoot基础系列-SpringBoot配置

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9990680.html SpringBoot基础系列-SpringBoot配置 概述 属性 ...

  9. springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器

    1.    Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...

随机推荐

  1. leetcode刷题记录——树

    递归 104.二叉树的最大深度 /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...

  2. 使用vim的妙招

    使用F1执行文件 Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器. 我们Linux运维经常在Linux中使用到Vim编辑器,当使用Vim写shell脚本或者python脚本的时候,想要 ...

  3. 安国AU6989主控 + K9GBG08U0A(NAND) 制作4GB闪存驱动器

    文档标识符:AU6989_FLASH-DRIVE_D-P8 作者:DLHC 最后修改日期:2020.8.22 本文链接: https://www.cnblogs.com/DLHC-TECH/p/AU6 ...

  4. 结对项目 实现自动生成四则运算题目的程序 (C++)

    本次作业由 陈余 与 郭奕材 结对完成 零.github地址: https://github.com/King-Authur/-Automatically-generate-four-arithmet ...

  5. Logistic回归分析之二元Logistic回归

    在研究X对于Y的影响时,如果Y为定量数据,那么使用多元线性回归分析(SPSSAU通用方法里面的线性回归):如果Y为定类数据,那么使用Logistic回归分析. 结合实际情况,可以将Logistic回归 ...

  6. update 字符串拼接

    UPDATE store SET food_ordering =1,self_pickup_remark = CONCAT('self pick up notes:',store_code,short ...

  7. Qt 如何使窗体初始最大化

    Qt 如何使窗体初始最大化 使用以下函数即可解决: void QWidget::setWindowState ( Qt::WindowStateswindowState ) 这样的函数,通过它可以设置 ...

  8. 精华技巧,学会这几招可以假装是Python高手

    Python里面的技巧真的是太多了,菜鸟如何假装自己的是高手呢,下面教大家几招快速变成老司机的招数,尤其是在实战的项目中,用上这几招一定让你周围的同事刮目相看,哇这个代码有两把刷子. 很多人学习pyt ...

  9. Python错误,pip安装包或更新时因超时而报错误

    报错原因 想要使用pip,结果出下以下警告: 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高 ...

  10. RabbitMQ高级之消息限流与延时队列

    人生终将是场单人旅途,孤独之前是迷茫,孤独过后是成长. 楔子 本篇是消息队列RabbitMQ的第五弹. 上篇本来打算讲述RabbitMQ的一些高级用法: 如何保证消息的可靠性? 消息队列如何进行限流? ...