Skip to content

Commit

Permalink
Merge pull request #451 from SWM-NM/dev
Browse files Browse the repository at this point in the history
[REFACTOR] 네이버 소셜로그인 추가
  • Loading branch information
aj4941 authored Oct 12, 2023
2 parents c458333 + 27d0bc4 commit 3572a1f
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class OAuthURIController {
@ResponseBody
@GetMapping("/google")
public String googleRedirect() {

//이것도 service로 빼기
return oAuthConstants.GOOGLE_REDIRECT_URL;

Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
98 changes: 95 additions & 3 deletions src/main/java/swm_nm/morandi/domain/auth/service/NaverService.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,114 @@
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";
}

@Override
public String getAccessToken(String authorization_code, Boolean isDev) {
return null;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

MultiValueMap<String, String> 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<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> 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<MultiValueMap<String, String>> requestEntity = new HttpEntity(headers);

ResponseEntity<String> 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);
}
}
}

0 comments on commit 3572a1f

Please sign in to comment.