当前位置:   article > 正文

SpringBoot3-实现和注册拦截器_springboot3拦截器

springboot3拦截器

1、pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.atguigu</groupId>
  7. <artifactId>pro28-springboot-interceptor</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!--所有springboot项目都必须继承自 spring-boot-starter-parent -->
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>3.0.5</version>
  14. <relativePath /> <!-- 根据情况添加 -->
  15. </parent>
  16. <properties>
  17. <maven.compiler.source>17</maven.compiler.source>
  18. <maven.compiler.target>17</maven.compiler.target>
  19. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20. </properties>
  21. <dependencies>
  22. <!--web开发的场景启动器 -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <!--导入配置处理器,配置文件自定义的properties配置都会有提示-->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-configuration-processor</artifactId>
  31. <optional>true</optional>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.projectlombok</groupId>
  35. <artifactId>lombok</artifactId>
  36. </dependency>
  37. </dependencies>
  38. </project>

2、实现HandlerInterceptor接口

  1. package com.atguigu.interceptor;
  2. import jakarta.servlet.http.HttpServletRequest;
  3. import jakarta.servlet.http.HttpServletResponse;
  4. import org.springframework.stereotype.Component;
  5. import org.springframework.web.servlet.HandlerInterceptor;
  6. import org.springframework.web.servlet.ModelAndView;
  7. @Component
  8. public class MyInterceptor implements HandlerInterceptor {
  9. @Override
  10. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  11. System.out.println("preHandle...");
  12. return true;
  13. }
  14. @Override
  15. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  16. System.out.println("postHandle...");
  17. }
  18. @Override
  19. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  20. System.out.println("afterCompletion...");
  21. }
  22. }

3、实现WebMvcConfigurer接口

  1. package com.atguigu.config;
  2. import com.atguigu.interceptor.MyInterceptor;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  7. @Configuration
  8. public class MyMvcConfig implements WebMvcConfigurer {
  9. @Autowired
  10. private MyInterceptor myInterceptor;
  11. @Override
  12. public void addInterceptors(InterceptorRegistry registry) {
  13. registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/hello/a/b");
  14. }
  15. }

4、HelloController.java

  1. package com.atguigu.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. @RequestMapping("/hello")
  7. public class HelloController {
  8. @GetMapping("/a/b")
  9. public String h01() {
  10. return "Hello world!";
  11. }
  12. @GetMapping("/b")
  13. public String h02() {
  14. return "hi world!";
  15. }
  16. @GetMapping("/c")
  17. public String h03() {
  18. return "bye bye!";
  19. }
  20. }

5、MyApplication.java

  1. package com.atguigu;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class MyApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(MyApplication.class);
  8. }
  9. }

 6、HandlerInterceptor

HandlerInterceptor是Spring MVC框架中的一种拦截器,类似于Servlet开发中的过滤器Filter。它用于在请求处理器执行前后进行预处理和后处理,可以用于实现权限检查、性能监控等操作。

在Spring MVC中,拦截器是一个接口,需要实现preHandle、postHandle和afterCompletion方法。其中,preHandle方法在请求处理器处理请求之前执行,可以进行编码、安全控制、权限校验等处理;postHandle方法在请求处理器处理请求之后、渲染视图之前执行,可以通过modelAndView对模型数据进行处理或对视图进行处理;afterCompletion方法在请求处理完成后执行,可以进行资源清理等工作。

在实现拦截器时,需要将拦截器注册到Spring MVC的配置文件中,并指定需要拦截的URL和处理器。当请求匹配到指定的URL时,拦截器就会自动执行。

总之,HandlerInterceptor是Spring MVC中非常有用的工具,可以用于实现各种处理操作,提高Web应用程序的性能和安全性。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/690027
推荐阅读
  

闽ICP备14008679号