You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring 6.0.0升级后system路径CSS/JS资源无法加载的配置求助

解决Spring 6.0.0下system路径静态资源无法加载的问题

针对你遇到的问题,下面是几个可行的配置调整方案:

1. 修正静态资源映射配置

你之前的配置中,addResourceLocations指向的路径和实际资源存放位置不匹配。如果静态资源实际存放在src/main/resources/system/assets/目录下,需要调整映射配置的资源位置:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 匹配请求路径/system/assets/**,映射到classpath下的system/assets/目录
        registry.addResourceHandler("/system/assets/**")
                .addResourceLocations("classpath:/system/assets/");
    }
}

如果是传统WAR项目,资源放在webapp/system/assets/下,资源位置要改成:

.addResourceLocations("/system/assets/");

2. 检查Spring Security拦截规则(如果使用)

Spring 6默认使用PathPatternParser作为路径匹配器,若你的项目配置了Spring Security,需要确保/system/assets/**路径被放行,避免被拦截:

@Configuration
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(auth -> auth
                // 放行静态资源路径
                .requestMatchers("/system/assets/**").permitAll()
                // 其他权限配置
                .anyRequest().authenticated()
        );
        return http.build();
    }
}

3. 全局配置静态资源位置(Spring Boot项目)

如果希望让Spring Boot自动识别system/assets/目录作为静态资源根目录,可以在配置文件中添加:

application.properties

spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/system/assets/

application.yml

spring:
  web:
    resources:
      static-locations:
        - classpath:/META-INF/resources/
        - classpath:/resources/
        - classpath:/static/
        - classpath:/public/
        - classpath:/system/assets/

4. 验证路径匹配规则

Spring 6对路径匹配的严格性有所提升,确保请求路径和映射路径完全匹配,比如避免路径末尾缺少斜杠的问题,同时确认资源文件名(包括大小写)和请求路径一致,部分操作系统对文件名大小写敏感。

内容的提问来源于stack exchange,提问作者Anushka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 09:07:27