如何在SpringBoot中添加拦截器忽略请求URL当中的指定字符串
作者:@小匠
这篇文章主要介绍了在SpringBoot中添加拦截器忽略请求URL当中的指定字符串,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
在SpringBoot中添加拦截器忽略请求URL当中的指定字符串
1 自定义拦截器
@Component public class GlobalInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String path = request.getRequestURI(); if (path.contains("/api/")) { path = path.replaceAll("/api/", "/"); request.getRequestDispatcher(path).forward(request, response); } return true; } }
2 注册拦截器
@Configuration @EnableWebMvc public class WebMvcConfig implements WebMvcConfigurer { GlobalInterceptor globalInterceptor; @Autowired public WebMvcConfig(GlobalInterceptor globalInterceptor) { this.globalInterceptor = globalInterceptor; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(globalInterceptor).addPathPatterns("/api/**"); } }
SpringBoot 使用拦截器(配置特定的url请求会进入拦截器)
SpringBoot 使用拦截器步骤为:
1、按照Spring mvc的方式编写一个拦截器类;
创建一个interceptor包
LoginInterceptor: package com.springboot.web.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor implements HandlerInterceptor { //主要是这个方法中实现拦截逻辑 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("已经进入拦截器"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
2、编写一个配置类继承WebMvcConfigurerAdapter类
3、为该配置类添加@Configuration注解,标注此类为一个配置类,让Spring boot 扫描到;
4、覆盖其中的addInterceptors方法并添加已经编写好的拦截器:
代码如下:编写WebConfig配置类
package com.springboot.web.config; import com.springboot.web.interceptor.LoginInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration //一定要加上这个注解,成为Springboot的配置类,不然不会生效 public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { String[] addPathPatterns = {//拦截所有请求 "/*" }; String[] excludePatterns = {//不拦截sayHi这个请求 【http://localhost:8080/sayHi】 "/sayHi" }; //如果有多个拦截器,可以继续添加 registry.addInterceptor(new LoginInterceptor()).addPathPatterns(addPathPatterns).excludePathPatterns(excludePatterns); } }
测试下:
【http://localhost:8080/config】会打印“已经进入拦截器”,说明拦截器生效
【http://localhost:8080/sayHi】不会打印,说明不拦截配置生效。
到此这篇关于如何在SpringBoot中添加拦截器忽略请求URL当中的指定字符串的文章就介绍到这了,更多相关SpringBoot添加拦截器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!