Skip to content

Commit 331ba03

Browse files
committed
feat : (#31) add oauthSuccessHandler
1 parent 71db75e commit 331ba03

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package entry.dsm.gitauth.equusgithubauth.global.oauth.handler
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import jakarta.servlet.http.HttpServletRequest
5+
import jakarta.servlet.http.HttpServletResponse
6+
import org.springframework.security.core.Authentication
7+
import org.springframework.security.web.authentication.AuthenticationSuccessHandler
8+
import entry.dsm.gitauth.equusgithubauth.global.security.auth.CustomOauth2UserDetails
9+
10+
class CustomOAuth2AuthenticationSuccessHandler(
11+
private val objectMapper: ObjectMapper
12+
) : AuthenticationSuccessHandler {
13+
14+
override fun onAuthenticationSuccess(
15+
request: HttpServletRequest,
16+
response: HttpServletResponse,
17+
authentication: Authentication
18+
) {
19+
// CustomOauth2UserDetails에 저장된 attributes에서 토큰 정보 추출
20+
val oauthUser = authentication.principal as? CustomOauth2UserDetails
21+
val tokenInfo = oauthUser?.attributes?.let { attributes ->
22+
mapOf(
23+
"accessToken" to attributes["accessToken"],
24+
"accessTokenExpiration" to attributes["accessTokenExpiration"],
25+
"refreshToken" to attributes["refreshToken"],
26+
"refreshTokenExpiration" to attributes["refreshTokenExpiration"]
27+
)
28+
}
29+
response.contentType = "application/json"
30+
response.characterEncoding = "UTF-8"
31+
response.writer.write(objectMapper.writeValueAsString(tokenInfo))
32+
}
33+
}

0 commit comments

Comments
 (0)