那么在 springBoot 中如何使用拦截器呢?
步骤:
HandlerInterceptor 接口
,WebMvcConfigurer接口
,重写 addInterceptors 方法
。创建我们自己的拦截器类并实现 HandlerInterceptor 接口。
package com.feng.springboot_interceptor.interceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor....在请求处理之前进行调用(Controller方法调用之前)");
String requestUrl = request.getRequestURI();
System.out.println("过滤器MyFilter拦截了请求为" + requestUrl);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor...请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)");
//HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("MyInterceptor....在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对 应的视图之后执行(主要是用于进行资源清理工作)");
//HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
修改 application.properties 加入开发接口通配地址
#凡是请求地址层级带有 open 都放行
open:
url: /**/open/**
创建一个 Java 实现 WebMvcConfigurer , 并重写 addInterceptors 方法。
package com.feng.springboot_interceptor.config;
import com.feng.springboot_interceptor.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
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 WebAppConfig implements WebMvcConfigurer {
@Value("${open.url}")
private String openUrl;
@Bean
public MyInterceptor getMyInterceptor() {
return new MyInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 拦截以 /api 开头的请求,但是排除 openUrl:/**/open/**。 即 :匹配这个(/**/open/**)URL的所有请求不拦截
registry.addInterceptor(getMyInterceptor()).addPathPatterns("/api/**").excludePathPatterns(openUrl);
// WebMvcConfigurer.super.addInterceptors(registry);
}
}
package com.feng.springboot_interceptor.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class InterceptorController {
@GetMapping("/home/open/info")
public String home() {
return "欢迎来到首页";
}
@GetMapping("/user/interceptor")
public String interceptor() {
return "我被拦截了并通过了拦截器";
}
}
http://localhost:8080/api/open/home/info
不拦截http://localhost:8080/api/user/interceptor
拦截 @GetMapping("/open/unLogin") // 带 open 放行
public String getUnauthorized() {
return "登录失效,请重新登录";
}
package com.feng.springboot_interceptor.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class InterceptorController {
@GetMapping("/home/open/info") // 带 open 放行
public String home() {
return "欢迎来到首页";
}
@GetMapping("/user/interceptor")
public String interceptor() {
return "我被拦截了并通过了拦截器";
}
@GetMapping("/open/unLogin") // 带 open 放行
public String getUnauthorized() {
return "登录失效,请重新登录";
}
}
package com.feng.springboot_interceptor.interceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor....在请求处理之前进行调用(Controller方法调用之前)");
String requestUrl = request.getRequestURI();
System.out.println(requestUrl + "被 MyInterceptor 拦截了"); //判断是否携带凭证就可以了
String token = request.getHeader("token");
if (StringUtils.isEmpty(token)) {
request.getRequestDispatcher("/api/open/unLogin").forward(request, response);
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor...请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)");
//HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("MyInterceptor....在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对 应的视图之后执行(主要是用于进行资源清理工作)");
//HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
http://localhost:8080/api/home/open/info
http://localhost:8080/api/user/interceptor
http://localhost:8080/api/user/interceptor