레디스 JWT 토큰 저장소 - tokenList 가 계속 null 이 되면서 초기화가 되지 않아 set 으로 변경하고 default value 를 이용한 초기화로 변경

This commit is contained in:
Klaus 2023-08-08 12:47:49 +09:00
parent f7cdf40976
commit 771dbeced0
3 changed files with 5 additions and 9 deletions

View File

@ -66,11 +66,9 @@ class TokenProvider(
val lock = getOrCreateLock(memberId = memberId)
lock.write {
val memberToken = tokenRepository.findByIdOrNull(memberId)
?: MemberToken(id = memberId, tokenList = mutableListOf())
?: MemberToken(id = memberId)
val memberTokenSet = memberToken.tokenList.toMutableSet()
memberTokenSet.add(token)
memberToken.tokenList = memberTokenSet.toMutableList()
memberToken.tokenSet.add(token)
tokenRepository.save(memberToken)
}
@ -89,7 +87,7 @@ class TokenProvider(
val memberToken = tokenRepository.findByIdOrNull(id = claims.subject.toLong())
?: throw SodaException("로그인 정보를 확인해주세요.")
if (!memberToken.tokenList.contains(token)) throw SodaException("로그인 정보를 확인해주세요.")
if (!memberToken.tokenSet.contains(token)) throw SodaException("로그인 정보를 확인해주세요.")
val member = repository.findByIdOrNull(id = claims.subject.toLong())
?: throw SodaException("로그인 정보를 확인해주세요.")

View File

@ -371,9 +371,7 @@ class MemberService(
val memberToken = tokenRepository.findByIdOrNull(memberId)
?: throw SodaException("로그인 정보를 확인해주세요.")
val memberTokenSet = memberToken.tokenList.toMutableSet()
memberTokenSet.remove(token)
memberToken.tokenList = memberTokenSet.toMutableList()
memberToken.tokenSet.remove(token)
tokenRepository.save(memberToken)
}
}

View File

@ -7,5 +7,5 @@ import org.springframework.data.redis.core.RedisHash
data class MemberToken(
@Id
val id: Long,
var tokenList: MutableList<String>
var tokenSet: MutableSet<String> = mutableSetOf()
)