diff --git a/backend/src/main/java/cn/hezhaohui/mail/config/WebConfig.java b/backend/src/main/java/cn/hezhaohui/mail/config/WebConfig.java new file mode 100644 index 0000000..4393d26 --- /dev/null +++ b/backend/src/main/java/cn/hezhaohui/mail/config/WebConfig.java @@ -0,0 +1,19 @@ +package cn.hezhaohui.mail.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 WebConfig implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") // 应用到所有路径 + .allowedOrigins("http://localhost:5173") // 允许前端开发服务器的源 + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法 + .allowedHeaders("*") // 允许所有header + .allowCredentials(true) // 允许发送凭证信息(如cookies) + .maxAge(3600); // 预检请求的有效期,单位为秒 + } +}