Spring boot的拦截器
程序员文章站
2022-07-09 19:11:22
...
定义拦截器和配置拦截器.
实现 WebMvcConfigurer 接口,然后重写 addInterceptors 方法,将自定义的拦截器添加进去.这样不会拦截静态资源
这里用到的maven工程
<?xml version="1.0" encoding="UTF-8"?>
<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>com.cs</groupId>
<artifactId>springBoot_filter</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置端口号
application.yml
server:
port: 8333
编写启动类
SpringBootFilterApplication .java
package com.cs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootFilterApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootFilterApplication.class,args);
}
}
编写自定义过滤器
package com.cs.demo.filter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component //注入到Spring容器当中
public class MyFilter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("请求处理之前");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("请求处理后");
}
//请求结果结果已经渲染好了,response没有进行返回但也无法修改reponse了(一般用于清理数据)
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("清理数据");
}
}
编写配置类 把只定义过滤器加载到spring过滤器当中
MyConfig .java
package com.cs.demo.config;
import com.cs.demo.filter.MyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration //告知spring这是一个配制类
public class MyConfig implements WebMvcConfigurer {
@Autowired
private MyFilter myFilter;
//重写 addInterceptors方法
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns("/**")指的是过滤那些请求
registry.addInterceptor(myFilter).addPathPatterns("/**");
}
}
写一个 Restfull风格的Controller
HelloController .java
package com.cs.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController
@GetMapping("hello")
public String hello(){
return "hello";
}
}
执行启动类 访问端口号:
回看控制台
显然 依次执行
推荐阅读
-
开机时出现reboot and select proper boot device的解决方法
-
Spring Boot 入门(五):集成 AOP 进行日志管理
-
手把手教你定制标准Spring Boot starter,真的很清晰
-
Spring在代码中获取bean的几种方式详解
-
spring boot 枚举使用的坑整理
-
浅谈Spring Security 对于静态资源的拦截与放行
-
02Spring基于xml的IOC配置--实例化Bean的三种方式
-
Spring Boot Security 入门—内存用户验证
-
Spring 的 Bean 生命周期,11 张高清流程图及代码,深度解析
-
Spring Boot2 系列教程 (二) | 第一个 SpringBoot 工程详解