• springboot学习(一) 拦截器


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

      





  • 相关阅读:
    《掌握需求过程》阅读笔记(二)
    《掌握需求过程》阅读笔记(一)
    《软件方法》阅读笔记(三)
    《软件方法》阅读笔记(二)
    《软件方法》阅读笔记(一)
    《大象Think in UML》阅读笔记(三)
    Java中toArray的用法探究(java数组与list转换)
    Eclipse调试常用技巧
    ListView 总结----持续中
    PowerDesigner提示This data item is already used in a primary identifier.的处理
  • 原文地址:https://www.cnblogs.com/lihuibin/p/12677871.html
Copyright © 2020-2023  润新知