Update application.yml to disable resource mapping for improved SPA handling

This commit is contained in:
liumangmang
2026-02-04 11:16:01 +08:00
parent 7e6ebd18a5
commit 4558ef20c0
9 changed files with 258 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
package com.sshmanager.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
import java.io.IOException;
/**
* SPA 前端路由回退:未匹配到静态资源时返回 index.html供 Vue Router history 模式使用。
*/
@Configuration
public class SpaForwardConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String path, Resource location) throws IOException {
Resource resource = location.createRelative(path);
if (resource.exists() && resource.isReadable()) {
return resource;
}
return location.createRelative("index.html");
}
});
}
}

View File

@@ -2,6 +2,9 @@ server:
port: 48080
spring:
web:
resources:
add-mappings: false # 使用 SpaForwardConfig 统一处理静态与 SPA 回退
datasource:
url: jdbc:h2:file:./data/sshmanager;DB_CLOSE_DELAY=-1
driver-class-name: org.h2.Driver