环境:spring boot 2.3.5 + freemarker + shiro 1.7.0
开发工具:idea 社区版。
现象:在成功解决因为shiro未设置白名单,静态资源访问时出现302跳转的问题之后,再次访问静态资源文件出现404错误。页面代码:<link rel="stylesheet" href="${re.contextPath}/css/login.css">。按说spring boot 默认的静态目录就是static,应该能找得到。在yml中配置了
mvc: static-path-pattern: /** resources: static-locations: - classpath:/static/
未起作用。后来发现之前从别的代码里抄过来一个WebMvcConfig文件,继承了WebMvcConfigurationSupport类,而重写的addResourceHandlers方法是这样的:
@Configuration public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
registry.addResourceHandler("/plugin/**", "/static/**")
.addResourceLocations("classpath:/plugin/", "classpath:/static/");
super.addResourceHandlers(registry);
}
}
把上文的斜体加粗部分改与 "/**",静态资源立刻可以显示。
总结:因为在网上看了好多文章,static-path-parttern这里一直设置成了/static/**,这样是错误的。后来改对了,又因为写了一个错误的配置类,造成yml写对了也没起作用。二者留其一即可,看个人喜好。