zoukankan      html  css  js  c++  java
  • 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/");
            }
        }
    }
    

      





  • 相关阅读:
    angular 前端路由不生效解决方案
    LinqMethod 实现 LeftJoin
    Newtonsoft.Json 序列化踩坑之 IEnumerable
    Newtonsoft.Json 指定某个属性使用特定的时间格式
    [svc]Linux中Swap与Memory内存简单介绍
    [svc]Linux vmstat命令实战详解
    [svc]ansible自动化模块
    [svc]ssh+gg二步认证
    [svc][cpu][jk]cpu的核心查看及什么是cpu的负载
    [vt][xen]xenserver初始安装增加第二块硬盘&xen图形界面安装vm&设置xen里vm开机启动
  • 原文地址:https://www.cnblogs.com/lihuibin/p/12677871.html
Copyright © 2011-2022 走看看