按路径模式排除Spring Request HandlerInterceptor [英] Exclude Spring Request HandlerInterceptor by Path-Pattern
问题描述
我知道我们可以将不同的URL映射到不同的拦截器,或者也可以将多个URL映射到单个拦截器.我只是想知道我们是否还有排除选项.例如,如果我在应用程序中有50个url映射,而除1个映射之外,我想全部调用interceptor,那么除了编写49映射的配置外,我可以只提及*,而排除第50个url吗?
I know we can map different url to different interceptor, or we can map multiple url to single interceptor too. I am just curious to know if we also have exclude option. for example if I have 50 url mapping in application and except 1 mapping I want to call interceptor for all so rather than writing configuration for 49 mapping can I just mention * and one exclude to the 50th url?
推荐答案
HandlerInterceptor
可以应用于或排除于(多个)特定的url或url模式.
HandlerInterceptor
s can be applied or excluded to (multiple) specific url's or url-patterns.
请参见 MVC拦截器配置.
以下是文档中的示例
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LocaleInterceptor());
registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");
// multiple urls (same is possible for `exludePathPatterns`)
registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*", "/admin/**", "/profile/**");
}
}
或使用XML配置
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/admin/**"/>
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<!-- intercept multiple urls -->
<mvc:mapping path="/secure/*"/>
<mvc:mapping path="/admin/**"/>
<mvc:mapping path="/profile/**"/>
<bean class="org.example.SecurityInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
这篇关于按路径模式排除Spring Request HandlerInterceptor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!