package com.svcman.config; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.ErrorPageRegistrar; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * Web 配置:CORS 支持 + SPA 路由转发 */ @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*"); } /** * SPA 前端路由支持: * 所有非 /api 且非静态资源的 404 请求,回退到 index.html 由前端路由处理 */ @Bean public ErrorPageRegistrar spaErrorPageRegistrar() { return registry -> registry.addErrorPages( new ErrorPage(HttpStatus.NOT_FOUND, "/index.html") ); } }