Ross Drew
Ross Drew

Reputation: 8246

Can Controllers specify which Interceptors to use

I'm moving an existing (3.1.1) web mvc Controller (called LoginController) to using annotations, I had

<bean id="loginHandlerMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="licenseInterceptor" />
            <ref bean="propertyInterceptor" />
            <ref bean="localeChangeInterceptor" />
        </list>
    </property>
    <property name="urlMap">
        <map>
        <!-- used to include references to my LoginController -->
            <entry key="error" value-ref="error" />
        </map>
    </property>
    <property name="order">
        <value>1</value>
    </property>
</bean>

I changed my LoginController to be annotated. Some other classes had also been annotated previously so it will use the existing...

<bean id="requestMappingHandlerMapping"
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="licenseInterceptor" />
            <ref bean="loginInterceptor" />
            <ref bean="propertyInterceptor" />
        </list>
    </property>
</bean>

LoginController cannot use the loginInterceptor that others use however as it's a pre-login Controller but a post-login Interceptor.

What I want to know, is there a way to tell Spring that this specific Controller should NOT be used with a specific (loginInterceptor) Interceptor? And perhaps if it (and only it) could also use localeChangeInterceptor.

What have I tried

Upvotes: 2

Views: 5712

Answers (3)

Ross Drew
Ross Drew

Reputation: 8246

Here is what it took to get this working in the LoginController. A little like the solution of @blank but with some other nonsence, I'd still like to have a spring (annotation or config) way of fixing this though

public final boolean preHandle(HttpServletRequest request)
{
    if (handler instanceof HandlerMethod &&
      ((HandlerMethod)handler).getBean() instanceof LoginController)
    {
      return true;
    }
    ...
}

Upvotes: 0

marco.eig
marco.eig

Reputation: 4239

using the spring mvcnamespace you could do the following:

<mvc:interceptors>
  <mvc:interceptor>
    <mvc:mapping path="/**"/>
    <mvc:exclude-mapping path="/login"/>
    <ref bean="loginInterceptor"/>
  </mvc:interceptor>
  <!-- .. further interceptors -->
</mvc:interceptors>

this allows to add paths that should not be intercepted by a specific interceptor.

add the mvc namespaces to your configuration root element..

xmlns:mvc="http://www.springframework.org/schema/mvc"

... and the schema ...

xsi:schemaLocation=" .....
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
...."

Upvotes: 2

blank
blank

Reputation: 18170

I've done this in the past by implementing it in the preHandle method in a HandlerInterceptorAdapter.

@Override
public final boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
   // inspect handler object to see if it's LoginController
}

Upvotes: 1

Related Questions