python
config_error
ai_generated
true
运行时错误:CORS:不能将allow_credentials与allow_origins='*'一起使用
RuntimeError: CORS: Cannot use allow_credentials with allow_origins='*'
ID: python/fastapi-cors-credentials-error
80%修复率
84%置信度
0证据数
2024-09-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
FastAPI CORS配置使用了allow_credentials=True和allow_origins=['*'],CORS规范不允许这样做。
English
FastAPI CORS configuration uses allow_credentials=True with allow_origins=['*'], which is not allowed by the CORS specification.
解决方案
-
95% 成功率 Specify explicit origins instead of wildcard
app.add_middleware( CORSMiddleware, allow_origins=["https://example.com"], allow_credentials=True, ) -
85% 成功率 Remove allow_credentials if wildcard is needed
app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=False, )
无效尝试
常见但无效的做法:
-
Setting allow_credentials to False without changing origins
60% 失败
May break functionality that requires credentials.
-
Ignoring the error and deploying anyway
95% 失败
CORS errors will occur in the browser.