1

我正在尝试创建一个拦截器以在调用控制器之前记录 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);

}

}

但它似乎不起作用。我正在使用弹簧 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 安全性的拦截器吗?

4

1 回答 1

0

可能是由于创建了 handlerMapping mvc:annotation-driven,我见过的最佳建议是删除mvc:annotation-driven,用适当的 HandlerAdapter 的显式 bean 定义替换(AnnotationMethodHandlerAdapter在 3.0 中),并显式指定 ahandlerMapping作为属性,其中定义了拦截器。

如何使用 spring mvc 3.0 注册处理程序拦截器?

于 2012-05-30T19:01:13.473 回答