1.添加pom.xml使用的依赖
<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>top.ytheng</groupId> <artifactId>springboot-demo</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> </dependencies> <build> <!-- 打包的名称 --> <finalName>myspringboot</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
2.添加拦截器Login业务类
package top.ytheng.demo.intecpter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * 拦截器业务类 * * */ public class LoginIntecepter implements HandlerInterceptor { /** * 调用Controller之前 * * */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub System.out.println("LoginIntecepter------>preHandle"); //这里可以做一些access_token的校验 String token = request.getParameter("token"); System.out.println(token); return HandlerInterceptor.super.preHandle(request, response, handler); } /** * 调用完Controller之后,视图渲染之前 * * */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub System.out.println("LoginIntecepter------>postHandle"); HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); } /** * 整个完成之后 * * */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub System.out.println("LoginIntecepter------>afterCompletion"); HandlerInterceptor.super.afterCompletion(request, response, handler, ex); } }
3.添加拦截器2业务类
package top.ytheng.demo.intecpter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * 拦截器业务类 * * */ public class TwoIntecepter implements HandlerInterceptor { /** * 调用Controller之前 * * */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub System.out.println("TwoIntecepter------>preHandle"); return HandlerInterceptor.super.preHandle(request, response, handler); } /** * 调用完Controller之后,视图渲染之前 * * */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub System.out.println("TwoIntecepter------>postHandle"); HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); } /** * 整个完成之后 * * */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub System.out.println("TwoIntecepter------>afterCompletion"); HandlerInterceptor.super.afterCompletion(request, response, handler, ex); } }
4.添加自定义拦截器
package top.ytheng.demo.intecpter; 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 CustomWebMvcConfigurer implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { // TODO Auto-generated method stub //拦截全部/** //最后一个必须为/**,如果是目录的话则是/*/ //第一个拦截器 registry.addInterceptor(new LoginIntecepter()).addPathPatterns("/api2/*/**"); //排除拦截的地址 // .excludePathPatterns("/api2/xxx"); //第二个拦截器 registry.addInterceptor(new TwoIntecepter()).addPathPatterns("/api2/*/**"); WebMvcConfigurer.super.addInterceptors(registry); } }
5.添加控制器
package top.ytheng.demo.controller; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api2/v1") public class CustomIntecepterController { @GetMapping("/test/filter") public Object testFilter() { System.out.println("CustomIntecepterController------>testFilter"); Map<String, Object> map = new HashMap<>(); map.put("username", "theng"); map.put("password", "123"); return map; } }
6.添加启动类
package top.ytheng.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication //等于下面3个 //@SpringBootConfiguration //@EnableAutoConfiguration //@ComponentScan //Filter,拦截器用到 @ServletComponentScan public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
7.右键项目Run As启动,测试地址
http://localhost:8080/api2/v1/test/filter
测试结果截图:
总结:
1.自定义拦截器 HandlerInterceptor preHandle:调用Controller某个方法之前 postHandle:Controller之后调用,视图渲染之前,如果控制器Controller出现了异常,则不会执行此方法 afterCompletion:不管有没有异常,这个afterCompletion都会被调用,用于资源清理 2.按照注册顺序进行拦截,先注册,先被拦截 拦截器不生效常见问题: 1)是否有加@Configuration 2)拦截路径是否有问题** 和 * 3)拦截器最后路径一定要"/**",如果是目录的话则是/*/ Filter 是基于函数回调doFilter(),而Interceptor则是基于AOP思想; 只在Servlet前后起作用,而Interceptor能够深入到方法前后,异常抛出前后等; 依赖于Servlet容器即Web应用中,而Interceptor不依赖于Servlet容器,所以可以运行在多种环境中; 在接口调用的生命周期里,Interceptor可以被多次调用,而Filter只能在容器初始化时调用一次; 3.Filter和Interceptor的执行顺序 过滤前->拦截前->action执行->拦截后->过滤后
另附:
原文地址:https://www.cnblogs.com/tianhengblogs/p/9784817.html
时间: 2024-10-13 02:12:48