Skip to content

Commit cf28ecc

Browse files
committed
Feat: 구글 로그인 구현
1 parent 8771330 commit cf28ecc

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/main/java/com/back/global/security/oauth/CustomOAuth2UserService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
4444
OAuth2UserInfo userInfo = switch (registrationId) {
4545
case "kakao" -> new KakaoOAuth2UserInfo(attributes);
4646
case "naver" -> new NaverOAuth2UserInfo(attributes);
47+
case "google" -> new GoogleOAuth2UserInfo(attributes);
4748
default -> throw new CustomException(ErrorCode.UNSUPPORTED_OAUTH_PROVIDER);
4849
};
4950

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.back.global.security.oauth;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* 구글 OAuth2 사용자 정보 구현체
7+
*/
8+
public class GoogleOAuth2UserInfo implements OAuth2UserInfo {
9+
private final Map<String, Object> attributes;
10+
11+
public GoogleOAuth2UserInfo(Map<String, Object> attributes) {
12+
this.attributes = attributes;
13+
}
14+
15+
@Override
16+
public String getProvider() {
17+
return "google";
18+
}
19+
20+
@Override
21+
public String getProviderId() {
22+
return (String) attributes.get("sub"); // 구글은 sub가 고유 ID
23+
}
24+
25+
@Override
26+
public String getEmail() {
27+
return (String) attributes.get("email");
28+
}
29+
30+
@Override
31+
public String getNickname() {
32+
return (String) attributes.get("name");
33+
}
34+
35+
@Override
36+
public String getProfileImageUrl() {
37+
return (String) attributes.getOrDefault("picture", null);
38+
}
39+
}

src/main/resources/application-dev.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,25 @@ spring:
3131
kakao:
3232
client-id: ${KAKAO_CLIENT_ID}
3333
authorization-grant-type: authorization_code
34+
client-name: Kakao
3435
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
3536
scope: profile_nickname, profile_image, account_email
36-
client-name: Kakao
3737
naver:
3838
client-id: ${NAVER_CLIENT_ID}
3939
client-secret: ${NAVER_CLIENT_SECRET}
40+
client-name: Naver
4041
authorization-grant-type: authorization_code
4142
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
4243
scope: email, nickname, profile_image
43-
client-name: Naver
44+
google:
45+
client-id: ${GOOGLE_CLIENT_ID}
46+
client-secret: ${GOOGLE_CLIENT_SECRET}
47+
client-name: Google
48+
authorization-grant-type: authorization_code
49+
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
50+
scope:
51+
- email
52+
- profile
4453
provider:
4554
kakao:
4655
authorization-uri: https://kauth.kakao.com/oauth/authorize
@@ -52,6 +61,11 @@ spring:
5261
token-uri: https://nid.naver.com/oauth2.0/token
5362
user-info-uri: https://openapi.naver.com/v1/nid/me
5463
user-name-attribute: response
64+
google:
65+
authorization-uri: https://accounts.google.com/o/oauth2/v2/auth
66+
token-uri: https://oauth2.googleapis.com/token
67+
user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo
68+
user-name-attribute: sub
5569

5670
springdoc:
5771
default-produces-media-type: application/json;charset=UTF-8

0 commit comments

Comments
 (0)