Define interceptors and interceptor configuration.
Implement WebMvcConfigurer interface, and rewrite addInterceptors method, custom interceptors added to it. This does not intercept static resources
Here used maven project
<?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>
Configure the port number
application.yml
server:
port: 8333
Start writing class
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);
}
}
Write custom filters
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("清理数据");
}
}
Write the configuration class defines only the filter which the filter is loaded into the 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("/**");
}
}
Write a Restfull style of the 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";
}
}
Startup class execution access port number:
Looking back at the console
apparently performed sequentially