Skip to content

Commit

Permalink
fix: Redis 조회가 안되는 오류 수정 (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhhong0509 authored Aug 15, 2022
1 parent 794b869 commit a2c5fa6
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.xquare.v1userservice.user.api

import com.xquare.v1userservice.user.api.dtos.SignInDomainRequest
import com.xquare.v1userservice.user.api.dtos.SignInResponse
import com.xquare.v1userservice.user.api.dtos.TokenRefreshResponse
import com.xquare.v1userservice.user.api.dtos.TokenResponse

interface UserSignInApi {
suspend fun userSignIn(signInDomainRequest: SignInDomainRequest): SignInResponse
suspend fun userTokenRefresh(refreshToken: String): TokenRefreshResponse
suspend fun userSignIn(signInDomainRequest: SignInDomainRequest): TokenResponse
suspend fun userTokenRefresh(refreshToken: String): TokenResponse
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.xquare.v1userservice.user.api.dtos

import java.time.LocalDateTime

data class SignInResponse(
data class TokenResponse(
val accessToken: String,
val refreshToken: String,
val expireAt: LocalDateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import com.xquare.v1userservice.user.TokenType
import com.xquare.v1userservice.user.User
import com.xquare.v1userservice.user.api.UserSignInApi
import com.xquare.v1userservice.user.api.dtos.SignInDomainRequest
import com.xquare.v1userservice.user.api.dtos.SignInResponse
import com.xquare.v1userservice.user.api.dtos.TokenRefreshResponse
import com.xquare.v1userservice.user.api.dtos.TokenResponse
import com.xquare.v1userservice.user.exceptions.PasswordNotMatchesException
import com.xquare.v1userservice.user.exceptions.UserNotFoundException
import com.xquare.v1userservice.user.refreshtoken.RefreshToken
Expand All @@ -27,7 +26,7 @@ class UserSignInApiImpl(
private val authorityListSpi: AuthorityListSpi,
private val refreshTokenSpi: RefreshTokenSpi
) : UserSignInApi {
override suspend fun userSignIn(signInDomainRequest: SignInDomainRequest): SignInResponse {
override suspend fun userSignIn(signInDomainRequest: SignInDomainRequest): TokenResponse {
val user = userRepositorySpi.findByAccountIdAndStateWithCreated(signInDomainRequest.accountId)
?: throw UserNotFoundException(UserNotFoundException.USER_ID_NOT_FOUND)

Expand All @@ -44,7 +43,7 @@ class UserSignInApiImpl(

val refreshToken = saveNewRefreshToken(user, params)

return SignInResponse(
return TokenResponse(
accessToken = accessToken,
refreshToken = refreshToken.tokenValue,
expireAt = expireAt
Expand All @@ -59,7 +58,7 @@ class UserSignInApiImpl(
}
}

override suspend fun userTokenRefresh(refreshToken: String): TokenRefreshResponse {
override suspend fun userTokenRefresh(refreshToken: String): TokenResponse {
val pureRefreshToken = refreshToken.split(" ").getOrNull(1)
?: throw InvalidRefreshTokenException("Invalid Refresh Token")

Expand All @@ -73,14 +72,15 @@ class UserSignInApiImpl(

val params = buildAccessTokenParams(user)

saveNewRefreshToken(user, params)
val refreshTokenDomain = saveNewRefreshToken(user, params)

val accessToken = jwtTokenGeneratorSpi.generateJwtToken(user.accountId, TokenType.ACCESS_TOKEN, params)

val expireAt = LocalDateTime.now().plusHours(jwtTokenGeneratorSpi.getAccessTokenExpirationAsHour().toLong())

return TokenRefreshResponse(
return TokenResponse(
accessToken = accessToken,
refreshToken = refreshTokenDomain.tokenValue,
expireAt = expireAt
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xquare.v1userservice.configuration.redis

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
Expand All @@ -11,11 +12,11 @@ import org.springframework.data.redis.serializer.RedisSerializationContext
import org.springframework.data.redis.serializer.StringRedisSerializer

@Configuration
class RedisConfiguration {
class RedisConfiguration() {
@Primary
@Bean
fun redisOperations(factory: ReactiveRedisConnectionFactory): ReactiveRedisOperations<String, Any> {
val jsonSerializer = GenericJackson2JsonRedisSerializer()
fun redisOperations(factory: ReactiveRedisConnectionFactory, objectMapper: ObjectMapper): ReactiveRedisOperations<String, Any> {
val jsonSerializer = GenericJackson2JsonRedisSerializer(objectMapper)
val stringSerializer = StringRedisSerializer()

val serializationContext = RedisSerializationContext.newSerializationContext<String, Any>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import org.springframework.http.codec.json.Jackson2JsonDecoder
import org.springframework.web.reactive.function.client.ExchangeStrategies
import org.springframework.web.reactive.function.client.WebClient


@Configuration
class WebClientConfiguration {
@Bean
fun webClient(objectMapper: ObjectMapper): WebClient =
WebClient.builder()
.exchangeStrategies(ExchangeStrategies.builder()
.codecs { configurer: ClientCodecConfigurer ->
configurer.defaultCodecs().jackson2JsonDecoder(Jackson2JsonDecoder(objectMapper))
}
.build())
.exchangeStrategies(
ExchangeStrategies.builder()
.codecs { configurer: ClientCodecConfigurer ->
configurer.defaultCodecs().jackson2JsonDecoder(Jackson2JsonDecoder(objectMapper))
}
.build()
)
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import com.xquare.v1userservice.exceptions.BaseException
class ApplicationRequestFailedException(
message: String,
statusCode: Int
): BaseException(message, statusCode)
) : BaseException(message, statusCode)
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import com.xquare.v1userservice.exceptions.BaseException
class AuthorityRequestFailedException(
message: String,
statusCode: Int
): BaseException(message, statusCode)
) : BaseException(message, statusCode)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.xquare.v1userservice.user.refreshtoken.spi

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.convertValue
import com.xquare.v1userservice.jwt.properties.JwtProperties
import com.xquare.v1userservice.user.refreshtoken.RefreshToken
import com.xquare.v1userservice.user.refreshtoken.RefreshTokenEntity
Expand All @@ -15,7 +17,8 @@ import org.springframework.stereotype.Repository
class RefreshTokenSpiImpl(
private val reactiveRedisOperations: ReactiveRedisOperations<String, Any>,
private val refreshTokenDomainMapper: RefreshTokenDomainMapper,
private val jwtProperties: JwtProperties
private val jwtProperties: JwtProperties,
private val objectMapper: ObjectMapper
) : RefreshTokenSpi {
override suspend fun saveRefreshToken(refreshToken: RefreshToken): RefreshToken {
val refreshTokenEntity = refreshTokenDomainMapper.refreshTokenDomainToEntity(refreshToken)
Expand All @@ -32,10 +35,10 @@ class RefreshTokenSpiImpl(
}

override suspend fun findByRefreshToken(refreshToken: String): RefreshToken? {
val refreshTokenEntity = reactiveRedisOperations.opsForValue().get(refreshToken).awaitSingleOrNull()
as? RefreshTokenEntity
val refreshTokenFromDB = reactiveRedisOperations.opsForValue().get(refreshToken).awaitSingleOrNull()
val refreshTokenEntity = refreshTokenFromDB?.let { objectMapper.convertValue<RefreshTokenEntity>(it) }

return refreshTokenEntity?.let { refreshTokenDomainMapper.refreshTokenEntityToDomain(refreshTokenEntity) }
return refreshTokenEntity?.let { refreshTokenDomainMapper.refreshTokenEntityToDomain(it) }
}

override suspend fun delete(refreshToken: RefreshToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class AuthorityListSpiImpl(

private suspend fun sendGetAuthoritiesRequest(userId: UUID): WebClient.ResponseSpec {
return webClient.get().uri { uri ->
uri.scheme("http")
.host(authorityProperties.host)
.path("/authorities/{userId}")
.build(userId)
}.retrieve()
uri.scheme("http")
.host(authorityProperties.host)
.path("/authorities/{userId}")
.build(userId)
}.retrieve()
.onStatus(HttpStatus::isError) {
throw AuthorityRequestFailedException("Request failed to get authorities", it.rawStatusCode())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.xquare.v1userservice.user.router

import com.ninjasquad.springmockk.MockkBean
import com.xquare.v1userservice.configuration.security.SecurityConfig
import com.xquare.v1userservice.user.api.dtos.SignInResponse
import com.xquare.v1userservice.user.api.dtos.TokenResponse
import com.xquare.v1userservice.user.router.dto.CreateUserRequest
import com.xquare.v1userservice.user.router.dto.GetUserResponse
import com.xquare.v1userservice.user.router.dto.SignInRequest
Expand Down Expand Up @@ -90,7 +90,7 @@ internal class UserRouterTest(
.answers(ConstantAnswer(ServerResponse.ok().bodyValueAndAwait(response)))
}

private fun buildUserSignInResponse() = SignInResponse(
private fun buildUserSignInResponse() = TokenResponse(
accessToken = ACCESS_TOKEN_SUCCESS_VALUE,
refreshToken = REFRESH_TOKEN_SUCCESS_VALUE,
expireAt = LocalDateTime.now().plusHours(2)
Expand All @@ -107,7 +107,7 @@ internal class UserRouterTest(
.uri("/users/login")
.body<SignInRequest>(signInRequest.toMono())
.exchange()
.returnResult<SignInResponse>()
.returnResult<TokenResponse>()

@Test
fun getUserById() = runTest {
Expand Down

0 comments on commit a2c5fa6

Please sign in to comment.