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

这里假设的场景是,定义一个过滤器,过滤所有请求,如果参数中没有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. centos 下使用 pytesseract 识别文字

    偶发一个想法搭一个验证码识别工具,网上查了一下有Tesseract 这个工具可以识别,所以有了后面一小时的搭建过程 ps:Ubuntu 下似乎可以直接用包管理工具来安装,我使用的源码编译安装 前提 由 ...

  2. 自由职业平台Upwork申请上市,能给国内猪八戒网带来什么启示?

    当下,在互联网的全面渗入下已经对整个社会的运行机制.大众的生活产生了前所未有的影响.尤其是在工作方面,更是衍生出诸多新兴职业.如,主播.水军.新媒体运营.自媒体人等.值得注意的是,这些职业绝大部分都有 ...

  3. springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+freemarker+layui

    前言: 开发环境:IDEA+jdk1.8+windows10 目标:使用springboot整合druid数据源+mysql+mybatis+通用mapper插件+pagehelper插件+mybat ...

  4. sql server2008 装上后,总是出现machine.config line136,或者 出现 配置错误 无法识别的配置节 system.serviceModel 。

    怀疑问题是vs 和 sql server2008安装冲突的问题造成, 有一个这样的说法: 用win8.1的64位 的系统,如果先装vs2010,再装sql server 2008 r2,根本就不行,一 ...

  5. Long型转ZonedDateTime型

    /** * 将Long类型转化成0 * @author yk * @param time * @return */public static ZonedDateTime toZonedDateTime ...

  6. Mutation|DNM|

    生命组学 DNA序列改变的分子基础 变异来源 据研究对象,可分为两类mutation:个体上的变异和群体上的变异,群体上的变异是关联研究,eg喝酒人群vs非喝酒人群相比. 造成mutation的三类机 ...

  7. 用hash存数组|得地址|取地址

    #!/usr/bin/perl -w use strict; my %hash = %{&collect};my $arr_ad=$hash{'a'};print "$arr_ad\ ...

  8. 曾经倍受年轻人追棒的Facebook为何如今却被称为“老年人社交网站”?

    一直以来,Facebook都被视为最受年轻人欢迎的社交媒体.毕竟此前在社交领域,能跟Facebook这一庞然巨物掰手腕的网站.应用几乎还没出现.但很显然,随着Instagram和Snapchat等新型 ...

  9. poj-3665 iCow(暴力吧)

    http://poj.org/problem?id=3665 题目描述 Fatigued by the endless toils of farming, Farmer John has decide ...

  10. 为什么java的接口的方法是public abstract修饰?为什么属性是public static final 修饰?

     为什么java的接口的方法是public abstract修饰? 1.首先要明白接口的定义和作用是什么: 接口定义:接口是一个全部由抽象方法组成的集合,里面都是抽象方法和常量,用interface修 ...