SpringBoot2.X中的静态资源访问失效

  今天开始使用SpringBoot写项目,于是先让其能够访问静态资源,但是配置半天也不能访问我的helloWorld页面,原来,在SpringBoot2.x中,一下静态资源路径不生效了。

  classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/)不生效。

  springboot2.x以后,支持jdk1.8,运用了jdk1.8的一些特性。jdk1.8支持在接口中添加default方法,而此方法具有具体的方法实现。静态资源和拦截器的处理,不再继承“WebMvcConfigurerAdapter”方法。而是直接实现“WebMvcConfigurer”接口。通过重写接口中的default方法,来增加额外的配置。

  要想能够访问静态资源,请配置WebMvcConfigurer

 1 package io.guangsoft.web.config;
 2 
 3 import io.guangsoft.common.interceptor.EncodingInterceptor;
 4 import io.guangsoft.common.security.shiro.interceptor.PermissionInterceptorAdapter;
 5 import io.guangsoft.common.utils.SpringContextHolder;
 6 import io.guangsoft.web.interceptor.WebInterceptor;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9 import org.springframework.core.env.Environment;
10 import org.springframework.web.servlet.HandlerInterceptor;
11 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
12 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
13 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
14 
15 @Configuration
16 public class InterceptorConfig implements WebMvcConfigurer {
17 
18     /**
19      * 编码拦截器
20      */
21     @Bean
22     public HandlerInterceptor encodingInterceptor() {
23         EncodingInterceptor encodingInterceptor = new EncodingInterceptor();
24         return encodingInterceptor;
25     }
26 
27     /**
28      * 安全验证拦截器
29      *
30      * @return
31      */
32     @Bean
33     public PermissionInterceptorAdapter permissionInterceptorAdapter() {
34         PermissionInterceptorAdapter permissionInterceptorAdapter = new PermissionInterceptorAdapter();
35         return permissionInterceptorAdapter;
36     }
37 
38     /**
39      * 静态资源拦截器
40      */
41     @Bean
42     public WebInterceptor webInterceptor() {
43         WebInterceptor webInterceptor = new WebInterceptor();
44         return webInterceptor;
45     }
46 
47     @Override
48     public void addInterceptors(InterceptorRegistry registry) {
49         //编码拦截器
50         registry.addInterceptor(encodingInterceptor()).addPathPatterns("/**").excludePathPatterns("/upload/**", "/static/**");
51         //安全验证拦截器
52         registry.addInterceptor(permissionInterceptorAdapter()).addPathPatterns("/**").excludePathPatterns("/upload/**", "/static/**");
53         //web拦截器
54         registry.addInterceptor(webInterceptor()).addPathPatterns("/**").excludePathPatterns("/upload/**", "/static/**");
55     }
56 
57     /**
58      * 添加静态资源文件,外部可以直接访问地址
59      */
60     @Override
61     public void addResourceHandlers(ResourceHandlerRegistry registry) {
62         //第一个方法设置访问路径前缀,第二个方法设置资源路径
63         registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
64         //添加对上传文件的直接访问
65         Environment env = SpringContextHolder.getBean(Environment.class);
66         String uploadFilePath = env.getProperty("upload-file-path");
67         registry.addResourceHandler("/upload/**").addResourceLocations("file:" + uploadFilePath);
68     }
69 
70 }

 

上一篇:springboot2.X 搭建redis


下一篇:SpringBoot2.x整合Redis-使用Spring缓存注解操作Redis