赞
踩
Spring Boot拦截器的使用
使用注解@Configuration
继承WebMvcConfigurerAdapter
重写addInterceptors添加需要拦截器地址:
运行截图如下:
后端拦截如下:
这里主要是依靠注册拦截器实现的
这里一个拦截器可以进行多个链接过滤,只需在后面加入addPathPatterns即可
程序结构如下:
WebMvcConfig.java
- package com.example.demo.config;
-
- import com.example.demo.interceptor.OneInterceptor;
- import com.example.demo.interceptor.TwoInterceptor;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
-
- @Configuration
- public class WebMvcConfig extends WebMvcConfigurerAdapter {
-
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
-
- registry.addInterceptor(new OneInterceptor()).addPathPatterns("/one/**");
- registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/two/**");
- registry.addInterceptor(new OneInterceptor()).addPathPatterns("/three/**");
- registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/three/**");
-
- super.addInterceptors(registry);
- }
- }
OneController.java
- package com.example.demo.interceptor;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- @RequestMapping("one")
- public class OneController {
-
- @RequestMapping("/index")
- public String index(){
-
- return "index";
- }
-
- @RequestMapping("/index2")
- public String index2(){
-
- return "index2";
- }
-
- @RequestMapping("/index3")
- public String index3(){
-
- return "index3";
- }
- }
OneInterceptor.java
- package com.example.demo.interceptor;
-
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class OneInterceptor implements HandlerInterceptor {
-
- /**
- * 在请求处理之前进行调用(Controller方法调用之前)
- */
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
- Object object) throws Exception {
-
- System.out.println("OneInterceptor preHandle");
-
- return true;
- }
-
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response,
- Object object, ModelAndView mv)
- throws Exception {
-
- System.out.println("OneInterceptor postHandle");
- }
-
- /**
- * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行
- * (主要是用于进行资源清理工作)
- */
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
- Object object, Exception ex)
- throws Exception {
-
- System.out.println("OneInterceptor afterCompletion");
- }
- }
ThreeController.java
- package com.example.demo.interceptor;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- @RequestMapping("three")
- public class ThreeController {
-
- @RequestMapping("/index")
- public String index(){
-
- return "index";
- }
-
- @RequestMapping("/index2")
- public String index2(){
-
- return "index2";
- }
-
- @RequestMapping("/index3")
- public String index3(){
-
- return "index3";
- }
- }
TwoController.java
- package com.example.demo.interceptor;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- @Controller
- @RequestMapping("two")
- public class TwoController{
-
- @RequestMapping("/index")
- public String index(){
-
- return "index";
- }
-
- @RequestMapping("/index2")
- public String index2(){
-
- return "index2";
- }
-
- @RequestMapping("/index3")
- public String index3(){
-
- return "index3";
- }
- }
TwoInterceptor.java
- package com.example.demo.interceptor;
-
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class TwoInterceptor implements HandlerInterceptor {
-
- /**
- * 在请求处理之前进行调用(Controller方法调用之前)
- */
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
- Object object) throws Exception {
-
- System.out.println("TwoController preHandle");
-
- return false;
- }
-
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response,
- Object object, ModelAndView mv)
- throws Exception {
-
- System.out.println("TwoController postHandle");
- }
-
- /**
- * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行
- * (主要是用于进行资源清理工作)
- */
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
- Object object, Exception ex)
- throws Exception {
-
- System.out.println("TwoController afterCompletion");
- }
- }
index相关.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>INDEX</title>
- </head>
- <body>
- <h1>INDEX PAGE</h1>
- </body>
- </html>
porn.xml
- <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.12.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>demo</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </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>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。