141-SpringBoot如何配置拦截器?

1.首先,我们应该写个拦截器配置类,写了这个类之后,我们就不就用在写配置文件了

@Configuration  //该注解就是用于定义某个类为配置类,该就相当于一个xml配置文件,我们将在此类在配置拦截器
public class InterceptorConfig implements WebMvcConfigurer {
    //此方法并不会强制我们实现
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //配置拦截路径
        String[] addPathPatterns = {
            "/user/**"
        };
        //排除不需要拦截的路径
        String[] excludePathPatterns = {
            "/user/login",
            "/user/index",
            "/user/show"
        };
        //指定拦截器,该方法相当于之前的mvc:interceptor标签
        registry.addInterceptor(new UserInterceptor())
                .addPathPatterns(addPathPatterns)
                .excludePathPatterns(excludePathPatterns);
    }
}

 

posted on 2020-04-22 19:29  十年磨三劍  阅读(220)  评论(0编辑  收藏  举报

导航