-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f747ba
commit 421262a
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/java/yapp/allround3/auth/oauth/client/KakaoUserInfoClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package yapp.allround3.auth.oauth.client; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
import yapp.allround3.common.exception.CustomException; | ||
|
||
@Component("KAKAO") | ||
@Slf4j | ||
public class KakaoUserInfoClient implements OauthUserInfoClient { | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
public OauthUserInfo getUserInfo(String accessToken) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Authorization", "Bearer " + accessToken); | ||
headers.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); | ||
|
||
HttpEntity<MultiValueMap<String, String>> kakaoUserInfoRequest = new HttpEntity<>(headers); | ||
RestTemplate rt = new RestTemplate(); | ||
ResponseEntity<String> response = rt.exchange( | ||
"https://kapi.kakao.com/v2/user/me", | ||
HttpMethod.POST, | ||
kakaoUserInfoRequest, | ||
String.class | ||
); | ||
|
||
String body = response.getBody(); | ||
try { | ||
JsonNode jsonNode = objectMapper.readTree(body); | ||
String id = jsonNode.get("id").asText(); | ||
String nickname = jsonNode.get("properties").get("nickname").asText(); | ||
String email = jsonNode.get("kakao_account").get("email").asText(); | ||
String imageUrl = jsonNode.get("properties").get("profile_image").asText(); | ||
|
||
return OauthUserInfo.builder() | ||
.oauthId(id) | ||
.nickname(nickname) | ||
.email(email) | ||
.imageUrl(imageUrl) | ||
.build(); | ||
} catch (Exception e) { | ||
throw new CustomException("Oauth 회원 정보 조회 에러"); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/yapp/allround3/auth/oauth/client/OauthUserInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package yapp.allround3.auth.oauth.client; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import yapp.allround3.auth.oauth.Provider; | ||
import yapp.allround3.member.domain.Member; | ||
|
||
@Getter | ||
@Builder | ||
public class OauthUserInfo { | ||
private final String nickname; | ||
private final String email; | ||
private final String imageUrl; | ||
private final Provider provider; | ||
private final String oauthId; | ||
|
||
public Member toMember() { | ||
return Member.builder() | ||
.name(nickname) | ||
.email(email) | ||
.imageUrl(imageUrl) | ||
.provider(provider) | ||
.oauthId(oauthId) | ||
.build(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/yapp/allround3/auth/oauth/client/OauthUserInfoClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package yapp.allround3.auth.oauth.client; | ||
|
||
public interface OauthUserInfoClient { | ||
OauthUserInfo getUserInfo(String accessToken); | ||
} |