@GetMapping("/naver/unlink")
public void logout(){
log.info("GET/naver/unlink...");
// 1. ๋ค์ด๋ฒ ์ฐ๋ ํด์ ์์ฒญ URL
String url = "<https://nid.naver.com/oauth2.0/token>";
// 2. ์์ฒญ ํค๋ ์ค์ (Content-Type ๋ช
์ ํ์)
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 3. ์์ฒญ ํ๋ผ๋ฏธํฐ ์ค์
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("client_id", NAVER_CLIENT_ID); // ์ฑ ๋ฑ๋ก ์ ๋ฐ์ Client ID
params.add("client_secret", NAVER_CLIENT_SECRET); // Client Secret
params.add("access_token", this.naverTokenResponse.getAccess_token()); // ์ ํจํ access_token
params.add("grant_type", "delete"); // ์์ฒญ ํ์
์ ๋ฐ๋์ delete
// 4. ์์ฒญ ์ํฐํฐ ๊ตฌ์ฑ
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, header);
// 5. ์์ฒญ ์ ์ก ๋ฐ ์๋ต ์์
RestTemplate rt = new RestTemplate();
ResponseEntity<String> response = rt.exchange(url, HttpMethod.POST, entity, String.class);
// 6. ๊ฒฐ๊ณผ ์ถ๋ ฅ
System.out.println(response.getBody());
// ์์ ์๋ต:
// {"result":"success","access_token":"AAAAOVGk7..."}
}
@GetMapping("/naver/logout")
public String disConnect(){
log.info("GET/naver/logout...");
return "redirect:<https://nid.naver.com/nidlogin.logout?returl=https://www.naver.com/>";
}
์ API๋ ๋ธ๋ผ์ฐ์ ์์ ๋ค์ด๋ฒ ์ธ์ ๋ง ๋ง๋ฃ์ํด
access_token ํ๊ธฐ์๋ ๋ฌด๊ดํ๋ฏ๋ก, ๋ณด์์ ์ํด /unlink์ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํจ
// ์์ธ์ค ํ ํฐ ์๋ต DTO
@Data
private static class NaverTokenResponse {
public String access_token;
public String refresh_token;
public String token_type;
public String expires_in;
}
// ์ฌ์ฉ์ ์ ๋ณด ์๋ต DTO
@Data
private static class Response {
public String id;
public String profile_image;
public String email;
public String name;
}
@Data
private static class NaverProfileResponse {
public String resultcode;
public String message;
public Response response;
}
{
"result": "success",
"access_token": "AAAAOVGk7cIRQXi1hmVeVYXrP..."
}
/unlink
๋ฅผ ํธ์ถํด์ผ ํจRestTemplate
์ฌ์ฉ ์ ๋ฐ๋์ Content-Type: application/x-www-form-urlencoded
์ค์ ํ์"result": "success"
์ผ ๊ฒฝ์ฐ ์ ์ ์ฒ๋ฆฌ๋จ