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

Spring Boot+Vaadin生产环境图标加载失败(405错误)求助

解决方案

1. 修正Service Worker缓存策略

移至frontend目录的sw.ts可能对自定义图标路径的缓存规则处理异常,导致请求被错误拦截。可针对性调整缓存逻辑:

  • 若需缓存图标,添加专属缓存规则:
workbox.routing.registerRoute(
  ({url}) => url.pathname.startsWith('/icons/'),
  new workbox.strategies.CacheFirst({
    cacheName: 'icons-cache',
    plugins: [
      new workbox.cacheableResponse.CacheableResponsePlugin({
        statuses: [200]
      })
    ]
  })
);
  • 若无需缓存,直接让请求走网络:
workbox.routing.registerRoute(
  ({url}) => url.pathname.startsWith('/icons/'),
  new workbox.strategies.NetworkOnly()
);

2. 明确Spring Security的GET方法权限

即使已开放图标路径,需确保明确允许GET请求:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers(HttpMethod.GET, "/icons/**").permitAll()
        // 其他原有配置...
}

同时排查自定义Filter或CSRF配置,避免误拦截图标路径的GET请求。

3. 清理浏览器Service Worker缓存

新旧Service Worker规则残留会导致加载异常,按以下步骤清理:

  • Chrome/Edge:打开开发者工具 → Application → Service Workers → 找到对应域名的Service Worker,点击Unregister后刷新页面。
  • 开发环境可添加强制更新逻辑到sw.ts,确保新规则立即生效:
self.addEventListener('install', (event) => {
  self.skipWaiting();
});

self.addEventListener('activate', (event) => {
  event.waitUntil(self.clients.claim());
});

4. 验证资源路径与静态资源映射

  • 直接在浏览器地址栏访问/icons/[图标文件名],确认返回200状态码,排查路径是否正确。
  • 若存在自定义WebMvcConfigurer,检查addResourceHandlers配置,确保未错误覆盖/icons/路径的映射。

5. 配置Vaadin识别静态资源目录

在application.properties中添加配置,让Vaadin明确处理图标目录:

vaadin.static.resources=classpath:/META-INF/resources/icons/

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 21:45:04