Skip to content

Commit 971aa77

Browse files
authored
Merge pull request #141 from howWeather/perf/model-request
fix: AI 응답 복호화 전 유효성 검사 추가
2 parents 63c6dba + 336c1d3 commit 971aa77

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/main/java/com/howWeather/howWeather_backend/domain/member/service/MyAccountService.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,26 @@ private Map<String, String> encryptPredictionData(List<AiPredictionRequestDto> d
303303

304304
@Transactional
305305
public void saveRecommendationsInternal(Map<String, String> encryptedData, String newRegionName) {
306-
if (encryptedData == null || encryptedData.isEmpty()) return;
306+
if (encryptedData == null || encryptedData.isEmpty()) {
307+
log.warn("[추천 데이터 처리 중단] 전달된 데이터 맵이 null이거나 비어있습니다.");
308+
return;
309+
}
310+
311+
if (!encryptedData.containsKey("iv") || !encryptedData.containsKey("payload")) {
312+
log.warn("[추천 데이터 처리 중단] AI 응답 Map에 복호화에 필요한 'iv' 또는 'payload' 키가 없습니다. 응답: {}", encryptedData);
313+
return;
314+
}
315+
316+
String iv = encryptedData.get("iv");
317+
String payload = encryptedData.get("payload");
318+
if (iv == null || iv.isBlank() || payload == null || payload.isBlank()) {
319+
log.warn("[추천 데이터 처리 중단] AI 응답 Map의 'iv' 또는 'payload' 값이 null이거나 비어있습니다. 응답: {}", encryptedData);
320+
return;
321+
}
307322

308323
try {
309324
String decryptedJson = aesCipher.decrypt(encryptedData);
325+
310326
if (decryptedJson.startsWith("\"") && decryptedJson.endsWith("\"")) {
311327
decryptedJson = objectMapper.readValue(decryptedJson, String.class);
312328
}
@@ -330,16 +346,17 @@ public void saveRecommendationsInternal(Map<String, String> encryptedData, Strin
330346
}
331347

332348
log.info("[추천 데이터 저장 완료] memberId={}", member.getId());
349+
333350
recommendationRepository.flush();
334351
entityManager.clear();
335352
} else {
336353
List<ClothingRecommendation> existingData =
337354
recommendationRepository.findByMemberIdAndDate(member.getId(), LocalDate.now());
338355

339356
if (!existingData.isEmpty()) {
340-
341357
for (ClothingRecommendation rec : existingData) {
342358
Map<String, Integer> updatedPrediction = new HashMap<>(rec.getPredictionMap());
359+
343360
ClothingRecommendation updatedEntity = ClothingRecommendation.builder()
344361
.id(rec.getId())
345362
.memberId(rec.getMemberId())
@@ -356,12 +373,17 @@ public void saveRecommendationsInternal(Map<String, String> encryptedData, Strin
356373
entityManager.clear();
357374

358375
log.info("[기존 데이터 지역명 업데이트 완료] memberId={}", member.getId());
376+
} else {
377+
log.info("[기존 데이터 없음] AI 결과가 비어있고 업데이트할 기존 데이터도 없습니다. memberId={}", member.getId());
359378
}
360379
}
361380
}
362381

382+
} catch (CustomException e) {
383+
log.error("[추천 데이터 처리 실패] KNOWN_ERROR - memberId={} message={}", encryptedData.get("userId"), e.getMessage(), e);
384+
throw e;
363385
} catch (Exception e) {
364-
log.error("[추천 데이터 처리 실패] message={}", e.getMessage(), e);
386+
log.error("[추천 데이터 처리 실패] UNKNOWN_ERROR - message={}", e.getMessage(), e);
365387
throw new CustomException(ErrorCode.UNKNOWN_ERROR, "지역명 업데이트 중 오류가 발생했습니다.");
366388
}
367389
}

0 commit comments

Comments
 (0)