jpablog : kakao login api - access token
This commit is contained in:
@@ -1,17 +1,18 @@
|
|||||||
package com.example.jpablog.controller.api;
|
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.dto.ResponseDto;
|
||||||
import com.example.jpablog.model.User;
|
import com.example.jpablog.model.User;
|
||||||
import com.example.jpablog.service.UserService;
|
import com.example.jpablog.service.UserService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.*;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
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.bind.annotation.*;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
|
|
||||||
@@ -41,6 +42,31 @@ public class UserApiController {
|
|||||||
return new ResponseDto<>(-1, HttpStatus.BAD_REQUEST.value());
|
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")
|
@PostMapping("/user/login")
|
||||||
public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) {
|
public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) {
|
||||||
|
|||||||
BIN
jpablog/src/main/resources/static/images/kakao_login_medium.png
Normal file
BIN
jpablog/src/main/resources/static/images/kakao_login_medium.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
@@ -13,6 +13,7 @@
|
|||||||
<input name="password" type="password" class="form-control" placeholder="Enter password" id="password">
|
<input name="password" type="password" class="form-control" placeholder="Enter password" id="password">
|
||||||
</div>
|
</div>
|
||||||
<button id="btn-login" type="submit" class="btn btn-primary">로그인</button>
|
<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>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user