Blame view

Antis.Erp.Plat/SERVICES_CONFIG.md 9.68 KB
f946e9dd   hexiaodong   hhh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  # 🚀 ERP 系统服务启动和 CORS 配置文档
  
  ## 📋 系统组件概览
  
  | 服务 | 端口 | 框架 | 状态 |
  |------|------|------|------|
  | 后端 API | 2011 | ASP.NET Core 6.0 | ✅ |
  | 前端管理 | 3001 | Vue 3 + Vite | ✅ |
  | 收银台 | 8888 | Python HTTP Server | ✅ |
  
  ---
  
  ## 🎯 快速启动
  
  ### 一键启动脚本(Mac/Linux)
  
  ```bash
  #!/bin/bash
  
  # 创建脚本文件: start-all.sh
  cat > start-all.sh << 'SCRIPT'
  #!/bin/bash
  
  # 启动后端 (2011)
  echo "启动后端 API..."
  cd netcore
  dotnet run --urls="http://localhost:2011" --project src/Application/NCC.API &
  BACKEND_PID=$!
  
  # 等待后端启动
  sleep 10
  
  # 启动前端 (3001)
  echo "启动前端..."
  cd ../antis-ncc-admin
  npm run dev -- --port 3001 &
  FRONTEND_PID=$!
  
  echo ""
  echo "✅ 后端 API: http://localhost:2011"
  echo "✅ 前端管理: http://localhost:3001"
  echo "✅ 收银台: http://localhost:8888"
  echo ""
  echo "后端 PID: $BACKEND_PID"
  echo "前端 PID: $FRONTEND_PID"
  echo ""
  echo "停止服务: kill $BACKEND_PID $FRONTEND_PID"
  SCRIPT
  
  chmod +x start-all.sh
  ```
  
  ### 分别启动
  
  #### 后端启动 (2011)
  ```bash
  cd /Users/hexiaodong/Desktop/git/erp2025/Antis.Erp.Plat/netcore
  dotnet run --urls="http://localhost:2011" --project src/Application/NCC.API
  ```
  
  #### 前端启动 (3001)
  ```bash
  cd /Users/hexiaodong/Desktop/git/erp2025/Antis.Erp.Plat/antis-ncc-admin
  npm run dev -- --port 3001
  ```
  
  #### 收银台启动 (8888)
  ```bash
  cd /Users/hexiaodong/Desktop/git/erp2025/Antis.Erp.Plat/sy
  python3 -m http.server 8888
  ```
  
  ---
  
  ## 🔒 CORS 配置详解
  
  ### 后端 CORS 配置
  
  **文件路径**: `netcore/src/Application/NCC.API/appsettings.json`
  
  ```json
  {
    "CorsAccessorSettings": {
      "PolicyName": "NCCCorsAccessor",
      "WithOrigins": [
        "http://192.168.0.138:8080",
        "http://localhost:8080",
        "http://localhost:3021",
        "http://localhost:3014",
        "http://localhost:3015",
2356540b   “wangming”   对成本进行了全逻辑的修改和添加
91
        "http://localhost:2011",
f946e9dd   hexiaodong   hhh
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
        "http://localhost:3009",
        "http://localhost:2016",
        "http://localhost:3000",
        "http://localhost:9528",
        "http://localhost:8200",
        "http://localhost:3001",      // ← 前端访问源
        "http://localhost:8080"
      ],
      "WithExposedHeaders": [
        "access-token",
        "x-access-token",
        "Content-Disposition"
      ]
    }
  }
  ```
  
  ### CORS 中间件配置
  
  **文件路径**: `netcore/src/Application/NCC.API.Core/Startup.cs`
  
  #### ConfigureServices 方法
  ```csharp
  public void ConfigureServices(IServiceCollection services)
  {
      // ... 其他配置 ...
      
      // 第 54 行:添加 CORS 服务
      services.AddCorsAccessor();
      
      // ... 其他配置 ...
  }
  ```
  
  #### Configure 方法
  ```csharp
  public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ...)
  {
      // ... 其他中间件 ...
      
      app.UseRouting();
      
      // 第 197 行:应用 CORS 中间件
      app.UseCorsAccessor();
      
      // 第 199 行:JWT 认证
      app.UseAuthentication();
      
      // 第 200 行:授权检查
      app.UseAuthorization();
      
      // ... 其他中间件 ...
      
      app.UseEndpoints(endpoints => { /* ... */ });
  }
  ```
  
  ### CORS 配置类
  
  **文件路径**: `netcore/src/Infrastructure/NCC/CorsAccessor/Options/CorsAccessorSettingsOptions.cs`
  
  ```csharp
  public sealed class CorsAccessorSettingsOptions : IConfigurableOptions<CorsAccessorSettingsOptions>
  {
      /// <summary>
      /// 策略名称
      /// </summary>
      public string PolicyName { get; set; }
  
      /// <summary>
      /// 允许来源域名,没有配置则允许所有来源
      /// </summary>
      public string[] WithOrigins { get; set; }
  
      /// <summary>
      /// 请求表头,没有配置则允许所有表头
      /// </summary>
      public string[] WithHeaders { get; set; }
  
      /// <summary>
      /// 响应标头
      /// </summary>
      public string[] WithExposedHeaders { get; set; }
  
      /// <summary>
      /// 设置跨域允许请求谓词,没有配置则允许所有
      /// </summary>
      public string[] WithMethods { get; set; }
  
      /// <summary>
      /// 跨域请求中的凭据
      /// </summary>
      public bool? AllowCredentials { get; set; }
  
      /// <summary>
      /// 设置预检过期时间
      /// </summary>
      public int? SetPreflightMaxAge { get; set; }
  }
  ```
  
  ---
  
  ## 🌐 跨域请求流程
  
  ### 简单请求流程(GET 等)
  
  ```
  前端 (3001)
  
     Origin: http://localhost:3001
  
  后端 (2011)
  
     检查 Origin 在 WithOrigins 中
  
     返回 Access-Control-Allow-Origin: http://localhost:3001
  
  浏览器允许前端接收响应 ✓
  ```
  
  ### 复杂请求流程(POST/PUT/DELETE + 自定义头)
  
  ```
  前端 (3001)
  
     发起 POST 请求
  
  浏览器自动发送 OPTIONS 预检请求
     ├─ Origin: http://localhost:3001
     ├─ Access-Control-Request-Method: POST
     └─ Access-Control-Request-Headers: content-type
  
  后端 (2011) CORS 中间件处理
     ├─ 检查 Origin 在白名单中 ✓
     ├─ 检查方法被允许 ✓
     ├─ 检查头被允许 ✓
  
     返回预检响应
     ├─ Access-Control-Allow-Origin: http://localhost:3001
     ├─ Access-Control-Allow-Methods: POST, PUT, DELETE, ...
     ├─ Access-Control-Allow-Headers: content-type, ...
     └─ Access-Control-Allow-Credentials: true
  
  浏览器预检通过 ✓
  
  浏览器发送真实 POST 请求
  
  后端处理并返回数据 ✓
  
  前端接收数据 ✓
  ```
  
  ---
  
  ## 🔧 API 端点验证
  
  ### 测试后端连接
  
  ```bash
  # 基本连接测试
  curl -I http://localhost:2011
  
  # 预期响应: HTTP 200
  
  # API 文档
  curl http://localhost:2011/antis.doc
  ```
  
  ### 测试 CORS 预检请求
  
  ```bash
  curl -X OPTIONS http://localhost:2011/api/some-endpoint \
    -H "Origin: http://localhost:3001" \
    -H "Access-Control-Request-Method: POST" \
    -H "Access-Control-Request-Headers: content-type" \
    -v
  ```
  
  预期响应头:
  ```
  Access-Control-Allow-Origin: http://localhost:3001
  Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
  Access-Control-Allow-Headers: content-type
  Access-Control-Allow-Credentials: true
  ```
  
  ### 前端 API 调用示例
  
  ```javascript
  // axios 配置
  import axios from 'axios'
  
  const request = axios.create({
    baseURL: 'http://localhost:2011',
    timeout: 10000,
    withCredentials: true  // 允许跨域凭据
  })
  
  // 请求拦截器
  request.interceptors.request.use(
    config => {
      config.headers['Authorization'] = `Bearer ${token}`
      return config
    }
  )
  
  // 调用 API
  request.get('/api/some-endpoint')
    .then(res => console.log(res))
    .catch(err => console.error(err))
  ```
  
  ---
  
  ## ⚠️ 常见 CORS 错误排查
  
  ### 错误 1:Access to XMLHttpRequest blocked by CORS policy
  
  **原因**: Origin 不在白名单中
  
  **解决方案**:
  ```json
  // 在 appsettings.json 中添加
  "WithOrigins": [
    "http://localhost:3001"  // ← 确保包含您的前端地址
  ]
  ```
  
  ### 错误 2:The value of the 'Access-Control-Allow-Credentials' header
  
  **原因**: AllowCredentials 为 true,但 AllowAnyOrigin 被设置
  
  **解决方案**:
  ```csharp
  // 不能同时设置
  builder.AllowAnyOrigin().AllowCredentials();  // ❌ 错误
  
  // 应该明确指定来源
  builder.WithOrigins("http://localhost:3001").AllowCredentials();  // ✅ 正确
  ```
  
  ### 错误 3:Response header 'access-token' is not allowed
  
  **原因**: 响应头未在白名单中
  
  **解决方案**:
  ```json
  "WithExposedHeaders": [
    "access-token",
    "x-access-token",
    "Content-Disposition"
  ]
  ```
  
  ---
  
  ## 📊 CORS 配置清单
  
  - [x] CORS 中间件在 ConfigureServices 中注册
  - [x] CORS 中间件在 Configure 中应用
  - [x] 前端源 (http://localhost:3001) 在白名单中
  - [x] 允许 OPTIONS 请求
  - [x] 允许所有 HTTP 方法
  - [x] 允许自定义请求头
  - [x] 暴露必要的响应头
  - [x] 允许认证凭据 (Credentials)
  - [x] 预检请求正确处理
  
  ---
  
  ## 🔐 安全建议
  
  ### 生产环境配置
  
  ```json
  {
    "CorsAccessorSettings": {
      "PolicyName": "ProductionCors",
      "WithOrigins": [
        "https://yourdomain.com",      // 只允许生产环境域名
        "https://admin.yourdomain.com"
      ],
      "AllowCredentials": true,
      "SetPreflightMaxAge": 3600
    }
  }
  ```
  
  ### JWT 令牌管理
  
  ```json
  {
    "JWTSettings": {
      "ValidateIssuerSigningKey": true,
      "IssuerSigningKey": "your-secure-key",
      "ValidateIssuer": true,
      "ValidIssuer": "your-issuer",
      "ValidateAudience": true,
      "ValidAudience": "your-audience",
      "ValidateLifetime": true,
      "ExpiredTime": 1440,
      "ClockSkew": 5
    }
  }
  ```
  
  ---
  
  ## 📞 故障诊断
  
  ### 步骤 1: 检查服务状态
  
  ```bash
  # 检查后端
  curl -I http://localhost:2011
  # 期望: HTTP 200
  
  # 检查前端
  curl -I http://localhost:3001
  # 期望: HTTP 200
  ```
  
  ### 步骤 2: 查看浏览器控制台
  
  1. 打开 F12 开发者工具
  2. 切换到 **Network** 标签
  3. 刷新页面
  4. 查找 **OPTIONS** 请求(预检请求)
  5. 检查 **Response Headers**
     - `Access-Control-Allow-Origin`
     - `Access-Control-Allow-Methods`
     - `Access-Control-Allow-Headers`
  
  ### 步骤 3: 查看后端日志
  
  ```bash
  # netcore 会在控制台输出
  # 【时间】...
  # 【等级】...
  # 【消息】...
  
  # 查找与 CORS 相关的错误
  ```
  
  ### 步骤 4: 使用 curl 测试
  
  ```bash
  # 模拟前端发送的预检请求
  curl -X OPTIONS http://localhost:2011/api/endpoint \
    -H "Origin: http://localhost:3001" \
    -H "Access-Control-Request-Method: POST" \
    -v
  
  # 检查响应头中是否包含 Access-Control-* 字段
  ```
  
  ---
  
  ## 🎯 总结
  
  **配置文件**: `netcore/src/Application/NCC.API/appsettings.json`  
  **中间件文件**: `netcore/src/Application/NCC.API.Core/Startup.cs`  
  **CORS 策略**: `NCCCorsAccessor`  
  **前端源**: `http://localhost:3001` ✓ (已在白名单中)  
  **API 端口**: `2011`  
  **跨域状态**: 已配置 ✓
  
  ---
  
  **最后更新**: 2025-01-01  
  **维护者**: ERP 开发团队