Today started with SpringBoot writing projects, so let it be able to access static resources, but the configuration for a long time can not access my helloWorld page, the original, in SpringBoot2.x, the path does not take effect at the static resources.
classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/)不生效。
springboot2.x later, support jdk1.8, jdk1.8 use some of the features of. jdk1.8 support adding default method in the interface, and this method has a specific methods. Deal with static resources and interceptors, no longer inherit "WebMvcConfigurerAdapter" method. But directly to achieve "WebMvcConfigurer" interface. By default method of rewriting the interface to add additional configuration.
To be able to access static resources, configure 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 returnpermissionInterceptorAdapter;
36 }
37 [
38 is / **
39 * static resource interceptor
40 * /
41 is @Bean
42 is public WebInterceptor webInterceptor () {
43 is WebInterceptor webInterceptor = new new WebInterceptor ();
44 is return webInterceptor;
45 }
46 is
47 @Override
48 public void addInterceptors (InterceptorRegistry Registry) {
49 // encoder interceptor
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/**");
voidpublic61 is @Override
60* /59 * add static resource files, direct external access address
58/ **57 is56 is }
55
addResourceHandlers (ResourceHandlerRegistry Registry) {
62 is // first access path is provided a method of prefixes, a method is provided second resource path
63 is registry.addResourceHandler ( "/ static / **") addResourceLocations. ( "CLASSPATH: / static /" );
64 // add direct access to upload files
65 Environment env = SpringContextHolder.getBean (Environment. class );
66 String uploadFilePath = env.getProperty ( "the Upload-file-path" );
67 registry.addResourceHandler ( "/ the Upload / * File * ") addResourceLocations (.": "+ uploadFilePath);
68 }
69
70 }