Skip to content

Commit 340a71a

Browse files
authored
security 수정 (#134)
1 parent 82c86ac commit 340a71a

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/main/kotlin/com/back/koreaTravelGuide/common/security/AuthenticationExtensions.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ package com.back.koreaTravelGuide.common.security
33
import org.springframework.security.core.Authentication
44

55
fun Authentication.getUserId(): Long {
6-
if (principal is Long) {
7-
return principal as Long
6+
return when (val principal = this.principal) {
7+
// jwtAuthenticFilter
8+
is Long -> principal
9+
is CustomOAuth2User -> principal.id
10+
else -> {
11+
this.name.toLongOrNull()
12+
?: throw IllegalStateException("인증 정보에서 사용자 ID를 찾을 수 없습니다. Principal: $principal")
13+
}
814
}
9-
throw IllegalStateException("인증된 사용자 ID를 찾을 수 없거나 타입이 올바르지 않습니다.")
1015
}

src/main/kotlin/com/back/koreaTravelGuide/common/security/CustomOAuth2LoginSuccessHandler.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.back.koreaTravelGuide.common.security
22

3+
import com.back.koreaTravelGuide.common.config.AppConfig
34
import com.back.koreaTravelGuide.domain.user.enums.UserRole
45
import com.back.koreaTravelGuide.domain.user.repository.UserRepository
56
import jakarta.servlet.http.Cookie
@@ -19,6 +20,7 @@ class CustomOAuth2LoginSuccessHandler(
1920
private val userRepository: UserRepository,
2021
private val redisTemplate: RedisTemplate<String, String>,
2122
@Value("\${jwt.refresh-token-expiration-days}") private val refreshTokenExpirationDays: Long,
23+
private val appConfig: AppConfig,
2224
) : SimpleUrlAuthenticationSuccessHandler() {
2325
@Transactional
2426
override fun onAuthenticationSuccess(
@@ -35,7 +37,7 @@ class CustomOAuth2LoginSuccessHandler(
3537
if (user.role == UserRole.PENDING) {
3638
val registerToken = jwtTokenProvider.createRegisterToken(user.id!!)
3739

38-
val targetUrl = "http://localhost:3000/signup/role?token=$registerToken"
40+
val targetUrl = "${AppConfig.siteFrontUrl}/signup/role?token=$registerToken"
3941

4042
redirectStrategy.sendRedirect(request, response, targetUrl)
4143
} else {
@@ -58,7 +60,7 @@ class CustomOAuth2LoginSuccessHandler(
5860

5961
response.addCookie(cookie)
6062

61-
val targetUrl = "http://localhost:3000/oauth/callback"
63+
val targetUrl = "${AppConfig.siteFrontUrl}/oauth/callback"
6264

6365
redirectStrategy.sendRedirect(request, response, targetUrl)
6466
}

src/main/kotlin/com/back/koreaTravelGuide/common/security/SecurityConfig.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,11 @@ class SecurityConfig(
5353
}
5454
}
5555

56-
if (!isDev) {
57-
oauth2Login {
58-
userInfoEndpoint {
59-
userService = customOAuth2UserService
60-
}
61-
authenticationSuccessHandler = customOAuth2LoginSuccessHandler
56+
oauth2Login {
57+
userInfoEndpoint {
58+
userService = customOAuth2UserService
6259
}
60+
authenticationSuccessHandler = customOAuth2LoginSuccessHandler
6361
}
6462

6563
authorizeHttpRequests {
@@ -77,9 +75,7 @@ class SecurityConfig(
7775
authorize(anyRequest, authenticated)
7876
}
7977
}
80-
if (!isDev) {
81-
addFilterBefore<UsernamePasswordAuthenticationFilter>(jwtAuthenticationFilter)
82-
}
78+
addFilterBefore<UsernamePasswordAuthenticationFilter>(jwtAuthenticationFilter)
8379
}
8480

8581
return http.build()

0 commit comments

Comments
 (0)