feat: ログインユーザー取得部分を共通化

This commit is contained in:
usbharu 2024-01-26 12:06:34 +09:00
parent a60b58daef
commit e2365c632b
2 changed files with 19 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package dev.usbharu.hideout.core.infrastructure.springframework.security
interface LoginUserContextHolder {
fun getLoginUserId(): Long
}

View File

@ -0,0 +1,14 @@
package dev.usbharu.hideout.core.infrastructure.springframework.security
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.oauth2.jwt.Jwt
import org.springframework.stereotype.Component
@Component
class OAuth2JwtLoginUserContextHolder : LoginUserContextHolder {
override fun getLoginUserId(): Long {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
return principal.getClaim<String>("uid").toLong()
}
}