项目结构
1.pom.xml文件,自动生成,不进行修改
- "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.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.3version>
- <relativePath/>
- parent>
- <groupId>com.itgroupId>
- <artifactId>012-springboot-interceptorartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>012-springboot-interceptorname>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
2.创建拦截器实现类LoginInterceptor(项目实战中自动返回true或者false,在本例中手动添加为返回true,也就是拦截成功的地址会返回true)
- package com.it.web;
-
- import org.springframework.web.servlet.HandlerInterceptor;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- /**
- * 自定义拦截器
- */
-
- public class LoginInterceptor implements HandlerInterceptor {
-
- /**
- *
- * @param request
- * @param response
- * @param handler 被拦截的控制器对象
- * @return boolean
- * true:请求可以被Controller处理
- * false:请求被截断
- * @throws Exception
- */
-
- @Override
- public boolean preHandle(HttpServletRequest request,
- HttpServletResponse response,
- Object handler) throws Exception {
- System.out.println("执行了LoginInterceptor的preHandler");
- return true;
- }
- }
3.注册拦截器对象,相当于springMVC的配置文件
详细代码
- package com.it.config;
-
- import com.it.web.LoginInterceptor;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
- import java.util.logging.Handler;
-
- @Configuration
- public class MyAppConfig implements WebMvcConfigurer {
-
- //添加拦截器对象,注入到容器中
-
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- //创建拦截器对象
- HandlerInterceptor interceptor=new LoginInterceptor();
- //指定拦截的请求uri地址
- String path []={"/user/**"};
- //指定不拦截的地址
- String excludePath []={"/user/login"};
- registry.addInterceptor(interceptor)
- .addPathPatterns(path)
- .excludePathPatterns(excludePath);
-
-
- }
- }
4.创建控制类BootController类
- package com.it.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- @Controller
- public class BootController {
-
- @RequestMapping("/user/account")
- @ResponseBody
- public String userAccount(){
- return "访问user/account地址";
- }
-
- @RequestMapping("/user/login")
- @ResponseBody
- public String userLogin(){
- return "访问user/login地址";
- }
-
- }
5.运行Application文件,启动Tomcat进行测试
清空控制台重新访问/user/login地址,如果没有拦截,控制台就不会打印上面的那句话