Skip to content

Commit 67dc3b0

Browse files
authored
Merge pull request #120 from M7-TAVE/fix/101-kakao-issue
fix: 카카오 로그아웃 해결 #101
2 parents d586975 + c09942b commit 67dc3b0

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

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

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

3+
import jakarta.servlet.http.HttpServletRequest;
4+
import jakarta.servlet.http.HttpServletResponse;
35
import org.springframework.http.ResponseEntity;
46
import org.springframework.security.core.Authentication;
7+
import org.springframework.security.core.context.SecurityContextHolder;
58
import org.springframework.security.oauth2.core.user.OAuth2User;
9+
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
610
import org.springframework.web.bind.annotation.GetMapping;
711
import org.springframework.web.bind.annotation.RequestMapping;
812
import org.springframework.web.bind.annotation.RestController;
@@ -103,21 +107,33 @@ public ResponseEntity<Map<String, Object>> getAuthStatus(Authentication authenti
103107

104108

105109
@PostMapping("/logout")
106-
public ResponseEntity<Map<String, Object>> logout() {
107-
// 로그 출력
108-
System.out.println("Logout API called");
110+
public ResponseEntity<Map<String, Object>> logout(HttpServletRequest request, HttpServletResponse response) {
111+
System.out.println("=== Logout Process Started ===");
112+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
113+
114+
System.out.println("Current Authentication: " +
115+
(authentication != null ? authentication.getName() : "anonymous"));
116+
System.out.println("Session ID: " + request.getSession(false) != null ?
117+
request.getSession().getId() : "no session");
118+
119+
if (authentication != null) {
120+
new SecurityContextLogoutHandler().logout(request, response, authentication);
121+
SecurityContextHolder.clearContext();
122+
System.out.println("Logout successful - session invalidated");
123+
} else {
124+
System.out.println("No authentication found to logout");
125+
}
109126

110-
// 수동으로 인증 상태 초기화
111127
Map<String, Object> authInfo = Map.of(
112128
"isAuthenticated", false,
113129
"kakaoId", null,
114130
"email", null,
115131
"nickname", null
116132
);
117133

118-
// 성공 응답 반환
134+
System.out.println("=== Logout Process Completed ===");
119135
return ResponseEntity.ok(Map.of(
120-
"message", "Logout API called successfully",
136+
"message", "Logged out successfully",
121137
"authInfo", authInfo
122138
));
123139
}

src/main/java/com/example/travelbag/global/config/SecurityConfig.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.web.cors.CorsConfigurationSource;
1818
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
1919
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
20+
import org.springframework.http.HttpStatus;
2021

2122
import java.io.IOException;
2223
import java.util.List;
@@ -32,13 +33,8 @@ public class SecurityConfig {
3233

3334
@Bean
3435
public AuthenticationSuccessHandler oauth2AuthenticationSuccessHandler() {
35-
return new AuthenticationSuccessHandler() {
36-
@Override
37-
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
38-
Authentication authentication) throws IOException, ServletException {
39-
// Vite 프론트엔드로 리다이렉트
40-
response.sendRedirect(front_url);
41-
}
36+
return (request, response, authentication) -> {
37+
response.sendRedirect(front_url);
4238
};
4339
}
4440

@@ -47,18 +43,7 @@ public CorsConfigurationSource corsConfigurationSource() {
4743
CorsConfiguration configuration = new CorsConfiguration();
4844
configuration.setAllowedOrigins(List.of(front_url));
4945
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
50-
configuration.setAllowedHeaders(List.of(
51-
"Authorization",
52-
"Cache-Control",
53-
"Content-Type",
54-
"Origin",
55-
"Accept",
56-
"Referer",
57-
"User-Agent",
58-
"Access-Control-Allow-Origin",
59-
"*"
60-
));
61-
configuration.setExposedHeaders(List.of("Authorization", "Content-Type"));
46+
configuration.setAllowedHeaders(List.of("*")); // 모든 헤더 허용으로 단순화
6247
configuration.setAllowCredentials(true);
6348
configuration.setMaxAge(3600L);
6449

@@ -94,12 +79,24 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
9479
)
9580
.successHandler(oauth2AuthenticationSuccessHandler())
9681
)
82+
.sessionManagement(session -> session
83+
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
84+
.maximumSessions(1)
85+
.expiredUrl(front_url + "/login")
86+
)
9787
.logout(logout -> logout
98-
.logoutSuccessUrl(front_url + "/login")
88+
.logoutUrl("/api/auth/logout")
89+
.logoutSuccessHandler((request, response, authentication) -> {
90+
response.setStatus(HttpStatus.OK.value());
91+
response.setContentType("application/json;charset=UTF-8");
92+
response.getWriter().write("{\"message\":\"Logout successful\",\"status\":\"success\"}");
93+
})
9994
.invalidateHttpSession(true)
100-
.deleteCookies("JSESSIONID")
95+
.clearAuthentication(true)
96+
.deleteCookies("JSESSIONID", "SESSION") // Redis 세션 쿠키 이름 수정
97+
.permitAll()
10198
);
10299

103100
return http.build();
104101
}
105-
}
102+
}

0 commit comments

Comments
 (0)