From fa8714487cb059ff66f071353736d0eebb8e0598 Mon Sep 17 00:00:00 2001 From: "aj4941@naver.com" Date: Thu, 12 Oct 2023 19:02:04 +0900 Subject: [PATCH] =?UTF-8?q?:recycle:=20[REFACTOR]=20=EB=84=A4=EC=9D=B4?= =?UTF-8?q?=EB=B2=84=20=EC=86=8C=EC=85=9C=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/auth/constants/OAuthConstants.java | 19 +++- .../auth/controller/OAuthURIController.java | 18 ++-- .../domain/auth/response/NaverUserDto.java | 39 ++++++++ .../domain/auth/service/NaverService.java | 98 ++++++++++++++++++- 4 files changed, 163 insertions(+), 11 deletions(-) create mode 100644 src/main/java/swm_nm/morandi/domain/auth/response/NaverUserDto.java diff --git a/src/main/java/swm_nm/morandi/domain/auth/constants/OAuthConstants.java b/src/main/java/swm_nm/morandi/domain/auth/constants/OAuthConstants.java index e890b1c6..e145e0a6 100644 --- a/src/main/java/swm_nm/morandi/domain/auth/constants/OAuthConstants.java +++ b/src/main/java/swm_nm/morandi/domain/auth/constants/OAuthConstants.java @@ -18,6 +18,12 @@ public class OAuthConstants { @Value("${oauth.github.client-id}") private String githubClientId; + @Value("${oauth.naver.client-id}") + private String naverClientId; + + @Value("${oauth.naver.redirect-uri}") + private String naverRedirectUri; + @PostConstruct public void init() { @@ -42,7 +48,18 @@ public void init() GITHUB_REDIRECT_URL = "https://github.com/login/oauth/authorize?client_id="+githubClientId+"&scope=user:email"; GITHUB_REDIRECT_URL_DEV = "https://github.com/login/oauth/authorize?client_id="+githubClientId+"&scope=user:email"; - ; + + NAVER_REDIRECT_URL="https://nid.naver.com/oauth2.0/authorize?" + + "response_type=code&" + + "client_id="+ naverClientId + "&" + + "redirect_uri=" + naverRedirectUri + "&" + + "state=state_parameter_passthrough_value"; + + NAVER_REDIRECT_URL_DEV="https://nid.naver.com/oauth2.0/authorize?" + + "response_type=code&" + + "client_id="+ naverClientId + "&" + + "redirect_uri=" + naverRedirectUri + "&" + + "state=state_parameter_passthrough_value"; } public String GITHUB_REDIRECT_URL; diff --git a/src/main/java/swm_nm/morandi/domain/auth/controller/OAuthURIController.java b/src/main/java/swm_nm/morandi/domain/auth/controller/OAuthURIController.java index 1d24d6d2..84358eb7 100644 --- a/src/main/java/swm_nm/morandi/domain/auth/controller/OAuthURIController.java +++ b/src/main/java/swm_nm/morandi/domain/auth/controller/OAuthURIController.java @@ -17,7 +17,6 @@ public class OAuthURIController { @ResponseBody @GetMapping("/google") public String googleRedirect() { - //이것도 service로 빼기 return oAuthConstants.GOOGLE_REDIRECT_URL; @@ -26,22 +25,27 @@ public String googleRedirect() { //백엔드 개발 시 사용하는 API @GetMapping("/google/dev") public String googleRedirectforDevelop() { - return "redirect:" + oAuthConstants.GOOGLE_REDIRECT_URL_DEV; } - @ResponseBody @GetMapping("/github") - public String githubRedirect() - { + public String githubRedirect() { return oAuthConstants.GITHUB_REDIRECT_URL; } @GetMapping("/github/dev") - public String githubRedirectforDevelop() - { + public String githubRedirectforDevelop() { return oAuthConstants.GITHUB_REDIRECT_URL; } + @ResponseBody + @GetMapping("/naver") + public String naverRedirect() { + return oAuthConstants.NAVER_REDIRECT_URL; + } + @GetMapping("/naver/dev") + public String naverRedirectforDevelop() { + return oAuthConstants.NAVER_REDIRECT_URL_DEV; + } } \ No newline at end of file diff --git a/src/main/java/swm_nm/morandi/domain/auth/response/NaverUserDto.java b/src/main/java/swm_nm/morandi/domain/auth/response/NaverUserDto.java new file mode 100644 index 00000000..aff0504a --- /dev/null +++ b/src/main/java/swm_nm/morandi/domain/auth/response/NaverUserDto.java @@ -0,0 +1,39 @@ +package swm_nm.morandi.domain.auth.response; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; +import swm_nm.morandi.domain.member.entity.SocialType; + +@Getter +@Setter +@Builder +public class NaverUserDto implements UserDto { + private String id; + private String email; + private String profileImage; + private SocialType type; + @Override + public SocialType getType() { + return type; + } + + @Override + public String getEmail() { + return email; + } + + @Override + public String getPicture() { + return profileImage; + } + + @Override + public String getIntroduceInfo() { + return null; + } + @Override + public String getGithubUrl() { + return null; + } +} diff --git a/src/main/java/swm_nm/morandi/domain/auth/service/NaverService.java b/src/main/java/swm_nm/morandi/domain/auth/service/NaverService.java index bf85464d..283156cb 100644 --- a/src/main/java/swm_nm/morandi/domain/auth/service/NaverService.java +++ b/src/main/java/swm_nm/morandi/domain/auth/service/NaverService.java @@ -1,10 +1,46 @@ package swm_nm.morandi.domain.auth.service; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.lettuce.core.ScriptOutputType; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.hibernate.internal.util.StringHelper; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.*; import org.springframework.stereotype.Service; -import swm_nm.morandi.domain.auth.response.UserDto; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; +import swm_nm.morandi.domain.auth.constants.OAuthConstants; +import swm_nm.morandi.domain.auth.response.*; +import swm_nm.morandi.domain.member.entity.SocialType; +import swm_nm.morandi.global.exception.MorandiException; +import swm_nm.morandi.global.exception.errorcode.AuthErrorCode; + +import java.util.Arrays; +import java.util.List; @Service +@Slf4j +@RequiredArgsConstructor public class NaverService implements OAuthService { + + @Value("${oauth.naver.client-id}") + private String naverClientId; + + @Value("${oauth.naver.client-secret}") + private String naverClientSecret; + + @Value("${oauth.naver.redirect-uri}") + private String naverClientRedirectUri; + + private final RestTemplate restTemplate; + private final ObjectMapper objectMapper; @Override public String getType() { return "naver"; @@ -12,11 +48,67 @@ public String getType() { @Override public String getAccessToken(String authorization_code, Boolean isDev) { - return null; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + + MultiValueMap params = new LinkedMultiValueMap<>(); + params.add("grant_type", "authorization_code"); + params.add("code", authorization_code); + params.add("client_id", naverClientId); + params.add("client_secret", naverClientSecret); + + HttpEntity> requestEntity = new HttpEntity<>(params, headers); + ResponseEntity responseEntity = null; + + try { + responseEntity = restTemplate.postForEntity("https://nid.naver.com/oauth2.0/token", requestEntity, String.class); + String accessToken = objectMapper.readValue(responseEntity.getBody(), TokenResponseDto.class).getAccess_token(); + return accessToken; + } catch (RestClientException e) { + log.error("params = {}, requestEntity = {}", params, requestEntity); + throw new MorandiException(AuthErrorCode.SSO_SERVER_ERROR); + } catch (NullPointerException e) { + log.error("params = {}, requestEntity = {}, responseEntity = {}", params, requestEntity, responseEntity); + throw new MorandiException(AuthErrorCode.SSO_ACCESS_TOKEN); + } catch (JsonProcessingException e) { + throw new MorandiException(AuthErrorCode.SSO_ACCESS_TOKEN); + } } @Override public UserDto getMemberInfo(String accessToken) { - return null; + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization","Bearer "+ accessToken); + HttpEntity> requestEntity = new HttpEntity(headers); + + ResponseEntity responseEntity = null; + try { + // 유저 정보 가져오기 + System.out.println("NaverService.getMemberInfo"); + responseEntity = restTemplate.exchange(OAuthConstants.NAVER_USERINFO_REQUEST_URL, HttpMethod.GET, requestEntity, String.class); + String responseBody = responseEntity.getBody(); + + // JSON 문자열을 JsonNode로 파싱 + JsonNode jsonNode = objectMapper.readTree(responseBody); + + // "response" 부분을 가져옴 + JsonNode responseNode = jsonNode.get("response"); + NaverUserDto naverUserDto = NaverUserDto.builder() + .id(responseNode.get("id").asText()) + .email(responseNode.get("email").asText()) + .profileImage(responseNode.get("profile_image").asText()) + .build(); + + return naverUserDto; + } + catch (RestClientException e){ + log.error("requestEntity = {}",requestEntity); + throw new MorandiException(AuthErrorCode.SSO_SERVER_ERROR); + } + catch (JsonProcessingException | NullPointerException e) + { + log.error("responseEntity = {}",responseEntity); + throw new MorandiException(AuthErrorCode.SSO_USERINFO); + } } }