diff --git a/.env.example b/.env.example index 1ca4197..18efef2 100644 --- a/.env.example +++ b/.env.example @@ -21,4 +21,12 @@ REDIS_PASSWORD= # DB_PASSWORD=your-db-password # πŸ”§ 개발 λͺ¨λ“œ μ„€μ • -SPRING_PROFILES_ACTIVE=dev \ No newline at end of file +SPRING_PROFILES_ACTIVE=dev + +# πŸ” OAuth 2.0 Client Credentials +GOOGLE_CLIENT_ID=your-google-client-id +GOOGLE_CLIENT_SECRET=your-google-client-secret +NAVER_CLIENT_ID=your-naver-client-id +NAVER_CLIENT_SECRET=your-naver-client-secret +KAKAO_CLIENT_ID=your-kakao-client-id +KAKAO_CLIENT_SECRET=your-kakao-client-secret \ No newline at end of file diff --git a/src/main/kotlin/com/back/koreaTravelGuide/common/security/CustomOAuth2UserService.kt b/src/main/kotlin/com/back/koreaTravelGuide/common/security/CustomOAuth2UserService.kt index 5a61b2c..a3739c5 100644 --- a/src/main/kotlin/com/back/koreaTravelGuide/common/security/CustomOAuth2UserService.kt +++ b/src/main/kotlin/com/back/koreaTravelGuide/common/security/CustomOAuth2UserService.kt @@ -23,6 +23,8 @@ class CustomOAuth2UserService( val oAuthUserInfo = when (provider) { "google" -> parseGoogle(attributes) + "naver" -> parseNaver(attributes) + "kakao" -> parseKakao(attributes) else -> throw IllegalArgumentException("μ§€μ›ν•˜μ§€ μ•ŠλŠ” μ†Œμ…œ λ‘œκ·ΈμΈμž…λ‹ˆλ‹€.") } @@ -57,6 +59,33 @@ class CustomOAuth2UserService( profileImageUrl = attributes["picture"] as String?, ) } + + private fun parseNaver(attributes: Map): OAuthUserInfo { + val response = attributes["response"] as Map + + return OAuthUserInfo( + oauthId = response["id"] as String, + email = response["email"] as String, + nickname = response["name"] as String, + profileImageUrl = response["profile_image"] as String?, + ) + } + + private fun parseKakao(attributes: Map): OAuthUserInfo { + val kakaoAccount = attributes["kakao_account"] as? Map + val profile = kakaoAccount?.get("profile") as? Map + val kakaoId = attributes["id"].toString() + + // μΉ΄μΉ΄μ˜€λŠ” 이메일 λͺ»λ°›μ•„μ„œ μ΄λ ‡κ²Œ μ²˜λ¦¬ν–ˆμŒ + val email = kakaoAccount?.get("email") as? String ?: "kakao_$kakaoId@social.login" + + return OAuthUserInfo( + oauthId = kakaoId, + email = email, + nickname = profile?.get("nickname") as? String ?: "μ‚¬μš©μž", + profileImageUrl = profile?.get("profile_image_url") as? String, + ) + } } data class OAuthUserInfo( diff --git a/src/main/kotlin/com/back/koreaTravelGuide/common/security/SecurityConfig.kt b/src/main/kotlin/com/back/koreaTravelGuide/common/security/SecurityConfig.kt index cb687f6..c854245 100644 --- a/src/main/kotlin/com/back/koreaTravelGuide/common/security/SecurityConfig.kt +++ b/src/main/kotlin/com/back/koreaTravelGuide/common/security/SecurityConfig.kt @@ -68,7 +68,7 @@ class SecurityConfig( } if (!isDev) { - addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter::class.java) + addFilterBefore(jwtAuthenticationFilter) } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 57aa50f..6719198 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -70,13 +70,53 @@ spring: session: store-type: none # Redis 없어도 μ‹€ν–‰ κ°€λŠ₯ν•˜λ„λ‘ λ³€κ²½ timeout: 30m - # Redis μžλ™ μ„€μ • λΉ„ν™œμ„±ν™” (μ„Έμ…˜ λΉ„ν™œμ„±ν™”μš©) autoconfigure: exclude: - org.springframework.boot.autoconfigure.session.SessionAutoConfiguration - org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration - + + security: + oauth2: + client: + registration: + google: + client-id: ${GOOGLE_CLIENT_ID} + client-secret: ${GOOGLE_CLIENT_SECRET} + scope: + - profile + - email + naver: + client-id: ${NAVER_CLIENT_ID} + client-secret: ${NAVER_CLIENT_SECRET} + authorization-grant-type: authorization_code + redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}" + scope: + - name + - email + - profile_image + client-name: Naver + kakao: + client-id: ${KAKAO_CLIENT_ID} + client-secret: ${KAKAO_CLIENT_SECRET} + client-authentication-method: client_secret_post + authorization-grant-type: authorization_code + redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}" + scope: + - profile_nickname + - profile_image + client-name: Kakao + provider: + naver: + authorization-uri: https://nid.naver.com/oauth2.0/authorize + token-uri: https://nid.naver.com/oauth2.0/token + user-info-uri: https://openapi.naver.com/v1/nid/me + user-name-attribute: response + kakao: + authorization-uri: https://kauth.kakao.com/oauth/authorize + token-uri: https://kauth.kakao.com/oauth/token + user-info-uri: https://kapi.kakao.com/v2/user/me + user-name-attribute: id # Swagger API λ¬Έμ„œ μ„€μ • (μ£Όλ‹ˆμ–΄ 개발자용) springdoc: api-docs: