1、两者的关系
2、过滤器的使用步骤
实现Filter接口,并加上@WebFilter注解
@Slf4j
// filterName 为该过滤器的名称;
// urlPatterns 对哪些url进行拦截
@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
public class LoginCheckFilter implements Filter { // 路径匹配器 public static final AntPathMatcher antPathMatcher = new AntPathMatcher();
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; String requestURI = httpServletRequest.getRequestURI(); log.info("拦截到请求: {}", requestURI); // 定义不需要拦截的url String[] urls = new String[]{ // 匹配目录 "/backend/page/**", };
boolean check = check(urls, requestURI); if (check){ log.info("本次拦截{}不需要处理", requestURI);
//通过filterChain.doFilter来放行请求
filterChain.doFilter(httpServletRequest, httpServletResponse); return; }
// 向页面返回相应数据 // httpServletResponse.getWriter().write(JSON.toJSONString(Result.error("NOLOGIN")));
// 重定向 httpServletResponse.sendRedirect("/backend/page/login/login.html"); } public boolean check(String[] urls, String requestURL){ for (String url : urls) { boolean match = antPathMatcher.match(url, requestURL); if(match){ return true; } } return false; } }
在启动类上加上@ServletComponentScan
@Slf4j @ServletComponentScan @SpringBootApplication public class ReggieApplication { public static void main(String[] args) { SpringApplication.run(ReggieApplication.class, args); } }
3、拦截器的使用步骤
实现HandlerInterceptor接口
public class MyInterceptor implements HandlerInterceptor { // 在目标方法执行之前执行 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle is running"); // false 阻止;true 放行 return true; // return HandlerInterceptor.super.preHandle(request, response, handler); } // 在目标方法执行之后,视图对象返回之前执行 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle is running"); HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); } // 在流程都执行完毕后执行 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion is running"); HandlerInterceptor.super.afterCompletion(request, response, handler, ex); } }
注册该拦截器
@Configuration public class InterceptorConfigurer implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { // 自己定义的拦截器 MyInterceptor myInterceptor = new MyInterceptor(); // 对所有目标方法都执行拦截操作 // addPathPatterns表示黑名单,excludePathPatterns表示白名单 registry.addInterceptor(myInterceptor).addPathPatterns("/**"); } }
标签:拦截器,String,request,public,handler,过滤器,response,SpringBoot From: https://www.cnblogs.com/ReturnOfTheKing/p/17159801.html