【发布时间】: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