Skip to content

Commit

Permalink
feat: 카카오 oauth 로그인
Browse files Browse the repository at this point in the history
  • Loading branch information
SJ70 committed Aug 1, 2024
1 parent 58401ec commit a398075
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,7 @@
public class AuthController {

private final GoogleAuthService googleAuthService;
private final KakaoAuthService kakaoAuthService;
private final MemberService memberService;
private final TokenService tokenService;

Expand All @@ -36,6 +38,7 @@ public class AuthController {
public ResponseEntity<LoginDto> 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);
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/com/j9/bestmoments/service/KakaoAuthService.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<JsonNode> 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<String> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
return restTemplate.exchange(userinfoUrl, HttpMethod.GET, entity, Map.class).getBody();
}

}

0 comments on commit a398075

Please sign in to comment.