1、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.atguigu</groupId><artifactId>pro28-springboot-interceptor</artifactId><version>1.0-SNAPSHOT</version><!--所有springboot项目都必须继承自 spring-boot-starter-parent --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.5</version><relativePath /> <!-- 根据情况添加 --></parent><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!--web开发的场景启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--导入配置处理器,配置文件自定义的properties配置都会有提示--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies> </project>
2、实现HandlerInterceptor接口
package com.atguigu.interceptor; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; @Component public class MyInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle...");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle...");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion...");} }
3、实现WebMvcConfigurer接口
package com.atguigu.config; import com.atguigu.interceptor.MyInterceptor; import org.springframework.beans.factory.annotation.Autowired; 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 MyMvcConfig implements WebMvcConfigurer {@Autowiredprivate MyInterceptor myInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/hello/a/b");} }
4、HelloController.java
package com.atguigu.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController {@GetMapping("/a/b")public String h01() {return "Hello world!";}@GetMapping("/b")public String h02() {return "hi world!";}@GetMapping("/c")public String h03() {return "bye bye!";} }
5、MyApplication.java
package com.atguigu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class);} }
6、HandlerInterceptor
HandlerInterceptor是Spring MVC框架中的一种拦截器,类似于Servlet开发中的过滤器Filter。它用于在请求处理器执行前后进行预处理和后处理,可以用于实现权限检查、性能监控等操作。
在Spring MVC中,拦截器是一个接口,需要实现preHandle、postHandle和afterCompletion方法。其中,preHandle方法在请求处理器处理请求之前执行,可以进行编码、安全控制、权限校验等处理;postHandle方法在请求处理器处理请求之后、渲染视图之前执行,可以通过modelAndView对模型数据进行处理或对视图进行处理;afterCompletion方法在请求处理完成后执行,可以进行资源清理等工作。
在实现拦截器时,需要将拦截器注册到Spring MVC的配置文件中,并指定需要拦截的URL和处理器。当请求匹配到指定的URL时,拦截器就会自动执行。
总之,HandlerInterceptor是Spring MVC中非常有用的工具,可以用于实现各种处理操作,提高Web应用程序的性能和安全性。