SpringBoot拦截器之接口拦截配置
- 自定义的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) {
}
}
- 自定义的拦截器
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;
}
}
- 将拦截器注入到适配器中
重写自定义的MVC适配器中的addInterceptors方法,该方法是负责拦截URL请求的。
@Override
public void addInterceptors(InterceptorRegistry registry) {
// ' /** '是拦截所有请求
// registry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**");
// 自定义URL拦截器模式
registry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**/xxx/**");
}
- 如何自定义拦截器的模式
我们可以发现在适配器中注入的自定义的拦截器中可以添加路径匹配模式。添加路径匹配模式使用的是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} — matches {@code com/test.jsp} but also
* {@code com/tast.jsp} or {@code com/txst.jsp}</li>
* <li>{@code com/*.jsp} — matches all {@code .jsp} files in the
* {@code com} directory</li>
* <li><code>com/**/test.jsp</code> — matches all {@code test.jsp}
* files underneath the {@code com} path</li>
* <li><code>org/springframework/**/*.jsp</code> — matches all
* {@code .jsp} files underneath the {@code org/springframework} path</li>
* <li><code>org/**/servlet/bla.jsp</code> — 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类的详细解释,具体点说就是:
’ ? ’ 匹配一个字符
’ * ’ 匹配零个或多个字符
’ ** ’ 匹配路径中的零个或多个目录
所以只要更具自己的需求来配置即可。
初来乍到,欢迎大家指正。