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

      





  • 相关阅读:
    在linux系统中
    记录一次编译安装Pg_rman缺少依赖包的问题
    使用Patroni和HAProxy创建高可用的PostgreSQL集群
    Zabbix4.0国内下载源
    centos7部署postgresql集群高可用 patroni + etcd 之patroni篇
    centos7部署etcd集群
    docker-compose部署gitlab
    zabbix-agent主动模式和proxy
    docker-compose更新image命令
    shell脚本自动化安装pgsql10.5版本
  • 原文地址:https://www.cnblogs.com/lihuibin/p/12677871.html
Copyright © 2011-2022 走看看