libik
libik

Reputation: 23029

Spring MVC Handler Interceptor does not run

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 :

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

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.

I am using Spring 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?

Upvotes: 4

Views: 15957

Answers (3)

Aniket Thakur
Aniket Thakur

Reputation: 68905

Providing additional explanation as to why this method worked for libik. As he has mentioned his controller looks like -

@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.

<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:interceptors> 
<bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterc‌​eptor" /> 
</mvc:interceptors>

Upvotes: 1

Richard Xue
Richard Xue

Reputation: 307

I solved this problem by changing the value of mvc:mapping. My working configuration is:

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

Upvotes: 1

libik
libik

Reputation: 23029

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 :

<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.

Upvotes: 4

Related Questions