Spring Boot 3.x 自动配置 CORS 请求的库。
- 自动配置 CORS 过滤器,无需手动编写配置代码
- 支持通过
application.yml或application.properties灵活配置 - 使用
HIGHEST_PRECEDENCE优先级,确保 CORS 头在认证过滤器之前添加 - 开箱即用,添加依赖即可生效
<dependency>
<groupId>com.edu</groupId>
<artifactId>cors</artifactId>
<version>1.0.0</version>
</dependency>在 application.yml 中配置:
cors:
enabled: true
allowed-origins: "*"
allowed-methods: GET,POST,PUT,DELETE,OPTIONS
allowed-headers: "*"
allow-credentials: true
max-age: 3600或在 application.properties 中配置:
cors.enabled=true
cors.allowed-origins=*
cors.allowed-methods=GET,POST,PUT,DELETE,OPTIONS
cors.allowed-headers=*
cors.allow-credentials=true
cors.max-age=3600| 配置项 | 说明 | 默认值 |
|---|---|---|
cors.enabled |
是否启用 CORS 自动配置 | true |
cors.allowed-origins |
允许的来源列表,* 表示允许所有 |
[] |
cors.allowed-methods |
允许的 HTTP 方法 | GET,POST,PUT,DELETE,OPTIONS |
cors.allowed-headers |
允许的请求头,* 表示允许所有 |
["*"] |
cors.allow-credentials |
是否允许携带凭据(Cookie、Authorization 头) | true |
cors.max-age |
预检请求缓存时间(秒) | 3600 |
如需禁用 CORS 自动配置,设置 cors.enabled=false:
cors:
enabled: false或完全移除此依赖。
# 编译
mvn compile
# 安装到本地仓库
mvn install如需完全禁用 CORS,请从项目中移除此依赖。
- Java 17+
- Spring Boot 3.4.x