Skip to content

Commit b73a70e

Browse files
authored
Merge pull request #109 from M7-TAVE/feat/105-logout
feat: 로그아웃 api 수정 추가 #105
2 parents 738cf69 + fabee65 commit b73a70e

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

src/main/java/com/example/travelbag/domain/member/controller/api/auth/status/AuthController.java

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package com.example.travelbag.domain.member.controller.api.auth.status;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatus;
35
import org.springframework.http.ResponseEntity;
46
import org.springframework.security.core.Authentication;
7+
import org.springframework.security.core.context.SecurityContextHolder;
8+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
9+
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
10+
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
511
import org.springframework.security.oauth2.core.user.OAuth2User;
612
import org.springframework.web.bind.annotation.*;
13+
import org.springframework.web.client.HttpStatusCodeException;
714
import org.springframework.web.client.RestTemplate;
8-
15+
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
918
import java.util.Map;
19+
20+
1021
@RestController
1122
@RequestMapping("/api/auth")
1223
public class AuthController {
@@ -52,30 +63,77 @@ public ResponseEntity<Map<String, Object>> getAuthStatus(Authentication authenti
5263
}
5364
}
5465

55-
// 새로운 로그아웃 API
66+
@GetMapping("/token")
67+
public ResponseEntity<Map<String, String>> getAccessToken(Authentication authentication,
68+
@Autowired OAuth2AuthorizedClientService authorizedClientService) {
69+
if (authentication == null) {
70+
return ResponseEntity.status(401).body(Map.of("error", "User not authenticated"));
71+
}
72+
73+
OAuth2AuthenticationToken oauthToken = (OAuth2AuthenticationToken) authentication;
74+
String clientRegistrationId = oauthToken.getAuthorizedClientRegistrationId();
75+
76+
OAuth2AuthorizedClient authorizedClient =
77+
authorizedClientService.loadAuthorizedClient(clientRegistrationId, oauthToken.getName());
78+
79+
if (authorizedClient == null || authorizedClient.getAccessToken() == null) {
80+
return ResponseEntity.status(500).body(Map.of("error", "Access token not available"));
81+
}
82+
83+
String kakaoAccessToken = authorizedClient.getAccessToken().getTokenValue();
84+
return ResponseEntity.ok(Map.of("accessToken", kakaoAccessToken));
85+
}
86+
5687
@PostMapping("/logout")
57-
public ResponseEntity<Map<String, Object>> logout(@RequestHeader("Authorization") String accessToken) {
88+
public ResponseEntity<Map<String, Object>> logout(@RequestHeader("Authorization") String authorizationHeader) {
89+
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
90+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Map.of(
91+
"error", "Authorization header is missing or invalid"
92+
));
93+
}
94+
95+
// Bearer 접두어 제거 후 Access Token 추출
96+
String accessToken = authorizationHeader.replace("Bearer ", "").trim();
5897
String kakaoLogoutUrl = "https://kapi.kakao.com/v1/user/logout";
5998

6099
try {
61100
// 카카오 로그아웃 API 호출
62101
RestTemplate restTemplate = new RestTemplate();
63102
var headers = new org.springframework.http.HttpHeaders();
64103
headers.set("Authorization", "Bearer " + accessToken);
104+
65105
var request = new org.springframework.http.HttpEntity<>(headers);
106+
var response = restTemplate.postForEntity(kakaoLogoutUrl, request, String.class);
66107

67-
restTemplate.postForEntity(kakaoLogoutUrl, request, String.class);
108+
// 로그아웃 성공 여부 확인
109+
if (response.getStatusCode().is2xxSuccessful()) {
110+
// Spring Security 세션 초기화
111+
SecurityContextHolder.clearContext();
112+
System.out.println("Kakao logout successful. Token: " + accessToken);
68113

69-
// 로그아웃 성공 응답
70-
return ResponseEntity.ok(Map.of(
71-
"message", "Successfully logged out"
114+
return ResponseEntity.ok(Map.of(
115+
"message", "Successfully logged out"
116+
));
117+
} else {
118+
System.out.println("Kakao logout failed with status: " + response.getStatusCode());
119+
return ResponseEntity.status(response.getStatusCode()).body(Map.of(
120+
"error", "Kakao logout failed"
121+
));
122+
}
123+
} catch (HttpStatusCodeException ex) {
124+
// 카카오 API에서 반환된 HTTP 상태 코드와 응답 메시지 처리
125+
System.out.println("Error during Kakao logout. Status: " + ex.getStatusCode() + ", Response: " + ex.getResponseBodyAsString());
126+
return ResponseEntity.status(ex.getStatusCode()).body(Map.of(
127+
"error", ex.getResponseBodyAsString()
72128
));
73129
} catch (Exception e) {
74-
System.out.println("Error in /api/auth/logout: " + e.getMessage());
130+
// 일반 예외 처리
131+
System.out.println("Unexpected error during Kakao logout: " + e.getMessage());
75132
e.printStackTrace();
76-
return ResponseEntity.status(500).body(Map.of(
77-
"error", e.getMessage()
133+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of(
134+
"error", "Unexpected error occurred"
78135
));
79136
}
80137
}
138+
81139
}

src/main/java/com/example/travelbag/domain/member/service/CustomOAuth2UserService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService {
2323
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
2424
OAuth2User oAuth2User = super.loadUser(userRequest);
2525

26+
String accessToken = userRequest.getAccessToken().getTokenValue();
27+
System.out.println("Access Token !?!?!?: " + accessToken); // 디버깅용 로그
28+
2629
System.out.println("OAuth2User attributes: " + oAuth2User.getAttributes()); // 디버깅용 로그
2730

2831
Map<String, Object> attributes = oAuth2User.getAttributes();

0 commit comments

Comments
 (0)