首页 > 编程语言 > 详细

springboot学习(一) 拦截器

时间:2020-04-11 10:38:55      阅读:81      评论:0      收藏:0      [点我收藏+]
  1. WebMvcConfigurerAdapter拦截器在springboot2.1之后被废除,在自定义拦截器的时候需要继承WebMvcConfigurationSupport类来重写addInterceptors()方法,添加我们自定义的拦截器。
  2. 关于配置自定义连接器之后静态资源无法访问的问题

需要在继承WebMvcConfigurationSupport类中重写addResourceHandlers()方法,如下:

package com.lhb.blog.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class LoginConfig extends WebMvcConfigurationSupport {

    @Autowired
    private LoginInterceptor interceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor).addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/")
                .excludePathPatterns("/admin/login");
    }

    /**
     * 配置静态资源访问权限
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        if(!registry.hasMappingForPattern("/webjars/**")){
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/**")){
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/META-INF/resources/")
                    .addResourceLocations("classpath:/resources/")
                    .addResourceLocations("classpath:/static/")
                    .addResourceLocations("classpath:/public/");
        }
    }
}

  





springboot学习(一) 拦截器

原文:https://www.cnblogs.com/lihuibin/p/12677871.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!