zoukankan      html  css  js  c++  java
  • springboot2.0 使用拦截器后,导致静态文件访问不到的解决方案

     1 package com.leenleda.ward.tv.admin.interceptor;
     2 
     3 import com.leenleda.ward.tv.common.config.LeenledaConfig;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.context.annotation.Configuration;
     6 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
     7 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
     8 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
     9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    10 
    11 import javax.annotation.Resource;
    12 import java.util.Arrays;
    13 
    14 
    15 /**
    16  * @Author: pengbenlei
    17  * @Date: 2020/2/19 11:22
    18  * @Description:
    19  */
    20 @Configuration
    21 @EnableWebMvc
    22 public class CustomMVCConfiguration implements WebMvcConfigurer {
    23 
    24     @Resource
    25     LoginInterceptor loginInterceptor;
    26 
    27     @Autowired
    28     LeenledaConfig leenledaConfig;
    29     @Override
    30     public void addInterceptors(InterceptorRegistry registry) {
    31         //登录拦截器
    32         registry.addInterceptor(loginInterceptor).addPathPatterns("/admin/**")
    33                 .excludePathPatterns(Arrays.asList("/file/**"));
    34     }
    35 
    36     /**
    37      * 添加静态资源文件,外部可以直接访问地址
    38      * @param registry
    39      */
    40     @Override
    41     public void addResourceHandlers(ResourceHandlerRegistry registry) {
    42         String locationPath="file:"+leenledaConfig.getLeenledaUploadRoot()+"/leenleda/application/";
    43         registry.addResourceHandler("/file/**")
    44                 .addResourceLocations(locationPath);
    45     }
    46 
    47 }
    拦截器注册

    其中file是自定义的虚拟路径,访问时过滤掉拦截,addResourceLocations 要访问的文件路径。

    参考地址  https://www.jianshu.com/p/6469e9c37605

  • 相关阅读:
    【原创】谈一个数学教育专业的IT职业生涯
    应用程序测试方法与技巧
    Windows和MacOS的比较——不断完善和补充,欢迎吐槽
    【原创】Windows上应用程序报错常用分析方法总结
    职场感悟,发现言语背后的故事
    职场感悟,乐业感恩
    javadoc
    包机制的创建
    运算符
    变量和常量
  • 原文地址:https://www.cnblogs.com/rolayblog/p/12619932.html
Copyright © 2011-2022 走看看