SpringBoot------拦截器Filter的使用
前言:
最新Servlet 3.0拦截器的使用
1.pom.xml添加需要使用的依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.ytheng</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies> <build>
<!-- 打包的名称 -->
<finalName>myspringboot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.添加Filter拦截器
package top.ytheng.demo.filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //Servlet3.0特性
//urlPatterns:拦截的url地址
//filterName:拦截器名称
@WebFilter(urlPatterns="/api/*", filterName="loginFilter")
public class LoginFilter implements Filter{ /*
* 容器加载完成调用
* */
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("filter init...");
} /*
* 请求被拦截的时候调用
* */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("doFilter..."); HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response; String username = req.getParameter("username");
if(username.equals("theng")) {
chain.doFilter(request, response);
} else {
//重定向
resp.sendRedirect("/filter.html");
return;
} } /*
* 容器被销毁的时候调用
* */
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("filter destroy...");
} }
3.添加测试控制器
package top.ytheng.demo.controller; import java.util.HashMap;
import java.util.Map; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/api/v1/filter")
public class FilterController { @RequestMapping("/test")
public Object testFilter() {
Map<String, Object> map = new HashMap<>();
map.put("name", "theng");
map.put("pwd", "123456");
return map;
}
}
4.添加启动类
package top.ytheng.demo; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication //等于下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//拦截器用到
@ServletComponentScan
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }
5.添加拦截后调整的页面filter.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>hello theng</h4>
<h3>filter success</h3>
</body>
</html>
6.右键项目Run As启动项目,测试地址
http://localhost:8080/api/v1/filter/test?username=theng
http://localhost:8080/api/v1/filter/test?username=ytheng
另附:

SpringBoot------拦截器Filter的使用的更多相关文章
- 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener
=================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...
- SpringBoot拦截器中Bean无法注入(转)
问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...
- Java结合SpringBoot拦截器实现简单的登录认证模块
Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...
- SpringBoot拦截器中无法注入bean的解决方法
SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...
- Springboot拦截器未起作用
之前遇到要使用springboot拦截器却始终未生效的状况,查了网上的博客,大抵都是@Component,@Configuration注解未加,或是使用@ComponentScan增加包扫描,但是尝试 ...
- SpringBoot拦截器中service或者redis注入为空的问题
原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...
- springboot + 拦截器 + 注解 实现自定义权限验证
springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...
- Springboot 拦截器配置(登录拦截)
Springboot 拦截器配置(登录拦截) 注意这里环境为springboot为2.1版本 1.编写拦截器实现类,实现接口 HandlerInterceptor, 重写里面需要的三个比较常用的方 ...
- Springboot拦截器实现IP黑名单
Springboot拦截器实现IP黑名单 一·业务场景和需要实现的功能 以redis作为IP存储地址实现. 业务场景:针对秒杀活动或者常规电商业务场景等,防止恶意脚本不停的刷接口. 实现功能:写一个拦 ...
- SpringBoot 拦截器获取http请求参数
SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 目录 SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 获取http请求参数是一种刚需 定义拦截器获取请求 为 ...
随机推荐
- AJAX传输——以XML文件传输为例
此文档解决以下问题: 一.responseText获取数据 1.AJAX异步传输,get请求方式/post请求方式,输出全部xml数据 二.responseXML获取数据 2.AJAX异步传输,get ...
- Oozie分布式工作流——流控制
最近又开始捅咕上oozie了,所以回头还是翻译一下oozie的文档.文档里面最重要就属这一章了--工作流定义. 一提到工作流,首先想到的应该是工作流都支持哪些工作依赖关系,比如串式的执行,或者一对多, ...
- Go学习入门
1. 为什么要学习Go Go语言宣称为互联网时代的C语言,那她有那些特性值得我们必须学习呢: 并行与分布式支持.除了我们日常熟悉的进程和线程,Go语言中提供了协程coroutine,从而简化了并行开发 ...
- MySQL到底能支持多大的数据量?
MySQL是中小型网站普遍使用的数据库之一,然而,很多人并不清楚MySQL到底能支持多大的数据量,再加上某些国内CMS厂商把数据承载量的责任推给它,导致很多不了解MySQL的站长对它产生了很多误解,那 ...
- apache2.4多站点配置
原来是跑单站,现在想跑多站,配置不算复杂,记录一下: 用默认的httpd.conf修改,去掉两个vhost的注释 servername指定任意一个合法的域名 如果是python,配置wsgi 修改ex ...
- framework中编译anroid工程并在模拟器上运行
1.在eclipse下创建android工程Hello并拷贝到“源码目录/packages/experimental”下面 2.在Hello工程目录下面创建Android.mk文件,内容如下: L ...
- MySQL5.7多主一从(多源复制)同步配置
MySQL5.7多主一从(多源复制)同步配置(抄袭) 原文地址:https://my.oschina.net/u/2399373/blog/2878650 多主一从,也称为多源复制,数据流向: 主库1 ...
- cmd adb批量安装与卸载
批量安装: SET dir=%~dp0echo dir is: %dir%cd /d %dir%for /R %dir% %%i in (*.apk) do adb install %%i 批量卸载: ...
- String拼接字符串效率低,你知道原因吗?
面试官Q1:请问为什么String用"+"拼接字符串效率低下,最好能从JVM角度谈谈吗? 对于这个问题,我们先来看看如下代码: public class StringTest { ...
- 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
原文链接:https://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api ...