feat:增加前端cors配置

This commit is contained in:
2025-08-02 10:50:25 +08:00
parent e2c1307003
commit a11489c5b2
3 changed files with 19 additions and 6 deletions
@@ -1,17 +1,22 @@
package com.guo.learningprogresstracker.config;
import org.springframework.beans.factory.annotation.Value;
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 WebCorsConfig implements WebMvcConfigurer {
@Value("${cors.allowed-origins}")
private String[] allowedOrigins; // 支持多个Origin配置
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 配置所有路径
.allowedOrigins("http://localhost:5173")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
registry.addMapping("/**")
.allowedOrigins(allowedOrigins)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
}
}