SpringBoot拦截器之拦截模式配置

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类下SpringBoot拦截器之拦截模式配置_第1张图片
    根据观察InterceptorRegistration类使用的是PathMatcher,而PathMatcher的默认实现就是AntPathMatcher类SpringBoot拦截器之拦截模式配置_第2张图片
    那么我们只要看AntPathMatcher类是整么处理模式匹配规则即可。
/**
 * {@link PathMatcher} implementation for Ant-style path patterns.
 *
 * 

Part of this mapping code has been kindly borrowed from Apache Ant. * *

The mapping matches URLs using the following rules:
*

    *
  • {@code ?} matches one character
  • *
  • {@code *} matches zero or more characters
  • *
  • {@code **} matches zero or more directories in a path
  • *
  • {@code {spring:[a-z]+}} matches the regexp {@code [a-z]+} as a path variable named "spring"
  • *
* *

Examples

*
    *
  • {@code com/t?st.jsp} — matches {@code com/test.jsp} but also * {@code com/tast.jsp} or {@code com/txst.jsp}
  • *
  • {@code com/*.jsp} — matches all {@code .jsp} files in the * {@code com} directory
  • *
  • com/**/test.jsp — matches all {@code test.jsp} * files underneath the {@code com} path
  • *
  • org/springframework/**/*.jsp — matches all * {@code .jsp} files underneath the {@code org/springframework} path
  • *
  • org/**/servlet/bla.jsp — matches * {@code org/springframework/servlet/bla.jsp} but also * {@code org/springframework/testing/servlet/bla.jsp} and {@code org/servlet/bla.jsp}
  • *
  • {@code com/{filename:\\w+}.jsp} will match {@code com/test.jsp} and assign the value {@code test} * to the {@code filename} variable
  • *
* *

Note: 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类的详细解释,具体点说就是:
’ ? ’ 匹配一个字符
’ * ’ 匹配零个或多个字符
’ ** ’ 匹配路径中的零个或多个目录
所以只要更具自己的需求来配置即可。

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

你可能感兴趣的:(SpringBoot)