45

Is it possible to apply an interceptor to all controllers and actions, except some that are defined?

Just to be clear, I am not interested in applying an interceptor on a list of defined ones. I want to define those to exclude.

Thanks!

mjs
  • 21,431
  • 31
  • 118
  • 200

3 Answers3

67

Since Spring 3.2 they added that feature with the tag

mvc:exclude-mapping

See this example from the Spring documentation:

<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<mvc:interceptor>
    <mvc:mapping path="/**"/>
    <mvc:exclude-mapping path="/admin/**"/>
    <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
    <mvc:mapping path="/secure/*"/>
    <bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>

Here's the link to the doc

gamerkore
  • 1,105
  • 1
  • 11
  • 16
  • 2
    Make sure the xsd points to 3.2. I spent ten minutes trying to figure out what is wrong. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> – RuntimeException Nov 09 '14 at 21:35
25

For java based configuration, from the docs

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleInterceptor());
        registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");
        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
    }

}
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
  • 1
    Where the XML equivalent is presented as: `... ...` - just for others to know that _SecurityInterceptor_ is not a springframework class. – Adrian May 27 '18 at 13:52
3

When configuring an interceptor, you can specify a path pattern. The interceptor will be invoked only for controllers which the path matches the interceptor path pattern.

ref: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-interceptor

But as you probably noticed it, the path pattern doesn't support exclusion.

So I think the only way is to code a blacklist of paths inside the interceptor. When the interceptor is invoked, retrieve the HttpServletRequest.getRequestURI() and check if the path is blacklisted or not.

You can build the blacklist inside a @PostConstruct annotated method of the interceptor, and so get the blacklisted path from a property file for instance.

tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • Yes, the question is what the proper way would be to determine the controller and action since the controller mappings can theoretically be build up in various ways... spring knows what the controller is to be fired after the interceptor, since the handler object is the controller instance. To properly do this one would have to look it up using the same mechanism that spring does to determine the target action... any one?? :) – mjs Mar 29 '12 at 14:16
  • The mechanism used by Spring to determine the right controller rely on the `@RequestMapping` annotation. So to have the same thing for interceptors mean basically annotating an interceptor with `@RequestMapping`. But this is not possible currently. – tbruyelle Mar 29 '12 at 17:33