Manu
Manu

Reputation: 4137

Set up an interceptor in Spring MVC

I am trying to cope with the SOP and I need to setup a filter and an interceptor, according to http://patrickgrimard.com/2013/12/12/cross-origin-resource-sharing-cors-requests-with-spring-mvc/. I setup the filter in the web.xml:

<filter>
    <filter-name>simpleCORSFilter</filter-name>
    <filter-class>base.SimpleCORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>simpleCORSFilter</filter-name>
    <servlet-name>rest</servlet-name>
</filter-mapping>

where SimpleCORSFilter is

@Component
public class SimpleCORSFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest req,
        HttpServletResponse res, FilterChain filterChain)
        throws ServletException, IOException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods",
            "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Content-Type");  
    filterChain.doFilter(req, res);
}

It works, because my test POST request is caught and manipulated. Now, I tried to config the following interceptor:

@Component
public class SimpleCORSInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    response.addHeader("Access-Control-Allow-Origin", "*");
    return true;
}
}

and I added the following configuration in the rest-servlet.xml (which works, as the REST service are correctly invoked):

<?xml version="1.0" encoding="UTF-8"?>
<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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.lh.clte.web" />
<mvc:annotation-driven />

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/*" />
        <bean class="base.SimpleCORSInterceptor"></bean>
    </mvc:interceptor>
</mvc:interceptors>

However, the overridden method is not getting called. What am I missing?

Upvotes: 0

Views: 2861

Answers (1)

a better oliver
a better oliver

Reputation: 26828

In the article the filter is intended only for requests using the OPTION method, whereas the interceptor is meant for the "real" calls. In your example all requests would be handled by both the filter and the interceptor. So you don't need the interceptor.

Moreover the interceptor is only called when there is a controller method handling the request. When you call a URL that is not mapped, then the interceptor is not called.

BTW: There's no need for @Component.

Upvotes: 2

Related Questions