• 10.springboot拦截器实例


    项目结构

    1.pom.xml文件,自动生成,不进行修改

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.3version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.itgroupId>
    12. <artifactId>012-springboot-interceptorartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>012-springboot-interceptorname>
    15. <description>Demo project for Spring Bootdescription>
    16. <properties>
    17. <java.version>1.8java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starter-webartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>org.springframework.bootgroupId>
    26. <artifactId>spring-boot-starter-testartifactId>
    27. <scope>testscope>
    28. dependency>
    29. dependencies>
    30. <build>
    31. <plugins>
    32. <plugin>
    33. <groupId>org.springframework.bootgroupId>
    34. <artifactId>spring-boot-maven-pluginartifactId>
    35. plugin>
    36. plugins>
    37. build>
    38. project>

    2.创建拦截器实现类LoginInterceptor(项目实战中自动返回true或者false,在本例中手动添加为返回true,也就是拦截成功的地址会返回true)

    1. package com.it.web;
    2. import org.springframework.web.servlet.HandlerInterceptor;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. /**
    6. * 自定义拦截器
    7. */
    8. public class LoginInterceptor implements HandlerInterceptor {
    9. /**
    10. *
    11. * @param request
    12. * @param response
    13. * @param handler 被拦截的控制器对象
    14. * @return boolean
    15. * true:请求可以被Controller处理
    16. * false:请求被截断
    17. * @throws Exception
    18. */
    19. @Override
    20. public boolean preHandle(HttpServletRequest request,
    21. HttpServletResponse response,
    22. Object handler) throws Exception {
    23. System.out.println("执行了LoginInterceptor的preHandler");
    24. return true;
    25. }
    26. }

    3.注册拦截器对象,相当于springMVC的配置文件

    详细代码

    1. package com.it.config;
    2. import com.it.web.LoginInterceptor;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.web.servlet.HandlerInterceptor;
    5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    7. import java.util.logging.Handler;
    8. @Configuration
    9. public class MyAppConfig implements WebMvcConfigurer {
    10. //添加拦截器对象,注入到容器中
    11. @Override
    12. public void addInterceptors(InterceptorRegistry registry) {
    13. //创建拦截器对象
    14. HandlerInterceptor interceptor=new LoginInterceptor();
    15. //指定拦截的请求uri地址
    16. String path []={"/user/**"};
    17. //指定不拦截的地址
    18. String excludePath []={"/user/login"};
    19. registry.addInterceptor(interceptor)
    20. .addPathPatterns(path)
    21. .excludePathPatterns(excludePath);
    22. }
    23. }

    4.创建控制类BootController类

    1. package com.it.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.ResponseBody;
    5. @Controller
    6. public class BootController {
    7. @RequestMapping("/user/account")
    8. @ResponseBody
    9. public String userAccount(){
    10. return "访问user/account地址";
    11. }
    12. @RequestMapping("/user/login")
    13. @ResponseBody
    14. public String userLogin(){
    15. return "访问user/login地址";
    16. }
    17. }

    5.运行Application文件,启动Tomcat进行测试

     

     清空控制台重新访问/user/login地址,如果没有拦截,控制台就不会打印上面的那句话

     

  • 相关阅读:
    神经辐射场(Neural Radiance Field,NeRF)的简单介绍
    xindong科技面试回顾
    IPWorks Encrypt Delphi强加密的一整套组件
    GFS分布式文件系统
    谷歌与OpenAI合作,用Google Search为大模型刷新!
    全新升级!《云原生架构白皮书 2022 版》重磅发布
    【Java 进阶篇】CSS语法格式详解
    1381:城市路(Dijkstra)
    比较三种非破坏性处理数组的方法
    C#,寻找图强连通分量(SCC,Strongly Connected Components)的Kosaraju算法与源代码
  • 原文地址:https://blog.csdn.net/weixin_59334478/article/details/126685683