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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM