本文介绍了带有注释的Spring HandlerInterceptor映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天。
我有一个弹簧mvc应用程序和2个控制器。
第一个控制器(PublicController)可以处理来自所有用户的请求,Second(PrivateController)只能处理授权用户。

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());
    }

}

我需要 LoggerInterceptor 来处理所有控制器的请求,我的 AccessInterceptor 只处理 PrivateController '请求。
我必须将拦截器映射到控制器带注释

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

推荐答案

解决它。

@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/**");;
    }

}

这篇关于带有注释的Spring HandlerInterceptor映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:34