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

这里假设的场景是,定义一个过滤器,过滤所有请求,如果参数中没有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. Python map filter reduce enumerate zip 的用法

    map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...

  2. Shell语法 【if while for】

    [if语法 测试条件 判断语句] 转自:http://lovelace.blog.51cto.com/1028430/1211353 [while for循环] 转自:https://blog.csd ...

  3. Vue专题-组件

    vue.js既然是框架,那就不能只是简单的完成数据模板引擎的任务,它还提供了页面布局的功能.本文详细介绍使用vue.js进行页面布局的强大工具,vue.js组件系统. Vue.js组件系统 每一个新技 ...

  4. let和var的区别

    在JavaScript中,定义变量的关键词一般用var,但还有一种定义变量的关键词叫let.两者的作用域范围不一样,我们可以将var理解为定义的是一个全局变量,而let定义的是一个局部变量.故let常 ...

  5. Python Numpy中数据的常用的保存与读取方法

    在经常性读取大量的数值文件时(比如深度学习训练数据),可以考虑现将数据存储为Numpy格式,然后直接使用Numpy去读取,速度相比为转化前快很多. 下面就常用的保存数据到二进制文件和保存数据到文本文件 ...

  6. mybatis使用Map<String,Object>映射mysql结果集,关于字段的问题

    --mysql常用字段类型如图 --mybatis使用Map<String,Object>映射,会将tinyint映射成Integer类型.decimal映射成BigDecimal类型 所 ...

  7. day53-线程池

    #1.from concurrent import futures可以开启进程池和线程池.concurrent是包,futures是模块,ThreadPoolExecutor是类,submit是方法. ...

  8. 【转】Linux服务器命令行模式安装Matlab2014a

    转自http://www.aichengxu.com/diannao/39100.htm 0.下载安装包  下载Matlab2014a for Linux安装包的ISO镜像文件 将下载好的iso文件挂 ...

  9. [数学][欧拉降幂定理]Exponial

    Exponial 题目 http://exam.upc.edu.cn/problem.php?cid=1512&pid=4 欧拉降幂定理:当b>phi(p)时,有a^b%p = a^(b ...

  10. TreeviewEditor软件的安装和使用

    TreeviewEditor是用VB6开发的一款Windows桌面程序,用户可以快速搭建树形结构,可以导出为Word文档. 支持节点的复制粘贴.节点的拖放. 下载地址:TreeviewEditor.r ...