-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2SuccessHandler.kt
More file actions
94 lines (87 loc) · 4.4 KB
/
Copy pathOAuth2SuccessHandler.kt
File metadata and controls
94 lines (87 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package befly.beflygateway.handler
import befly.beflygateway.dto.LoginRequest
import befly.beflygateway.dto.LoginResponse
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseCookie
import org.springframework.security.core.Authentication
import org.springframework.security.oauth2.core.user.OAuth2User
import org.springframework.security.web.server.WebFilterExchange
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Mono
import java.net.URI
import java.time.Duration
@Component
class OAuth2SuccessHandler (
private val webClient: WebClient
): ServerAuthenticationSuccessHandler {
@Value("\${url.front}")
lateinit var FRONT_END_URL: String
override fun onAuthenticationSuccess(
webFilterExchange: WebFilterExchange?,
authentication: Authentication?
): Mono<Void> = (authentication?.principal as? OAuth2User)
?.getAttribute<Long>("id")
?.toString()
?.let { userId ->
webClient
.post()
.uri("/auth/oauth2")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.ALL)
.bodyValue(LoginRequest(userId))
.retrieve()
.bodyToMono(LoginResponse::class.java)
.flatMap { response ->
webFilterExchange?.exchange?.let { exchange ->
response
.takeIf { it.signUpStatus }
?.run {
val accessCookie = ResponseCookie.from("accessToken", "${response.accessToken}")
.httpOnly(true)
.secure(true)
.sameSite("Strict")
.domain(".befly.blog")
.maxAge(Duration.ofMinutes(15))
.path("/")
.build()
val refreshCookie = ResponseCookie.from("refreshToken", "${response.refreshToken}!!")
.httpOnly(true)
.secure(true)
.sameSite("Strict")
.domain(".befly.blog")
.maxAge(Duration.ofDays(7))
.path("/")
.build()
webClient
.get()
.uri("/auth/refresh")
.accept(MediaType.ALL)
.header("X-Refresh-Token", refreshToken)
.retrieve()
exchange.response.addCookie(accessCookie)
exchange.response.addCookie(refreshCookie)
exchange.response.statusCode = HttpStatus.FOUND
exchange.response.headers.location = URI.create("$FRONT_END_URL/")
}
?: run {//회원가입 페이지로 리다이렉트
exchange.response.statusCode = HttpStatus.FOUND
val tempCookie = ResponseCookie.from("tempClientId", userId)
.httpOnly(false)
.secure(true)
.sameSite("Lax")
.domain(".befly.blog")
.maxAge(Duration.ofMinutes(10))
.path("/")
.build()
exchange.response.addCookie(tempCookie)
exchange.response.headers.location = URI.create("$FRONT_END_URL/signup")
}
Mono.empty()
} ?: Mono.empty()
}
}!!
}