springboot 访问静态资源失效

环境: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写对了也没起作用。二者留其一即可,看个人喜好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值