jpablog : kakao login api - get user profile
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package com.example.jpablog.controller.api;
|
||||
|
||||
import com.example.jpablog.dto.ResponseDto;
|
||||
import com.example.jpablog.model.KakaoProfile;
|
||||
import com.example.jpablog.model.OAuthToken;
|
||||
import com.example.jpablog.model.User;
|
||||
import com.example.jpablog.service.KakaoLogin;
|
||||
import com.example.jpablog.service.UserService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
@@ -45,27 +50,12 @@ public class UserApiController {
|
||||
@GetMapping("/auth/kakao/callback")
|
||||
public String kakaoCallback(String code) {
|
||||
// Retrofit2, OkHttp, RestTemplate, HttpsURLConnection 등이 있음
|
||||
RestTemplate rt = new RestTemplate();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
|
||||
KakaoLogin kakaoLogin = new KakaoLogin();
|
||||
|
||||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
||||
params.add("grant_type", "authorization_code");
|
||||
params.add("client_id", "e40b81f26358aa250b10a59dc8f3aa62");
|
||||
params.add("redirect_uri", "http://localhost:8080/auth/kakao/callback");
|
||||
params.add("code", code);
|
||||
|
||||
HttpEntity<MultiValueMap<String, String>> kakaoTokenRequest =
|
||||
new HttpEntity<>(params, headers);
|
||||
|
||||
ResponseEntity<String> response = rt.exchange(
|
||||
"https://kauth.kakao.com/oauth/token",
|
||||
HttpMethod.POST,
|
||||
kakaoTokenRequest,
|
||||
String.class
|
||||
);
|
||||
|
||||
return "카카오 토큰 요청 완료" + response;
|
||||
OAuthToken token = kakaoLogin.getCode(code);
|
||||
KakaoProfile kakaoProfile = kakaoLogin.getKakaoProfile(token);
|
||||
System.out.println(kakaoProfile);
|
||||
return "ㅎㅇ";
|
||||
}
|
||||
/*// 기본 로그인
|
||||
@PostMapping("/user/login")
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.jpablog.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class KakaoProfile {
|
||||
public Integer id;
|
||||
public String connected_at;
|
||||
public Properties properties;
|
||||
public KakaoAccount kakao_account;
|
||||
}
|
||||
@Data
|
||||
class Properties {
|
||||
public String nickname;
|
||||
public String profile_image;
|
||||
public String thumbnail_image;
|
||||
}
|
||||
|
||||
@Data
|
||||
class KakaoAccount {
|
||||
public Boolean profile_needs_agreement;
|
||||
public Profile profile;
|
||||
public Boolean has_email;
|
||||
public Boolean email_needs_agreement;
|
||||
public Boolean is_email_valid;
|
||||
public Boolean is_email_verified;
|
||||
public String email;
|
||||
}
|
||||
|
||||
@Data
|
||||
class Profile {
|
||||
public String nickname;
|
||||
public String thumbnail_image_url;
|
||||
public String profile_image_url;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.jpablog.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class OAuthToken {
|
||||
private String access_token;
|
||||
private String token_type;
|
||||
private String refresh_token;
|
||||
private int expires_in;
|
||||
private String scope;
|
||||
private int refresh_token_expires_in;
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.example.jpablog.service;
|
||||
|
||||
import com.example.jpablog.model.KakaoProfile;
|
||||
import com.example.jpablog.model.OAuthToken;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
public class KakaoLogin {
|
||||
|
||||
public OAuthToken getCode(String code) {
|
||||
RestTemplate rt = new RestTemplate();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
|
||||
|
||||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
||||
params.add("grant_type", "authorization_code");
|
||||
params.add("client_id", "e40b81f26358aa250b10a59dc8f3aa62");
|
||||
params.add("redirect_uri", "http://localhost:8080/auth/kakao/callback");
|
||||
params.add("code", code);
|
||||
|
||||
HttpEntity<MultiValueMap<String, String>> kakaoTokenRequest =
|
||||
new HttpEntity<>(params, headers);
|
||||
|
||||
ResponseEntity<String> response = rt.exchange(
|
||||
"https://kauth.kakao.com/oauth/token",
|
||||
HttpMethod.POST,
|
||||
kakaoTokenRequest,
|
||||
String.class
|
||||
);
|
||||
// Gson, Json Simple, ObjectMapper
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
OAuthToken oAuthToken = null;
|
||||
try {
|
||||
oAuthToken = objectMapper.readValue(response.getBody(), OAuthToken.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return oAuthToken;
|
||||
}
|
||||
|
||||
public KakaoProfile getKakaoProfile(OAuthToken token) {
|
||||
RestTemplate rt = new RestTemplate();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Authorization", "Bearer " + token.getAccess_token());
|
||||
headers.add("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
|
||||
|
||||
HttpEntity<MultiValueMap<String, String>> kakaoProfileRequest =
|
||||
new HttpEntity<>(headers);
|
||||
|
||||
ResponseEntity<String> response = rt.exchange(
|
||||
"https://kapi.kakao.com/v2/user/me",
|
||||
HttpMethod.POST,
|
||||
kakaoProfileRequest,
|
||||
String.class
|
||||
);
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
KakaoProfile kakaoProfile = null;
|
||||
try {
|
||||
kakaoProfile = objectMapper.readValue(response.getBody(), KakaoProfile.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return kakaoProfile;
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@
|
||||
<input name="password" type="password" class="form-control" placeholder="Enter password" id="password">
|
||||
</div>
|
||||
<button id="btn-login" type="submit" class="btn btn-primary">로그인</button>
|
||||
<a href="https://kauth.kakao.com/oauth/authorize?client_id=e40b81f26358aa250b10a59dc8f3aa62&redirect_uri=http://localhost:8080/auth/kakao/callback&response_type=code&scope=account_email"><img height="38px" src="/images/kakao_login_medium.png" alt="kakao_login_button"/> </a>
|
||||
<a href="https://kauth.kakao.com/oauth/authorize?client_id=e40b81f26358aa250b10a59dc8f3aa62&redirect_uri=http://localhost:8080/auth/kakao/callback&response_type=code"><img height="38px" src="/images/kakao_login_medium.png" alt="kakao_login_button"/> </a>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user