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

  • 相关阅读:
    【环境部署】centos7安装mysql-5.7.19 group-replication
    centos7远程安装oracle11g R2详细教程-解决一切问题
    docker:构建nginx+php-fpm镜像(一):构建nginx自启动镜像
    python virtualenv 安装运行saltstack
    自动化运维:flask-bootstrap + highstock整合
    自动化运维web环境搭建:Nginx+Django+uwsgi
    计算机二级-word错题总结
    SVPWM学习笔记2
    SVPWM自学笔记
    电力拖动自动控制系统_学习笔记2
  • 原文地址:https://www.cnblogs.com/rolayblog/p/12619932.html
Copyright © 2011-2022 走看看