【问题标题】:Spring MVC Handler Interceptor does not runSpring MVC 处理程序拦截器不运行
【发布时间】:2014-06-11 04:23:05
【问题描述】:

我有以下拦截器类:

package cz.coffeeexperts.feedback.server.web.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class RestAuthorizationInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, 
            HttpServletResponse response, Object handler)
        throws Exception {

        System.out.println("fuu");
        response.setStatus( HttpServletResponse.SC_UNAUTHORIZED );
        return false;
    }
}

我在 spring-webmvc.xml 中配置如下:

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

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:annotation-driven/>

    <mvc:interceptors>
     <mvc:interceptor>
       <mvc:mapping path="/rest/api/01/status" />
       <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
     </mvc:interceptor>
    </mvc:interceptors>     

</beans>

但是,当我转到 http://localhost:8080/myserver/rest/api/01/status 时,我会得到状态码 200 的常规答案(与添加拦截器之前相同)。此外,不打印消息“fuu”(因此不调用 preHandle 方法)。

有什么想法吗?我开始使用这个示例:http://javapapers.com/spring/spring-mvc-handler-interceptor/,但所有其他示例看起来都一样,我找不到哪里出错了。

我正在使用 Spring 3.2.4.RELEASE


重要的编辑,它适用于这个:

<mvc:interceptors>
 <mvc:interceptor>
   <mvc:mapping path="/**" />
   <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>  

所以问题是,我的路径有什么问题?

【问题讨论】:

  • 任何日志?错误?作为它的预句柄,响应将被您的普通控制器方法覆盖,不是吗 - 预句柄通常用于处理请求。
  • 什么都没有发生,没有错误,没有调用该方法(我说过,没有 System.out.println("fuu") 的输出出现)。此外,当您在 preHandle 上返回 false 时,它​​应该拦截控制器方法...
  • 删除映射后拦截器是否工作?
  • @geoand - SAXError 出现:Invalid content was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/mvc":mapping}' is expected.
  • 试试这个:

标签: java spring spring-mvc interceptor


【解决方案1】:

好的,我找到了解决方案,因为我的路径定义如下:

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

这就是我的控制器的样子

@Controller
@RequestMapping(value = "/api")
public class ApiController {
 @RequestMapping(value = "/01/status", method = RequestMethod.GET)
    @ResponseBody
    public ServerStatusJSON getStatus(HttpServletResponse response) {
        ...
    }
}

这个地址的工作配置:http://localhost:8080/myserver/rest/api/01/status如下:

<mvc:interceptors>
 <mvc:interceptor>
   <mvc:mapping path="/api/01/status" />
   <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>    

PS : 感谢 geoand,他把我推向了正确的方向。

【讨论】:

    【解决方案2】:

    我通过更改mvc:mapping 的值解决了这个问题。我的工作配置是:

        <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="cn.mmd.micro.common.TokenInterceptor">
                <property name="excludeUrls">
                    <list>
                        <value>/app/token</value>
                    </list>
                </property>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>
    

    【讨论】:

      【解决方案3】:

      提供有关此方法为何适用于 libik 的附加说明。正如他所提到的,他的控制器看起来像 -

      @Controller
      @RequestMapping(value = "/api")
      public class ApiController {
       @RequestMapping(value = "/01/status", method = RequestMethod.GET)
          @ResponseBody
          public ServerStatusJSON getStatus(HttpServletResponse response) {
              ...
          }
      }
      

      还要记住拦截器位于HandlerMapping 级别。在这种情况下,它将是RequestMappingHandlerMapping(Spring 3.1+ 和mvc:annotation-driven)或DefaultAnnotationHandlerMapping。这里的映射将用于 /api/01/status 这正是他所做的。

      <mvc:interceptors>
       <mvc:interceptor>
         <mvc:mapping path="/api/01/status" />
         <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
       </mvc:interceptor>
      </mvc:interceptors>   
      

      如果您希望将其应用于所有模式,您可以简单地执行 &lt;mvc:mapping path="/**"/&gt; - 这将匹配所有 URL(包括子路径),或者您可以简单地为所有 HandlerMappings 调用拦截器 -

      <mvc:interceptors> 
      <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterc‌​eptor" /> 
      </mvc:interceptors>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多