From 92b969743046328ce1e54caafb3573e7d3f9d37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=84=AD=E6=B2=9B=E8=BB=92?= Date: Thu, 9 Oct 2025 22:16:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E6=88=90=E5=89=8D=E7=AB=AF=20U?= =?UTF-8?q?RL=20=E9=85=8D=E7=BD=AE=E7=B5=B1=E4=B8=80=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修復 API 配置預設端口從 5000 改為 5008 - 重構 Profile 頁面使用統一的 API_URLS 配置 - 更新測試檔案的端口號 - 新增系統上線前測試代碼清理計劃文檔 完成前端 URL 配置統一管理,所有 API 調用現在都使用正確的端口和統一配置。 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- frontend/app/profile/page.tsx | 9 +- frontend/lib/config/api.ts | 2 +- frontend/lib/services/imageGeneration.ts | 5 +- frontend/public/test-register.html | 2 +- 系統上線前測試代碼清理計劃.md | 109 +++++++++++++++++++++++ 5 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 系統上線前測試代碼清理計劃.md diff --git a/frontend/app/profile/page.tsx b/frontend/app/profile/page.tsx index 356daa5..df5a41b 100644 --- a/frontend/app/profile/page.tsx +++ b/frontend/app/profile/page.tsx @@ -1,6 +1,7 @@ 'use client' import { useState, useEffect } from 'react' +import { API_URLS } from '@/lib/config/api' import { Navigation } from '@/components/shared/Navigation' import { useAuth } from '@/contexts/AuthContext' @@ -117,13 +118,13 @@ export default function ProfilePage() { // 並行載入所有資料 const [profileResponse, settingsResponse] = await Promise.all([ - fetch('http://localhost:5000/api/auth/profile', { + fetch(API_URLS.auth('/profile'), { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` } }), - fetch('http://localhost:5000/api/auth/settings', { + fetch(API_URLS.auth('/settings'), { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` @@ -189,7 +190,7 @@ export default function ProfilePage() { return } - const response = await fetch('http://localhost:5000/api/auth/profile', { + const response = await fetch(API_URLS.auth('/profile'), { method: 'PUT', headers: { 'Content-Type': 'application/json', @@ -224,7 +225,7 @@ export default function ProfilePage() { return } - const response = await fetch('http://localhost:5000/api/auth/settings', { + const response = await fetch(API_URLS.auth('/settings'), { method: 'PUT', headers: { 'Content-Type': 'application/json', diff --git a/frontend/lib/config/api.ts b/frontend/lib/config/api.ts index 7df84e1..845be94 100644 --- a/frontend/lib/config/api.ts +++ b/frontend/lib/config/api.ts @@ -5,7 +5,7 @@ // API基礎配置 export const API_CONFIG = { - BASE_URL: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5000', + BASE_URL: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5008', ENDPOINTS: { AUTH: '/api/auth', FLASHCARDS: '/api', diff --git a/frontend/lib/services/imageGeneration.ts b/frontend/lib/services/imageGeneration.ts index fbe4423..9a6e8bc 100644 --- a/frontend/lib/services/imageGeneration.ts +++ b/frontend/lib/services/imageGeneration.ts @@ -66,13 +66,12 @@ class ImageGenerationService { private baseUrl = BASE_URL private async makeRequest(url: string, options: RequestInit = {}): Promise> { - const token = localStorage.getItem('token') + const token = localStorage.getItem('auth_token') const response = await fetch(`${this.baseUrl}${url}`, { headers: { 'Content-Type': 'application/json', - // 開發階段:不發送無效的token,讓後端使用測試用戶 - // 'Authorization': token ? `Bearer ${token}` : '', + 'Authorization': token ? `Bearer ${token}` : '', ...options.headers, }, ...options, diff --git a/frontend/public/test-register.html b/frontend/public/test-register.html index efeeaf9..b945d4c 100644 --- a/frontend/public/test-register.html +++ b/frontend/public/test-register.html @@ -36,7 +36,7 @@ console.log('Sending data:', data); try { - const response = await fetch('http://localhost:5000/api/auth/register', { + const response = await fetch('http://localhost:5008/api/auth/register', { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/系統上線前測試代碼清理計劃.md b/系統上線前測試代碼清理計劃.md new file mode 100644 index 0000000..9dcf0ec --- /dev/null +++ b/系統上線前測試代碼清理計劃.md @@ -0,0 +1,109 @@ +# 系統上線前測試代碼清理計劃 + +## 🎯 目標 +清理所有開發測試代碼,確保系統安全上線,特別是解決圖片生成功能的用戶認證問題。 + +## 🚨 **高優先級 - 必須修復** + +### 1. 固定測試用戶 ID 問題 ⭐ **最重要** +**影響**:🔴 **阻止圖片生成功能運行** + +**位置**: +- `Controllers/BaseController.cs:79` +- `Controllers/ImageGenerationController.cs:160` + +**問題**: +```csharp +// 當前代碼 +return Guid.Parse("00000000-0000-0000-0000-000000000001"); + +// 應該修復為 +var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value + ?? User.FindFirst("sub")?.Value; +return Guid.Parse(userIdClaim); +``` + +**後果**: +- 圖片生成使用錯誤的用戶 ID +- 外鍵約束失敗,功能完全無法使用 + +### 2. 開發環境認證繞過 +**影響**:🟠 **安全風險** + +**位置**: +- `Controllers/FlashcardsController.cs:16` - `[AllowAnonymous] // 暫時開放以測試` +- `Controllers/ImageGenerationController.cs:10` - `[AllowAnonymous] // 暫時移除認證要求` +- `Controllers/AIController.cs:10` - `[AllowAnonymous]` + +**修復**:移除 `[AllowAnonymous]` 或改為 `[Authorize]` + +### 3. 開發專用 JWT Secret +**影響**:🟠 **安全風險** + +**位置**: +- `Program.cs:93` +- `Extensions/ServiceCollectionExtensions.cs:174` +- `Controllers/AuthController.cs:181` + +**問題**: +```csharp +?? "dev-secret-minimum-32-characters-long-for-jwt-signing-in-development-mode-only" +``` + +**修復**:確保生產環境使用正確的環境變數 + +## 🟡 **中優先級 - 建議修復** + +### 4. 開發專用配置 +**位置**: +- `Program.cs:49` - `options.ApiKey = "test-key"` +- `Models/Configuration/GeminiOptionsValidator.cs:15` - 允許 `"test-key"` + +### 5. CORS 開發設定 +**位置**: +- `Program.cs:115` - localhost CORS 設定 +- `Program.cs:185` - 開發環境寬鬆 CORS + +### 6. 其他開發標記 +**位置**: +- `Controllers/ImageGenerationController.cs:134` - `TODO: 實現分頁查詢邏輯` +- `Controllers/ImageGenerationController.cs:162` - `TODO: 恢復真實認證後改回` + +## ✅ **可保留 - 正確的生產實務** + +### 7. 環境判斷邏輯 +- `if (builder.Environment.IsDevelopment())` - 正確的條件判斷 +- 環境變數讀取邏輯 +- 配置驗證邏輯 + +### 8. 測試相關檔案 +- `frontend/public/test-register.html` - 不影響生產環境 +- 各種文檔中的測試範例 +- 測試套件依賴 + +## 🔧 **關鍵發現** + +### 圖片生成失敗的根本原因 +1. **用戶認證問題**:使用固定測試 ID `00000000-0000-0000-0000-000000000001` +2. **外鍵約束失敗**:嘗試為不存在的用戶創建圖片生成請求 +3. **API keys 正常**:所有必要的 API keys 都已正確配置 + +### Google Cloud Storage 狀態 +- ✅ **認證正常**:gcloud auth 已設置 +- ✅ **配置正確**:專案和儲存桶設定完整 +- ✅ **服務初始化**:GoogleCloudImageStorageService 正常運行 +- ✅ **儲存就緒**:圖片將正確儲存到 `gs://dramaling-images/examples/` + +## 🚀 **修復順序建議** + +1. **立即修復**:用戶 ID hardcoding - 解決圖片生成問題 +2. **安全加強**:移除 AllowAnonymous 標記 +3. **配置清理**:確保生產環境配置安全 +4. **測試驗證**:完整功能測試 + +## 💡 **完成修復後的預期結果** + +- ✅ 圖片生成功能完全正常 +- ✅ 圖片正確儲存到 Google Cloud Storage +- ✅ 用戶認證安全保護 +- ✅ 系統準備好安全上線 \ No newline at end of file