Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring HandlerInterceptor mapping with annotations

Good day. I got a spring mvc application and 2 controllers inside. First controller (PublicController) can process requests from all users, Second (PrivateController) can only process authorized users.

So I implemented two Handler Interceptor`s

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="webapp.base.package")
public class WebApplicationConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoggerInterceptor());
        registry.addInterceptor(new AccessInterceptor());
    }

}

I need my LoggerInterceptor to handle all controller's requests, and my AccessInterceptor to handle only PrivateController's requests. I must map Interceptors to Controllers with annotations

like image 205
user2160696 Avatar asked May 23 '13 06:05

user2160696


2 Answers

Just solve it.

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="webapp.base.package")
public class WebApplicationConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoggerInterceptor()).addPathPatterns("/**");;
        registry.addInterceptor(new AccessInterceptor()).addPathPatterns("/private/**");;
    }

}
like image 104
user2160696 Avatar answered Nov 20 '22 05:11

user2160696


I don't know why, when I use your way, it worked. But the interceptor executed twice. And I found another way to do this.

    @Bean
    public MappedInterceptor interceptor() {
        return new MappedInterceptor(null, new String[]{"/","/**/*.js", "/**/*.html", "/**/*.css"}, new LogInterceptor());
    }
like image 2
craig_wu9 Avatar answered Nov 20 '22 05:11

craig_wu9