Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude Spring Request HandlerInterceptor by Path-Pattern

I know we can map different url to different interceptor, or we can map multiple url to single interceptor too. I am just curious to know if we also have exclude option. for example if I have 50 url mapping in application and except 1 mapping I want to call interceptor for all so rather than writing configuration for 49 mapping can I just mention * and one exclude to the 50th url?

like image 885
Ron Zoosk Avatar asked Jan 23 '16 23:01

Ron Zoosk


3 Answers

HandlerInterceptors can be applied or excluded to (multiple) specific url's or url-patterns.

See the MVC Interceptor Configuration.

Here are the examples from the documentation

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

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

        // multiple urls (same is possible for `exludePathPatterns`)
        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*", "/admin/**", "/profile/**");
    }
}

or using XML config

<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>
        <!-- intercept multiple urls -->
        <mvc:mapping path="/secure/*"/>
        <mvc:mapping path="/admin/**"/>
        <mvc:mapping path="/profile/**"/>
        <bean class="org.example.SecurityInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>
like image 114
fateddy Avatar answered Oct 25 '22 15:10

fateddy


In my case:

/api/v1/user-manager-service/tenants/add

there was incorrect PathPattern configuration:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new RequestInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/tenants/**");
}

I was missing:

/**

before actual path.

After correction it works as expected:

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new RequestInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/**/tenants/**");
}
like image 35
Dmitry Avatar answered Oct 25 '22 16:10

Dmitry


I think in spring-boot 2.0 version, this have changed a lot right now. Below is the implementation you can easily add and configure the path pattern.

@Component
public class ServiceRequestAppConfig implements WebMvcConfigurer {

    @Autowired
    ServiceRequestInterceptor sri;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String pathPattern = "/admin/**";
        registry.addInterceptor(sri).excludePathPatterns(pathPattern);
    }

}


@Component
public class ServiceRequestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // Your logic
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex){
         // Some useful techniques
    }

}
like image 28
Rajesh Somasundaram Avatar answered Oct 25 '22 15:10

Rajesh Somasundaram