(021)Spring Boot之拦截器HandlerInterceptor
Posted 明月之诗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(021)Spring Boot之拦截器HandlerInterceptor相关的知识,希望对你有一定的参考价值。
拦截器的使用步骤:
第一步,实现HandlerInterceptor接口,该接口有三个方法preHandle 、postHandle 、afterCompletion
(1)preHandle在controller执行之前调用
(2)postHandle在controller执行之后,页面渲染之前调用
(3)afterCompletion在页面渲染之后调用,一般用于资源清理操作
第二步,继承WebMvcConfigurationSupport或者实现WebMvcConfigurer,重写他们的addInterceptors方法。把上一步的拦截器加进去。
pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>springboot_web</artifactId> <version>1.0.0</version> <name>springboot_web</name> <!-- FIXME change it to the project\'s website --> <url>http://www.example.com</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
LogHandlerInterceptor.java
package com.edu.spring.springboot; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class LogHandlerInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("====================preHandle================="+handler.getClass().getName()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("==================postHandle==================="+handler.getClass().getName()); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("==================afterCompletion==================="+handler.getClass().getName()); } }
MyWebMvcConfigurationSupport.java
package com.edu.spring.springboot; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class MyWebMvcConfigurationSupport extends WebMvcConfigurationSupport{ @Override protected void addInterceptors(InterceptorRegistry registry) { // TODO Auto-generated method stub registry.addInterceptor(new LogHandlerInterceptor()); } }
或者MyWebMvcConfigurer.java
package com.edu.spring.springboot; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MyWebMvcConfigurer implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { // TODO Auto-generated method stub registry.addInterceptor(new LogHandlerInterceptor()); } }
UserController.java
package com.edu.spring.springboot; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @RequestMapping("/user/home") public String home(){ System.out.println("============home============="); return "user home"; } }
App.java
package com.edu.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
浏览器输入:http://127.0.0.1:8080/user/home ,运行结果如下:
以上是关于(021)Spring Boot之拦截器HandlerInterceptor的主要内容,如果未能解决你的问题,请参考以下文章
#展望我的2022Flag#Springboot学习笔记Spring Boot和web组件
spring boot整合 spring security之授权访问