SpringBoot拦截器之拦截模式配置

本文介绍了如何在SpringBoot中自定义MVC适配器和拦截器,并详细讲解了拦截器的路径匹配模式,包括'?'、'*'和'**'的匹配规则,以及AntPathMatcher类在模式匹配中的作用。通过理解这些,读者可以更好地配置拦截器以适应不同的URL请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot拦截器之接口拦截配置

  1. 自定义的MVC适配器
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
   @Override
   public void addViewControllers( ViewControllerRegistry registry ) {
       super.addViewControllers( registry );
   }

   public void addResourceHandlers(ResourceHandlerRegistry registry) {
   }
   @Override
   public void addInterceptors(InterceptorRegistry registry) {
   }
}
  1. 自定义的拦截器
public class CommonInterceptor implements HandlerInterceptor {

  @Override
  public void afterCompletion(HttpServletRequest arg0,
  		HttpServletResponse arg1, Object arg2, Exception arg3)
  		throws Exception {
  }

  @Override
  public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
  		Object arg2, ModelAndView arg3) throws Exception {
  	String method = httpServletRequest.getMethod();
  	if ( "POST".equalsIgnoreCase(method)) {
  		 System.out.println("Method is Post");
  		  return true;
  	}
     return false;
  }

  @Override
  public boolean preHandle(HttpServletRequest request,
  		HttpServletResponse response, Object arg2) throws Exception {
  	// 此处必须返回true ,不然在此处拦截器就会结束,不会在处理postHandle中的操作
  	return true; 
  }
}
  1. 将拦截器注入到适配器中
    重写自定义的MVC适配器中的addInterceptors方法,该方法是负责拦截URL请求的。
  @Override
    public void addInterceptors(InterceptorRegistry registry) {
       // ' /** '是拦截所有请求
       // registry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**");
       // 自定义URL拦截器模式
        registry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**/xxx/**");
    }
  1. 如何自定义拦截器的模式
    我们可以发现在适配器中注入的自定义的拦截器中可以添加路径匹配模式。添加路径匹配模式使用的是addPathPatterns方法,该方法位于InterceptorRegistration类下在这里插入图片描述
    根据观察InterceptorRegistration类使用的是PathMatcher,而PathMatcher的默认实现就是AntPathMatcher类在这里插入图片描述
    那么我们只要看AntPathMatcher类是整么处理模式匹配规则即可。
/**
 * {@link PathMatcher} implementation for Ant-style path patterns.
 *
 * <p>Part of this mapping code has been kindly borrowed from <a href="http://ant.apache.org">Apache Ant</a>.
 *
 * <p>The mapping matches URLs using the following rules:<br>
 * <ul>
 * <li>{@code ?} matches one character</li>
 * <li>{@code *} matches zero or more characters</li>
 * <li>{@code **} matches zero or more <em>directories</em> in a path</li>
 * <li>{@code {spring:[a-z]+}} matches the regexp {@code [a-z]+} as a path variable named "spring"</li>
 * </ul>
 *
 * <h3>Examples</h3>
 * <ul>
 * <li>{@code com/t?st.jsp} &mdash; matches {@code com/test.jsp} but also
 * {@code com/tast.jsp} or {@code com/txst.jsp}</li>
 * <li>{@code com/*.jsp} &mdash; matches all {@code .jsp} files in the
 * {@code com} directory</li>
 * <li><code>com/&#42;&#42;/test.jsp</code> &mdash; matches all {@code test.jsp}
 * files underneath the {@code com} path</li>
 * <li><code>org/springframework/&#42;&#42;/*.jsp</code> &mdash; matches all
 * {@code .jsp} files underneath the {@code org/springframework} path</li>
 * <li><code>org/&#42;&#42;/servlet/bla.jsp</code> &mdash; matches
 * {@code org/springframework/servlet/bla.jsp} but also
 * {@code org/springframework/testing/servlet/bla.jsp} and {@code org/servlet/bla.jsp}</li>
 * <li>{@code com/{filename:\\w+}.jsp} will match {@code com/test.jsp} and assign the value {@code test}
 * to the {@code filename} variable</li>
 * </ul>
 *
 * <p><strong>Note:</strong> a pattern and a path must both be absolute or must
 * both be relative in order for the two to match. Therefore it is recommended
 * that users of this implementation to sanitize patterns in order to prefix
 * them with "/" as it makes sense in the context in which they're used.
 *
 * @author Alef Arendsen
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @author Arjen Poutsma
 * @author Rossen Stoyanchev
 * @author Sam Brannen
 * @since 16.07.2003
 */

上面一段就是AntPathMatcher类的详细解释,具体点说就是:
’ ? ’ 匹配一个字符
’ * ’ 匹配零个或多个字符
’ ** ’ 匹配路径中的零个或多个目录
所以只要更具自己的需求来配置即可。

初来乍到,欢迎大家指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值