过滤器有两种配置方式,一种是通过注解来完成,一种是通过自定义配置类来设置

这里假设的场景是,定义一个过滤器,过滤所有请求,如果参数中没有username信息则重定向到login_page登录页面,如果带有username则放行

注解-WebFilter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.demo.myFilter;

import org.springframework.stereotype.Component;

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; * @author fengxi
* @date 2019-03-06 10:47
*/ (value = "/*") // WebFilter注解告诉spring这是一个过滤器
@Component // Component注解表示这是一个组件,需要创建出一个实例来
public class implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init,在服务启动的时候被调用");
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getRequestURI();
HttpSession session = req.getSession();
if (req.getParameter("username") != null){
// 放行
chain.doFilter(request, response);
}else {
System.out.println(path);
if (path.equals("/login_page")) {
// 如果本身访问的是登录页面,直接放行
chain.doFilter(request, response);
} else {
// 没有username,访问非登录页面,重定向到登录页面
HttpServletResponse resp = (HttpServletResponse) response;
resp.sendRedirect("login_page");
}
} } @Override
public void destroy() {
System.out.println("destroy,在停止服务程序的时候会调用");
}
}

代码配置

1。 在fitler实现类不使用注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
大专栏  spring-boot-学习笔记(三)-过滤器ss="line">25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.example.demo.myFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException; * @author fengxi
* @date 2019-03-06 15:31
*/
public class secondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init");
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getRequestURI();
HttpSession session = req.getSession();
if (req.getParameter("username") != null){
chain.doFilter(request, response);
}else {
System.out.println(path);
if (path.equals("/login_page")) {
chain.doFilter(request, response);
} else {
HttpServletResponse resp = (HttpServletResponse) response;
resp.sendRedirect("login_page");
}
}
} @Override
public void destroy() {
System.out.println("destroy");
}
}

2。 使用configuration注解定义过滤器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.example.demo.config;

import com.example.demo.myFilter.secondFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; * @author fengxi
* @date 2019-03-06 15:30
*/ @Configuration
public class myConfig { @Bean("bean1")
public FilterRegistrationBean registerFilter1(){
// 创建过滤器注册bean对象
FilterRegistrationBean bean1 = new FilterRegistrationBean();
// 设置要配置的过滤器对象
bean1.setFilter(new secondFilter());
// 添加该过滤器要过滤的url规则
bean1.addUrlPatterns("/*");
// 设置过滤器执行的优先级,数字小的先执行
bean1.setOrder(1);
return bean1;
}
}

spring-boot-学习笔记(三)-过滤器的更多相关文章

  1. Spring Boot学习笔记(三)实现热部署

    pom文件中添加如下依赖即可 <dependency> <groupId>org.springframework.boot</groupId> <artifa ...

  2. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  3. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

  4. Java框架spring Boot学习笔记(三):Controller的使用

    Controller注解介绍 @Controller:处理http请求 @RestController: Spirng4之后新加的注解,其实是一个组合注解等同于@ResponseBody和@Contr ...

  5. spring boot 学习笔记(三)之 配置

    一:概述 在Spring boot 中根据业务需求,我们往往会在不同地方配置我们所需的key-value 配置项,配置文件存在不同的地方的场景如下: (1) 默认存在 application.prop ...

  6. Spring Boot学习笔记二

    Spring Boot入门第二篇 第一天的详见:https://www.cnblogs.com/LBJLAKERS/p/12001253.html 同样是新建一个pring Initializer快速 ...

  7. spring boot 学习笔记(一)

    学习链接:http://www.cnblogs.com/ityouknow/category/914493.html 定义 spring boot 是由pivotal 团队提供的权限框架,设计目的是用 ...

  8. Spring Boot学习笔记:整合Shiro

    Spring Boot如何和Shiro进行整合: 先自定义一个Realm继承AuthorizingRealm,并实现其中的两个方法,分别对应认证doGetAuthenticationInfo和授权do ...

  9. Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用[z]

    前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...

  10. Spring Boot 学习笔记1---初体验之3分钟启动你的Web应用

    前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...

随机推荐

  1. 题解 P1884 【[USACO12FEB]过度种植(银)Overplanting 】

    什么,扫描线需要线段树? 那我第一个不干啊(其实是不会写) 这里介绍一种裸的扫描线: 我们根据x排序,对于相等的 \(x\) ,将 \(y\) 进入和退出分类讨论,然后全部放进set里面.每次 \(x ...

  2. js变量的相关要点

    如果变量在函数内没有声明(没有使用 var 关键字),该变量为全局变量. JavaScript 变量生命周期在它声明时初始化. 局部变量在函数执行完毕后销毁. 全局变量在页面关闭后销毁.

  3. Window命令行切换命令

    Windows 命令行切换目录 特别注意:切换到其它盘符不需要 cd 命令 1. 切换到 C 盘根目录 打开终端 cmd 后,输入cd C:\(一定要加上后面的反斜扛) 2.切换到 C 盘子目录 打开 ...

  4. std::string和ctime之间的转换

    int year, month, day, hour, minute, second; string strTime: sscanf(strTime.c_str(), "%d-%d-%d % ...

  5. mysql 查询a表在b表中不存在的记录

    select * from tbl_user a where(select count(1) as cnt from tbl_order b where a.phone=b.phone)=0

  6. 没有更好的,五种操作系统助力研发,IMX6开发板做得到

    核心板参数 尺寸 51mm*61mm 四核商业级-2G NXP 四核 i.MX6Q,主频 1 GHz 内存:2GB DDR3:存储:16GB EMMC:SATA接口:支持 双核商业级-1G NXP 双 ...

  7. 文件流下载时 axios blob文件大小不正确?

    文件流下载时 js blob文件大小不正确? res.data的字节长度 length blob.size匹配不上.. axio请求里必须修改 responseType: 'blob' 参数, 默认是 ...

  8. 吴裕雄--天生自然 JAVA开发学习:Applet 基础

    import java.applet.*; import java.awt.*; public class HelloWorldApplet extends Applet { public void ...

  9. 关于Java杂项知识总结

    JVM内存结构 JVM在运行时把从操作系统申请到的内存分为若干区域,主要有栈.堆和方法区,方便Java程序使用 堆内存 使用new关键字创建出来的对象都存储在堆内存中 方法区 被加载的类的信息存储在方 ...

  10. surprise库使用

    自动交叉使用法 #-*- coding:utf-8 -*- from surprise import SVD from surprise import Dataset from surprise.mo ...