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

posted @ 2020-04-02 15:00  Rolay  阅读(706)  评论(0编辑  收藏  举报