2012-05-30 44 views
1

我試圖創建一個攔截器來記錄http請求細節,然後調用控制器。我的春節,XML是spring 3.0.5 <mvc:攔截器>不工作

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:tx="http://www.springframework.org/schema/tx" 

xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" 
    > 

<context:component-scan base-package="com.xxx.controller" /> 

<mvc:interceptors> 
    <mvc:interceptor> 
    <mvc:mapping path="/**" /> 
    <bean class="com.xxx.interceptor.LogBillingInterceptor"> 
</bean> 
</mvc:interceptor> 
</mvc:interceptors> 

我的攔截器類

public class LogBillingInterceptor extends HandlerInterceptorAdapter{ 

@Override 
public boolean preHandle(HttpServletRequest request, 
     HttpServletResponse response, Object handler) throws Exception { 
    System.out.println("sdfsdfdsfd"); 
    return super.preHandle(request, response, handler); 
} 

@Override 
public void postHandle(HttpServletRequest request, 
     HttpServletResponse response, Object handler, 
     ModelAndView modelAndView) throws Exception { 
    System.out.println("aaaaaaaasdfsdfdsfd"); 
    super.postHandle(request, response, handler, modelAndView); 
} 

@Override 
public void afterCompletion(HttpServletRequest request, 
     HttpServletResponse response, Object handler, Exception ex) 
     throws Exception { 
    System.out.println("qqqqqqqqqqsdfsdfdsfd"); 
    super.afterCompletion(request, response, handler, ex); 

} 

}

但它似乎沒有工作。我正在使用彈簧3.0.5

我剛纔看到我們使用的是spring-oauth2並使用以下命令。

<http access-denied-page="/error" access-decision-manager-ref="accessDecisionManager" entry-point-ref="loginUrlAuthenticationEntryPoint" xmlns="http://www.springframework.org/schema/security"> 

    <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 
    <intercept-url pattern="/oauth/**" access="ROLE_USER" /> 

    <intercept-url pattern="/store/**" access="ROLE_USER" /> 
    <intercept-url pattern="/balance/**" access="ROLE_USER" /> 
    <intercept-url pattern="/api/**" access="ROLE_USER" /> 
    <intercept-url pattern="/tapjoy" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 

    <intercept-url pattern="/verify_credentials" access="ROLE_USER" /> 

    <intercept-url pattern="/welcome/**" access="ROLE_USER" /> 
    <intercept-url pattern="/logout/**" access="ROLE_USER" /> 

    <intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 
    <intercept-url pattern="/loginfailed" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 

    <intercept-url pattern="/request_token_authorized.jsp" access="ROLE_USER,DENY_OAUTH" /> 

     <custom-filter position="FORM_LOGIN_FILTER" ref="tpAuthenticationFilter" /> 
</http> 

我們可以添加攔截器在Spring Security ..

+0

懷疑它可能是春季安全@harshit,DispatcherServlet會前的Spring Security攔截獲取請求,通過過濾器和攔截器處理調用控制器之前的請求。 –

回答

0

這是因爲通過mvc:annotation-driven創建的處理器映射的可能,是我見過的最好的建議是取消mvc:annotation-driven,有一個明確的bean定義爲替代適當的HandlerAdapter(3.034中的AnnotationMethodHandlerAdapter),並明確指定handlerMapping作爲其中定義了攔截器的屬性。

How to register handler interceptors with spring mvc 3.0?

+1

我想那是有效的春天3.1和我使用3.0.5和XML顯示錯誤。 – harshit

+0

是的,這是真的@harshit,它也是無效的3.1也 - 這個鏈接有一個更好的答案 - http://stackoverflow.com/q/3230633/204788 –