InterceptorRegistration
org.springframework.web.servlet.config.annotation.InterceptorRegistration 用於協助創建{@link MappedInterceptor}。
addPathPatterns方法(指定攔截路徑,往往使用 "/**")
/**
* 添加註冊攔截器應該應用到的URL模式。
*/
public InterceptorRegistration addPathPatterns(String... patterns) {
return addPathPatterns(Arrays.asList(patterns));
}
/**
* 基於列表的{@link #addPathPatterns(String…)}變體。
*/
public InterceptorRegistration addPathPatterns(List<String> patterns) {
this.includePatterns.addAll(patterns);
return this;
}
excludePathPatterns方法(指定排除攔截路徑,用於登錄等部分開放接口)
/**
* 添加註冊攔截器不應該應用到的URL模式。
*/
public InterceptorRegistration excludePathPatterns(String... patterns) {
return excludePathPatterns(Arrays.asList(patterns));
}
/**
* 基於列表的{@link #excludePathPatterns(String…)}變體。
* @since 5.0.3
*/
public InterceptorRegistration excludePathPatterns(List<String> patterns) {
this.excludePatterns.addAll(patterns);
return this;
}
pathMatcher方法(高級方式)
/**
* 與此攔截器一起使用的路徑匹配器實現。
* 這是一個可選的高級屬性,只有在使用自定義路徑匹配器實現時才需要,
* 該實現支持映射元數據,而默認情況下不支持Ant路徑模式。
*/
public InterceptorRegistration pathMatcher(PathMatcher pathMatcher) {
this.pathMatcher = pathMatcher;
return this;
}
order方法
/**
* 指定要使用的訂單位置。默認值爲0。
* @since 5.0
*/
public InterceptorRegistration order(int order){
this.order = order;
return this;
}
getOrder方法
/**
* 返回要使用的訂單位置。
* @since 5.0
*/
protected int getOrder() {
return this.order;
}
getInterceptor方法
/**
* 構建底層攔截器。如果提供URL模式,則返回類型爲{@link MappedInterceptor};
* 否則{@link HandlerInterceptor}。
*/
protected Object getInterceptor() {
if (this.includePatterns.isEmpty() && this.excludePatterns.isEmpty()) {
return this.interceptor;
}
String[] include = StringUtils.toStringArray(this.includePatterns);
String[] exclude = StringUtils.toStringArray(this.excludePatterns);
MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor);
if (this.pathMatcher != null) {
mappedInterceptor.setPathMatcher(this.pathMatcher);
}
return mappedInterceptor;
}