Browse Source

allowedOriginPatterns

master
han\hanst 1 week ago
parent
commit
47c75f3487
  1. 25
      src/main/java/com/xujie/sys/config/CorsConfig.java

25
src/main/java/com/xujie/sys/config/CorsConfig.java

@ -6,16 +6,31 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* CORS 跨域资源共享配置
*
* <p><b>功能说明</b></p>
* <ul>
* <li>允许前端跨域访问后端API</li>
* <li>支持所有来源的跨域请求</li>
* <li>支持常用HTTP方法和自定义请求头</li>
* </ul>
*
* @author System
* @since 2026-01-06
*/
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(false)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")// 允许的请求头
.maxAge(3600);
// Spring Boot 3.x 使用 allowedOriginPatterns 替代 allowedOrigins
.allowedOriginPatterns("*")
.allowCredentials(true) // 允许携带凭证如Cookie
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
.allowedHeaders("*") // 允许所有请求头
.exposedHeaders("*") // 暴露所有响应头
.maxAge(3600); // 预检请求缓存时间
}
}
Loading…
Cancel
Save