From 58401ec09dcaf8245324ae45635596014225eb25 Mon Sep 17 00:00:00 2001 From: SJ70 Date: Thu, 1 Aug 2024 19:08:50 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20oauth=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20api=20url=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/j9/bestmoments/controller/AuthController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/j9/bestmoments/controller/AuthController.java b/src/main/java/com/j9/bestmoments/controller/AuthController.java index 30c5173..13adc35 100644 --- a/src/main/java/com/j9/bestmoments/controller/AuthController.java +++ b/src/main/java/com/j9/bestmoments/controller/AuthController.java @@ -31,7 +31,7 @@ public class AuthController { private final MemberService memberService; private final TokenService tokenService; - @GetMapping("/{registrationId}/callback") + @GetMapping("/{registrationId}/login") @Operation(summary = "OAuth 인증코드로 로그인/회원가입", description = "registrationId: google") public ResponseEntity login(@PathVariable String registrationId, @RequestParam String code) { OAuthService oAuthService = switch (registrationId) { From a398075a7bddf0a0626dc0e35ca26937ce494f76 Mon Sep 17 00:00:00 2001 From: SJ70 Date: Thu, 1 Aug 2024 20:09:25 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20oauth?= =?UTF-8?q?=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuthController.java | 3 + .../bestmoments/service/KakaoAuthService.java | 74 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/main/java/com/j9/bestmoments/service/KakaoAuthService.java diff --git a/src/main/java/com/j9/bestmoments/controller/AuthController.java b/src/main/java/com/j9/bestmoments/controller/AuthController.java index 13adc35..8438632 100644 --- a/src/main/java/com/j9/bestmoments/controller/AuthController.java +++ b/src/main/java/com/j9/bestmoments/controller/AuthController.java @@ -3,6 +3,7 @@ import com.j9.bestmoments.dto.response.LoginDto; import com.j9.bestmoments.dto.response.OAuthUserInfoDto; import com.j9.bestmoments.service.GoogleAuthService; +import com.j9.bestmoments.service.KakaoAuthService; import com.j9.bestmoments.service.OAuthService; import com.j9.bestmoments.domain.Member; import com.j9.bestmoments.service.MemberService; @@ -28,6 +29,7 @@ public class AuthController { private final GoogleAuthService googleAuthService; + private final KakaoAuthService kakaoAuthService; private final MemberService memberService; private final TokenService tokenService; @@ -36,6 +38,7 @@ public class AuthController { public ResponseEntity login(@PathVariable String registrationId, @RequestParam String code) { OAuthService oAuthService = switch (registrationId) { case "google" -> googleAuthService; + case "kakao" -> kakaoAuthService; default -> throw new OAuth2AuthenticationException("존재하지 않는 OAuth 인증 방식입니다."); }; OAuthUserInfoDto oAuthUserInfo = oAuthService.getUserInfo(code); diff --git a/src/main/java/com/j9/bestmoments/service/KakaoAuthService.java b/src/main/java/com/j9/bestmoments/service/KakaoAuthService.java new file mode 100644 index 0000000..0250e62 --- /dev/null +++ b/src/main/java/com/j9/bestmoments/service/KakaoAuthService.java @@ -0,0 +1,74 @@ +package com.j9.bestmoments.service; + +import com.fasterxml.jackson.databind.JsonNode; +import com.j9.bestmoments.dto.response.OAuthUserInfoDto; +import java.util.Collections; +import java.util.Map; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +@Service +public class KakaoAuthService implements OAuthService { + + @Value("${oauth2.kakao.client-id}") + private String clientId; + + @Value("${oauth2.kakao.redirect-uri}") + private String redirectUri; + + @Value("${oauth2.kakao.token-uri}") + private String tokenUri; + + @Value("${oauth2.kakao.userinfo-uri}") + private String userinfoUrl; + + @Override + public OAuthUserInfoDto getUserInfo(String code) { + String accessToken = getAccessToken(code); + Map attributes = getUserInfoAttributes(accessToken); + Map kakao_account = (Map) attributes.get("kakao_account"); + Map profile = (Map) kakao_account.get("profile"); + return OAuthUserInfoDto.builder() + .provider("kakao") + .id(attributes.get("id").toString()) + .name(profile.get("nickname").toString()) + .email(kakao_account.get("email").toString()) + .build(); + } + + private String getAccessToken(String code) { + MultiValueMap params = new LinkedMultiValueMap<>(); + params.put("grant_type", Collections.singletonList("authorization_code")); + params.put("client_id", Collections.singletonList(clientId)); + params.put("redirect_uri", Collections.singletonList(redirectUri)); + params.put("code", Collections.singletonList(code)); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + + HttpEntity entity = new HttpEntity(params, headers); + + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseNode = restTemplate.exchange(tokenUri, HttpMethod.POST, entity, JsonNode.class); + JsonNode accessTokenNode = responseNode.getBody(); + + return accessTokenNode.get("access_token").asText(); + } + + private Map getUserInfoAttributes(String accessToken) { + HttpHeaders headers = new HttpHeaders(); + headers.setBearerAuth(accessToken); + HttpEntity entity = new HttpEntity<>(headers); + RestTemplate restTemplate = new RestTemplate(); + return restTemplate.exchange(userinfoUrl, HttpMethod.GET, entity, Map.class).getBody(); + } + +} From c659576e057bc4de2ccd683ff752d50deb932b8e Mon Sep 17 00:00:00 2001 From: SJ70 Date: Thu, 1 Aug 2024 20:15:35 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20google=20oauth=20yml=20?= =?UTF-8?q?=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/j9/bestmoments/service/GoogleAuthService.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/j9/bestmoments/service/GoogleAuthService.java b/src/main/java/com/j9/bestmoments/service/GoogleAuthService.java index ed96f49..1eb95ea 100644 --- a/src/main/java/com/j9/bestmoments/service/GoogleAuthService.java +++ b/src/main/java/com/j9/bestmoments/service/GoogleAuthService.java @@ -18,19 +18,19 @@ @Service public class GoogleAuthService implements OAuthService { - @Value("${spring.security.oauth2.client.registration.google.client-id}") + @Value("${oauth2.google.client-id}") private String clientId; - @Value("${spring.security.oauth2.client.registration.google.client-secret}") + @Value("${oauth2.google.client-secret}") private String clientSecret; - @Value("${spring.security.oauth2.client.registration.google.redirect-uri}") + @Value("${oauth2.google.redirect-uri}") private String redirectUri; - @Value("${spring.security.oauth2.client.registration.google.token-uri}") + @Value("${oauth2.google.token-uri}") private String tokenUri; - @Value("${spring.security.oauth2.client.registration.google.userinfo-uri}") + @Value("${oauth2.google.userinfo-uri}") private String userinfoUrl; @Override From be0bbe4f4e2cb470ad5eeaf48e7b2b3e58122b15 Mon Sep 17 00:00:00 2001 From: SJ70 Date: Thu, 1 Aug 2024 20:16:32 +0900 Subject: [PATCH 4/4] =?UTF-8?q?docs:=20oauth=20=EB=A1=9C=EA=B7=B8=EC=9D=B8?= =?UTF-8?q?=20=EC=A2=85=EB=A5=98=20=EB=AA=85=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/j9/bestmoments/controller/AuthController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/j9/bestmoments/controller/AuthController.java b/src/main/java/com/j9/bestmoments/controller/AuthController.java index 8438632..2acce34 100644 --- a/src/main/java/com/j9/bestmoments/controller/AuthController.java +++ b/src/main/java/com/j9/bestmoments/controller/AuthController.java @@ -34,7 +34,7 @@ public class AuthController { private final TokenService tokenService; @GetMapping("/{registrationId}/login") - @Operation(summary = "OAuth 인증코드로 로그인/회원가입", description = "registrationId: google") + @Operation(summary = "OAuth 인증코드로 로그인/회원가입", description = "registrationId: \"google\" | \"kakao\"") public ResponseEntity login(@PathVariable String registrationId, @RequestParam String code) { OAuthService oAuthService = switch (registrationId) { case "google" -> googleAuthService;