安全最佳實踐
1. 保護您的 API 密鑰
❌ 禁止:
- 在代碼中硬編碼密鑰
- 將密鑰提交到版本控制系統
- 以明文方式分享密鑰
✅ 推薦:
- 將密鑰存儲在環境變量中
- 使用密鑰管理工具 (AWS Secrets Manager、HashiCorp Vault 等)
- 定期輪換密鑰
java
// ❌ 錯誤
private static final String API_SECRET = "MpkvVlXvYTZVPPJ0LReNA-g_yC7V7foe4UAj7tgWJSM";
// ✅ 正確
private static final String API_SECRET = System.getenv("STABLEPAY_API_SECRET");2. 使用 HTTPS
生產環境必須使用 HTTPS:
java
// ❌ 錯誤 (僅用於測試)
private static final String BASE_URL = "http://api.paystablecoin.global";
// ✅ 正確 (生產環境)
private static final String BASE_URL = "https://api.paystablecoin.global";3. 實現重試邏輯
處理臨時網絡故障:
java
int maxRetries = 3;
for (int i = 0; i < maxRetries; i++) {
try {
HttpResponse<String> response = httpClient.send(request, ...);
return response;
} catch (IOException e) {
if (i == maxRetries - 1) {
throw e;
}
Thread.sleep(1000 * (i + 1)); // 指數退避
}
}4. 記錄簽名詳情 (僅限開發環境)
在開發環境中,記錄簽名組成部分用於調試:
javascript
console.log('=== 調試信息 ===');
console.log('時間戳:', timestamp);
console.log('方法:', method);
console.log('路徑:', path);
console.log('請求體哈希:', bodyHash);
console.log('簽名字符串:', stringToSign.replace(/\n/g, ' | '));
console.log('簽名:', signature);⚠️ 生產環境禁止記錄敏感數據 (API 密鑰、簽名)。