jpablog : kakao login api - access token

This commit is contained in:
kim
2021-02-01 21:23:48 +09:00
parent 2d73e60fb9
commit bbb0a43dd5
3 changed files with 30 additions and 3 deletions

View File

@@ -1,17 +1,18 @@
package com.example.jpablog.controller.api;
import com.example.jpablog.config.auth.PrincipalDetail;
import com.example.jpablog.config.auth.PrincipalDetailService;
import com.example.jpablog.dto.ResponseDto;
import com.example.jpablog.model.User;
import com.example.jpablog.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.*;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.security.Principal;
@@ -41,6 +42,31 @@ public class UserApiController {
return new ResponseDto<>(-1, HttpStatus.BAD_REQUEST.value());
}
@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");
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;
}
/*// 기본 로그인
@PostMapping("/user/login")
public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@@ -13,6 +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>
</form>
</div>