123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- """
- CORS配置文件
- 用于管理Flask应用的跨域资源共享设置
- """
- # 允许的源(前端地址)
- # 方案1: 允许所有源(开发环境推荐,最灵活)
- ALLOW_ALL_ORIGINS = True
- # 方案2: 允许特定网段的IP地址(生产环境推荐)
- ALLOWED_IP_RANGES = [
- "192.168.0.0/16", # 局域网IP段
- "10.0.0.0/8", # 内网IP段
- "172.16.0.0/12", # 内网IP段
- ]
- # 方案3: 允许的固定源(如果需要限制特定地址)
- ALLOWED_ORIGINS = [
- "http://localhost:5173", # Vite默认端口
- "http://localhost:3000", # React默认端口
- "http://localhost:8080", # Vue默认端口
- "http://127.0.0.1:5173",
- "http://127.0.0.1:3000",
- "http://127.0.0.1:8080",
- "http://192.168.3.218:5173", # 客户端前端地址
- "http://192.168.3.218:3000", # 客户端备用端口
- "http://192.168.3.218:8080", # 客户端备用端口
- # 生产环境地址(如果需要)
- # "https://yourdomain.com",
- ]
- # 允许的HTTP方法
- ALLOWED_METHODS = [
- "GET",
- "POST",
- "PUT",
- "DELETE",
- "OPTIONS"
- ]
- # 允许的请求头
- ALLOWED_HEADERS = [
- "Content-Type",
- "Authorization",
- "X-Requested-With",
- "Accept",
- "Origin",
- "Cache-Control",
- "X-File-Name"
- ]
- # 暴露的响应头
- EXPOSED_HEADERS = [
- "Content-Type",
- "Content-Length",
- "Content-Disposition",
- "X-Total-Count",
- "X-Content-Type-Options",
- "X-Frame-Options",
- "X-XSS-Protection"
- ]
- # CORS配置选项
- if ALLOW_ALL_ORIGINS:
- # 方案1: 允许所有源(最灵活)
- CORS_OPTIONS = {
- "resources": {r"/api/*": {"origins": "*"}},
- "supports_credentials": False, # 通配符时不能启用凭据
- "methods": ALLOWED_METHODS,
- "allow_headers": ALLOWED_HEADERS,
- "expose_headers": EXPOSED_HEADERS,
- "max_age": 86400, # 预检请求缓存时间(秒)
- "send_wildcard": True,
- "automatic_options": True
- }
- else:
- # 方案2: 使用固定源列表
- CORS_OPTIONS = {
- "resources": {r"/api/*": {"origins": ALLOWED_ORIGINS}},
- "supports_credentials": True,
- "methods": ALLOWED_METHODS,
- "allow_headers": ALLOWED_HEADERS,
- "expose_headers": EXPOSED_HEADERS,
- "max_age": 86400, # 预检请求缓存时间(秒)
- "send_wildcard": False,
- "automatic_options": True
- }
|