Skip to content

Commit 343e37f

Browse files
정동하정동하
authored andcommitted
feat(be):소셜로그인 구현
1 parent 85007ed commit 343e37f

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class CustomOAuth2UserService(
2323
val oAuthUserInfo =
2424
when (provider) {
2525
"google" -> parseGoogle(attributes)
26+
"naver" -> parseNaver(attributes)
27+
"kakao" -> parseKakao(attributes)
2628
else -> throw IllegalArgumentException("지원하지 않는 소셜 로그인입니다.")
2729
}
2830

@@ -57,6 +59,33 @@ class CustomOAuth2UserService(
5759
profileImageUrl = attributes["picture"] as String?,
5860
)
5961
}
62+
63+
private fun parseNaver(attributes: Map<String, Any>): OAuthUserInfo {
64+
val response = attributes["response"] as Map<String, Any>
65+
66+
return OAuthUserInfo(
67+
oauthId = response["id"] as String,
68+
email = response["email"] as String,
69+
nickname = response["name"] as String,
70+
profileImageUrl = response["profile_image"] as String?,
71+
)
72+
}
73+
74+
private fun parseKakao(attributes: Map<String, Any>): OAuthUserInfo {
75+
val kakaoAccount = attributes["kakao_account"] as? Map<String, Any>
76+
val profile = kakaoAccount?.get("profile") as? Map<String, Any>
77+
val kakaoId = attributes["id"].toString()
78+
79+
// 카카오는 이메일 못받아서 이렇게 처리했음
80+
val email = kakaoAccount?.get("email") as? String ?: "kakao_$kakaoId@social.login"
81+
82+
return OAuthUserInfo(
83+
oauthId = kakaoId,
84+
email = email,
85+
nickname = profile?.get("nickname") as? String ?: "사용자",
86+
profileImageUrl = profile?.get("profile_image_url") as? String,
87+
)
88+
}
6089
}
6190

6291
data class OAuthUserInfo(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class SecurityConfig(
6868
}
6969

7070
if (!isDev) {
71-
addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter::class.java)
71+
addFilterBefore<UsernamePasswordAuthenticationFilter>(jwtAuthenticationFilter)
7272
}
7373
}
7474

src/main/resources/application.yml

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,53 @@ spring:
7070
session:
7171
store-type: none # Redis 없어도 실행 가능하도록 변경
7272
timeout: 30m
73-
7473
# Redis 자동 설정 비활성화 (세션 비활성화용)
7574
autoconfigure:
7675
exclude:
7776
- org.springframework.boot.autoconfigure.session.SessionAutoConfiguration
7877
- org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
7978

79+
security:
80+
oauth2:
81+
client:
82+
registration:
83+
google:
84+
client-id: 11962659605-optp0eb5h1c1ob1b9qie53s83j165qt8.apps.googleusercontent.com
85+
client-secret: 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
86+
scope:
87+
- profile
88+
- email
89+
naver:
90+
client-id: At2KDxDK_ppHMuMYqEDL
91+
client-secret: osKwSljl9i
92+
authorization-grant-type: authorization_code
93+
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
94+
scope:
95+
- name
96+
- email
97+
- profile_image
98+
client-name: Naver
99+
kakao:
100+
client-id: da170def761db299a36665bbcf50817b
101+
client-secret: SoEKP391vVXvrMy3nANjdWZA7RrheBJW
102+
client-authentication-method: client_secret_post
103+
authorization-grant-type: authorization_code
104+
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
105+
scope:
106+
- profile_nickname
107+
- profile_image
108+
client-name: Kakao
109+
provider:
110+
naver:
111+
authorization-uri: https://nid.naver.com/oauth2.0/authorize
112+
token-uri: https://nid.naver.com/oauth2.0/token
113+
user-info-uri: https://openapi.naver.com/v1/nid/me
114+
user-name-attribute: response
115+
kakao:
116+
authorization-uri: https://kauth.kakao.com/oauth/authorize
117+
token-uri: https://kauth.kakao.com/oauth/token
118+
user-info-uri: https://kapi.kakao.com/v2/user/me
119+
user-name-attribute: id
80120
# Swagger API 문서 설정 (주니어 개발자용)
81121
springdoc:
82122
api-docs:

0 commit comments

Comments
 (0)