增加CorsConfig处理跨域

This commit is contained in:
2025-05-09 13:55:03 +09:00
parent 94308202e9
commit 203fc9866d

View File

@ -0,0 +1,18 @@
package co.jp.app.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**") // 允许 /api/ 下的所有请求
.allowedOrigins("http://192.168.1.50:5173") // 允许来自该域的请求
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的 HTTP 方法
.allowedHeaders("*") // 允许所有头部
.allowCredentials(true); // 允许发送 Cookie
}
}