From c2300679ed3824502de3e3b403f44879163ddfc9 Mon Sep 17 00:00:00 2001 From: xuanzhi33 Date: Fri, 15 Nov 2024 13:21:30 +0800 Subject: [PATCH] Add CORS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用于HTML端使用 --- .../NottinghamWall/config/WebMvcConfiguration.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/backend/wall-server/src/main/java/cn/yiming1234/NottinghamWall/config/WebMvcConfiguration.java b/backend/wall-server/src/main/java/cn/yiming1234/NottinghamWall/config/WebMvcConfiguration.java index bb53452a..f5e64eb8 100644 --- a/backend/wall-server/src/main/java/cn/yiming1234/NottinghamWall/config/WebMvcConfiguration.java +++ b/backend/wall-server/src/main/java/cn/yiming1234/NottinghamWall/config/WebMvcConfiguration.java @@ -50,6 +50,17 @@ protected void addInterceptors(InterceptorRegistry registry) { .excludePathPatterns("/student/uniapp/status"); } + // 全局CORS + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("https://gh.xuanzhi33.cn") // 允许特定域名 + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法 + .allowedHeaders("*") // 允许的请求头 + .allowCredentials(false) // 禁用Cookie发送 + .maxAge(3600); // 预检请求的缓存时间 + } + /** * 通过knife4j生成接口文档 * @return