Skip to content

Refactor/jira kan 71 token 쿠키 저장 및 redis aof rdb 고려#85

Merged
usingjun merged 47 commits intodevelopfrom
Refactor/JIRA-kan-71-refactor-token-쿠키-저장-및-redis-aof-rdb-고려
Apr 9, 2025

Hidden character warning

The head ref may contain hidden characters: "Refactor/JIRA-kan-71-refactor-token-\ucfe0\ud0a4-\uc800\uc7a5-\ubc0f-redis-aof-rdb-\uace0\ub824"
Merged

Refactor/jira kan 71 token 쿠키 저장 및 redis aof rdb 고려#85
usingjun merged 47 commits intodevelopfrom
Refactor/JIRA-kan-71-refactor-token-쿠키-저장-및-redis-aof-rdb-고려

Conversation

@usingjun
Copy link
Collaborator

@usingjun usingjun commented Apr 8, 2025

🛠️ 과제 요약

  • token 쿠키 저장 및 redis aof rdb 고려
  • 예약 송금 & 이상 감지 적용

📝 요구 사항과 구현 내용

  • access, refresh Token httpOnly로 쿠키 저장
  • redis aof & rdb 모두 적용해 보안성 높임
  • refreshToken tokenRotate 적용해 재사용 불가능 설정
  • 예약 송금 구현
  • 이상 감지 적용(한도 초과, 5분 내 거래 횟수 초과, 동일 금액 반복, 심야 시간대 거래)

✅ 피드백 반영사항

  • 피드백 받은 내용과 반영된 결과 적기

💬 PR 포인트 & 궁금한 점

  • 예약 송금은 @Scheduler 사용해 1분마다 송금 가능하게 구현
  • dto에 @FutureOrPresent 적용해 바로 송금하거나 예약 송금 적용
  • redisTemplate와 Lua 스크립트 적용해 이상탐지

🔗 연관된 이슈

  • 열린 이슈를 닫고싶으면
  • close #이슈번호 [이슈제목]
  • 이슈를 닫지 않을 거라면 작성X

usingjun added 30 commits April 3, 2025 19:39
…-및-redis-aof-rdb-고려

# Conflicts:
#	src/test/java/bumblebee/xchangepass/config/TestUserInitializer.java
@usingjun usingjun added the 🔨 Refactor 코드 리팩토링 label Apr 8, 2025
@usingjun usingjun self-assigned this Apr 8, 2025
@usingjun usingjun linked an issue Apr 8, 2025 that may be closed by this pull request
2 tasks
Copy link
Collaborator

@Si-rauis Si-rauis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분 한 번 확인부탁드립니다!

ResponseCookie refreshCookie = loginService.saveRefreshToken(response);

return ResponseEntity.ok()
.header("Set-Cookie", accessCookie.toString())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

access 토큰도 쿠키로 반환하는 것일까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 accessToken을 localStorage에 저장하게 되면 XSS 공격 시 토큰이 노출될 수 있는데
이를 HttpOnly 쿠키에 저장하면 XSS에 더 안전하게 됩니다.

build.gradle Outdated
testImplementation "org.testcontainers:testcontainers:1.19.2" // Docker 기반 통합 테스트 지원 (Testcontainers 기본 라이브러리)
testImplementation "org.testcontainers:junit-jupiter:1.19.2" // JUnit 5(Testcontainers 연동) 지원
testImplementation 'org.testcontainers:postgresql:1.19.2'
testImplementation "org.testcontainers:redis:1.19.1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testImplementation 'com.redis:testcontainers-redis:2.2.2' // Redis용 Testcontainers (테스트 환경에서 Redis 컨테이너 실행) 이것과 다른 의존성인가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알아보니 "org.testcontainers:redis:1.19.1"는

  • Redis 클러스터 모드 테스트 (노드 여러 개로 분산)
  • Redis Sentinel 기반 장애 조치 테스트
  • Streams, Pub/Sub, RedisTimeSeries 등 특화 모듈 테스트
  • 복잡한 상태 시뮬레이션 (failover, re-shard 등)
    등의 고급 테스트 시나리오에 사용된다고 합니다.

저희 프로젝트는 단일 redis 인스턴스만을 띄우고 있기때문에 불필요하다고 판단합니다.

return storedToken != null && storedToken.equals(refreshToken);
}

public String getRefreshToken(Long userId) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

일관성을 위해서 주석을 추가해주시면 감사하겠습니다

Long userId = refreshTokenRepository.getUserIdFromRefreshToken(refreshToken);

// Redis에 저장된 토큰과 비교 → 재사용 감지
String storedToken = refreshTokenRepository.getRefreshToken(userId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 로직으로 볼 때 Redis에 전에 사용했던 refreshToken을 누적시키는 것 처럼 보이는데 redis의 용량은 한정적인데 방안이 있을까요?

Copy link
Collaborator Author

@usingjun usingjun Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RefreshToken Rotate방식을 적용해서 RefreshToken을 사용하면 레디스에서 삭제후 재발급하는 방식을 사용하고있습니다.

if (storedToken == null || !storedToken.equals(refreshToken)) {
// 이미 사용된 토큰 or 위조된 토큰 → 재사용 시도
refreshTokenRepository.deleteRefreshToken(userId); // Redis 강제 삭제

        throw ErrorCode.REFRESH_TOKEN_INVALID.commonException();

}

.secure(true)
.path("/")
.maxAge(60 * 60 * 24 * 7)
.sameSite("Strict")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sameStite 설정의 경우 https 도메인이 아니면 의미가 없는데 대체 방안이 있을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

금융서비스 특징상 https를 사용할 것이라 생각해서 만들었습니다만 만약 아니라면
.sameSite("Lax")를 적용해서 최소한의 CSRF 방어하겠습니다

Comment on lines +54 to +55
.header("Set-Cookie", accessCookie.toString())
.header("Set-Cookie", refreshCookie.toString())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

대체적으로 access 로컬 / refresh 쿠키로 저장하는게 일반적인데 access도 쿠키로 저장하는 이유가 있을까요?

@@ -75,6 +79,13 @@ public void withdrawBalance(WalletBalance balance, BigDecimal amount) {
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

walletbalance 서버스도 다른 서버스와 같이 간단하게 주석을 추가해주실 수 있을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵!

Copy link
Collaborator

@Si-rauis Si-rauis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수고하셨습니다!

@usingjun usingjun merged commit 32fcf85 into develop Apr 9, 2025
1 check passed
@usingjun usingjun deleted the Refactor/JIRA-kan-71-refactor-token-쿠키-저장-및-redis-aof-rdb-고려 branch April 9, 2025 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 Refactor 코드 리팩토링

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KAN-71 [Refactor] token 쿠키 저장 및 redis aof & rdb 고려

2 participants