Skip to content

Commit

Permalink
refactor : replace deprecated serialization utils (#163)
Browse files Browse the repository at this point in the history
* refactor : replace deprecated serialization utils

* 리뷰반영
  • Loading branch information
kshired authored Dec 17, 2023
1 parent e1ac17e commit 96656d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
32 changes: 24 additions & 8 deletions src/main/kotlin/io/csbroker/apiserver/common/util/CookieUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import jakarta.servlet.http.Cookie
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest
import org.springframework.util.SerializationUtils
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable
import java.util.Base64

fun getCookie(request: HttpServletRequest, name: String) = request.cookies?.let {
Expand Down Expand Up @@ -32,13 +36,25 @@ fun deleteCookie(request: HttpServletRequest, response: HttpServletResponse, nam

fun OAuth2AuthorizationRequest.serialize(): String {
return Base64.getUrlEncoder()
.encodeToString(SerializationUtils.serialize(this))
.encodeToString(this.toByteArray())
}

fun <T> deserialize(cookie: Cookie, cls: Class<T>): T {
return cls.cast(
SerializationUtils.deserialize(
Base64.getUrlDecoder().decode(cookie.value),
),
)
inline fun <reified T> Cookie.deserialize(): T {
return Base64.getUrlDecoder().decode(this.value).let {
ByteArrayInputStream(it).use { byteArrayInputStream ->
ObjectInputStream(byteArrayInputStream).use { objectInput ->
objectInput.readObject()
}
}
} as? T ?: throw ClassCastException()
}

private fun Serializable.toByteArray(): ByteArray {
return ByteArrayOutputStream().use {
ObjectOutputStream(it).use { objectOutputStream ->
objectOutputStream.writeObject(this)
objectOutputStream.flush()
it.toByteArray()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ private const val COOKIE_EXPIRE_SECONDS = 180L
class OAuth2AuthorizationRequestBasedOnCookieRepository : AuthorizationRequestRepository<OAuth2AuthorizationRequest> {
override fun loadAuthorizationRequest(request: HttpServletRequest): OAuth2AuthorizationRequest? {
val cookie = getCookie(request, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME) ?: return null

return deserialize(cookie, OAuth2AuthorizationRequest::class.java)
return cookie.deserialize()
}

override fun saveAuthorizationRequest(
Expand Down

0 comments on commit 96656d1

Please sign in to comment.