环境: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写对了也没起作用。二者留其一即可,看个人喜好。
本文讲述了在SpringBoot2.3.5、Freemarker和Shiro1.7.0环境下,遇到静态资源访问404问题的解决过程。问题起因是Shiro未设置静态资源白名单导致302跳转,然后误配置WebMvcConfig类中的addResourceHandlers方法,使得静态资源路径配置错误。修正方法是正确配置静态资源路径,并调整WebMvcConfig中的资源处理路径。总结强调了正确配置静态资源路径的重要性。

被折叠的 条评论
为什么被折叠?



