Java Spring MVC 处理程序拦截器不运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23293368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC Handler Interceptor does not run
提问by libik
I have following interceptor class :
我有以下拦截器类:
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;
}
}
I configured it inside my spring-webmvc.xml as following :
我在 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>
However when I go to http://localhost:8080/myserver/rest/api/01/status
, I get regular answer with status code 200 (same as before I added interceptor). Also, message "fuu" is not printed (so the preHandle method is not called).
但是,当我转到 时http://localhost:8080/myserver/rest/api/01/status
,我会收到状态代码为 200 的常规答案(与添加拦截器之前相同)。此外,不会打印消息“fuu”(因此不会调用 preHandle 方法)。
Any ideas? I started to do it with this example : http://javapapers.com/spring/spring-mvc-handler-interceptor/, but all other examples look the same, I cant find where I go wrong.
有任何想法吗?我开始用这个例子来做:http: //javapapers.com/spring/spring-mvc-handler-interceptor/,但所有其他例子看起来都一样,我找不到哪里出错了。
I am using Spring 3.2.4.RELEASE
我正在使用春天 3.2.4.RELEASE
Important edit, it works with this :
重要的编辑,它适用于这个:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
So the question is, what is wrong with my path?
所以问题是,我的路径有什么问题?
采纳答案by libik
Ok, I found solution, because my path is defined with following :
好的,我找到了解决方案,因为我的路径定义如下:
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
And this is how my controller looks like
这就是我的控制器的样子
@Controller
@RequestMapping(value = "/api")
public class ApiController {
@RequestMapping(value = "/01/status", method = RequestMethod.GET)
@ResponseBody
public ServerStatusJSON getStatus(HttpServletResponse response) {
...
}
}
The working configuration for this address : http://localhost:8080/myserver/rest/api/01/status
is as following :
该地址的工作配置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 : My thanks to geoand, he pushed me to the right way.
PS:感谢geoand,他把我推向了正确的道路。
回答by Richard Xue
I solved this problem by changing the value of mvc:mapping
. My working configuration is:
我通过更改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>
回答by Aniket Thakur
Providing additional explanation as to why this method worked for libik. As he has mentioned his controller looks like -
提供有关此方法为何适用于 libik 的额外解释。正如他所提到的,他的控制器看起来像 -
@Controller
@RequestMapping(value = "/api")
public class ApiController {
@RequestMapping(value = "/01/status", method = RequestMethod.GET)
@ResponseBody
public ServerStatusJSON getStatus(HttpServletResponse response) {
...
}
}
And also remember interceptors are at HandlerMapping
level. In this case it would be RequestMappingHandlerMapping
(Spring 3.1+ with mvc:annotation-driven
) or DefaultAnnotationHandlerMapping
. and the mapping here would be for
/api/01/status
which is precisely what he has done.
还要记住拦截器处于HandlerMapping
水平状态。在这种情况下,它将是RequestMappingHandlerMapping
(Spring 3.1+ with 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>
If you want it to be applied for all patterns you can simply do <mvc:mapping path="/**"/>
- this will match all URLs (including subpaths) or you can simply invoke interceptors for all HandlerMappings -
如果您希望将其应用于所有模式,您可以简单地执行<mvc:mapping path="/**"/>
- 这将匹配所有 URL(包括子路径),或者您可以简单地为所有 HandlerMappings 调用拦截器 -
<mvc:interceptors>
<bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterc??eptor" />
</mvc:interceptors>