spring 3.0.5 <mvc:interceptor> not working </mvc:interceptor>

时间:2012-05-30 18:55:12

标签: java spring spring-mvc interceptor

我正在尝试创建一个拦截器,以便在调用控制器之前记录http请求详细信息。我的春天xml是

<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);

}

}

但它似乎没有用。我使用的是spring 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安全性添加拦截器吗。

1 个答案:

答案 0 :(得分:0)

可能是因为mvc:annotation-driven创建的handlerMapping,我看到的最好建议是删除mvc:annotation-driven,替换为适当的HandlerAdapter的显式bean定义(AnnotationMethodHandlerAdapter在3.0)中,并明确指定handlerMapping作为属性,并在那里定义了拦截器。

How to register handler interceptors with spring mvc 3.0?