2012-03-28 562 views

回答

64

從Spring 3.2它們添加了標籤

mvc:exclude-mapping 

該功能從Spring文檔見這個例子:

<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> 
    <mvc:mapping path="/secure/*"/> 
    <bean class="org.example.SecurityInterceptor" /> 
</mvc:interceptor> 

這裏的link到文檔

+0

非常感謝!我會試試看! – momomo 2013-04-12 09:36:58

+2

確保xsd指向3.2。我花了十分鐘試圖弄清楚什麼是錯的。 xsi:schemaLocation =「http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd \t \t http://www.springframework。 org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd \t \t http://www.springframework.org/schema/mvc http://www.springframework。組織/架構/ MVC /彈簧-MVC-3.2.xsd「> – RuntimeException 2014-11-09 21:35:49

2

配置攔截器時,可以指定路徑模式。攔截器只會被調用,其路徑與攔截器路徑模式相匹配。

裁判:http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-interceptor

但是,正如你可能會注意到它的路徑模式不支持排除。

所以我認爲唯一的方法是編碼攔截器內的路徑黑名單。當攔截器被調用時,檢索HttpServletRequest.getRequestURI()並檢查路徑是否被列入黑名單。

您可以在攔截器的註釋方法內部構建黑名單,並從例如屬性文件中獲取黑名單路徑。

+0

是的,問題是什麼的正確方法是確定,因爲控制器的映射控制器和動作理論上可以以多種方式建立起來......春天知道控制器是什麼之後被解僱攔截器,因爲處理程序對象是控制器實例。要正確地做到這一點,必須使用春天確定目標動作的相同機制來查找它......任何一個? :) – momomo 2012-03-29 14:16:59

+0

Spring用來確定正確的控制器的機制依賴於@ @ RequestMapping註解。所以爲攔截器做同樣的事情意味着基本上用'@ RequestMapping'來註釋一個攔截器。但目前這是不可能的。 – tbruyelle 2012-03-29 17:33:41

14

基於java的配置,從docs

@Configuration 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addInterceptors(InterceptorRegistry registry) { 
     registry.addInterceptor(new LocaleInterceptor()); 
     registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**"); 
     registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*"); 
    } 

}