2016-01-23 93 views
2

我知道我們可以將不同的url映射到不同的攔截器,或者我們也可以將多個url映射到單個攔截器。我只是想知道我們是否也有排除選項。例如,如果我在應用程序中有50個url映射,除了1個映射,我想爲所有的調用攔截器,而不是爲49映射編寫配置,我可以只提及*和一個排除到第50個url?按路徑模式排除Spring Request HandlerInterceptor

回答

6

HandlerInterceptor s可以應用或排除(多個)特定的url或url模式。

查看MVC Interceptor Configuration

下面是從文檔

@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> 
+0

三江源非常多的例子。 –

+0

爲什麼大家總是隻舉這個例子?我有多個映射需要排除,這個例子對我沒有任何幫助。 – Encryption

+0

好點。你甚至看過架構/方法簽名嗎?無論如何。更新了答案。 – fateddy