Skip to content

Commit 56118df

Browse files
committed
Merge branch 'develop'
2 parents df817eb + 098f1c2 commit 56118df

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/main/java/inha/gdgoc/domain/auth/controller/AuthController.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import inha.gdgoc.domain.auth.service.RefreshTokenService;
2626
import inha.gdgoc.domain.user.entity.User;
2727
import inha.gdgoc.domain.user.repository.UserRepository;
28+
import inha.gdgoc.global.config.jwt.TokenProvider;
2829
import inha.gdgoc.global.dto.response.ApiResponse;
2930
import jakarta.servlet.http.HttpServletResponse;
3031
import java.security.InvalidKeyException;
@@ -34,6 +35,7 @@
3435
import lombok.RequiredArgsConstructor;
3536
import lombok.extern.slf4j.Slf4j;
3637
import org.springframework.http.ResponseEntity;
38+
import org.springframework.security.access.prepost.PreAuthorize;
3739
import org.springframework.security.core.Authentication;
3840
import org.springframework.security.core.context.SecurityContextHolder;
3941
import org.springframework.web.bind.annotation.CookieValue;
@@ -99,18 +101,26 @@ public ResponseEntity<ApiResponse<LoginResponse, Void>> login(
99101
}
100102

101103
@PostMapping("/logout")
104+
@PreAuthorize("isAuthenticated()")
102105
public ResponseEntity<ApiResponse<Void, Void>> logout() {
103106
// TODO 서비스로 넘기기
104107
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
105108

106-
if (authentication == null || !authentication.isAuthenticated()) {
109+
// 1) 익명 방어
110+
if (authentication == null
111+
|| !authentication.isAuthenticated()
112+
|| "anonymousUser".equals(authentication.getName())) {
107113
throw new AuthException(UNAUTHORIZED_USER);
108114
}
109115

110-
String email = authentication.getName();
111-
User user = userRepository.findByEmail(email)
112-
.orElseThrow(() -> new AuthException(USER_NOT_FOUND));
113-
Long userId = user.getId();
116+
// 2) principal 캐스팅해서 확정적으로 userId/email 사용
117+
Object principal = authentication.getPrincipal();
118+
if (!(principal instanceof TokenProvider.CustomUserDetails userDetails)) {
119+
throw new AuthException(UNAUTHORIZED_USER);
120+
}
121+
122+
Long userId = userDetails.getUserId();
123+
String email = userDetails.getUsername();
114124

115125
log.info("로그아웃 시도: 사용자 ID: {}, 이메일: {}", userId, email);
116126

src/main/java/inha/gdgoc/global/security/SecurityConfig.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3939
.httpBasic(AbstractHttpConfigurer::disable)
4040
.authorizeHttpRequests(auth -> auth
4141
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
42+
.requestMatchers("/api/v1/auth/logout").authenticated()
4243
.requestMatchers(
4344
"/swagger-ui/**",
4445
"/v3/api-docs/**",
@@ -89,10 +90,11 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
8990
public CorsConfigurationSource corsConfigurationSource() {
9091
CorsConfiguration config = new CorsConfiguration();
9192
config.setAllowedOrigins(List.of(
92-
"http://localhost:3000",
93-
"https://gdgocinha.com",
94-
"https://www.gdgocinha.com",
95-
"https://typing-game-alpha-umber.vercel.app"
93+
"http://localhost:3000",
94+
"https://gdgocinha.com",
95+
"https://dev.gdgocinha.com",
96+
"https://www.gdgocinha.com",
97+
"https://typing-game-alpha-umber.vercel.app"
9698
));
9799
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"));
98100
config.setAllowedHeaders(

src/main/java/inha/gdgoc/global/security/TokenAuthenticationFilter.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
2525
protected boolean shouldNotFilter(HttpServletRequest request) {
2626
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
2727
return true;
28-
}
28+
}
2929

3030
String uri = request.getRequestURI();
31+
32+
if (uri.equals("/api/v1/auth/logout")) return false;
33+
3134
return uri.startsWith("/v3/api-docs")
3235
|| uri.startsWith("/swagger-ui")
3336
|| uri.equals("/swagger-ui.html")
34-
|| uri.startsWith("/api/v1/auth/")
37+
|| uri.startsWith("/api/v1/auth/refresh")
38+
|| uri.startsWith("/api/v1/auth/login")
39+
|| uri.startsWith("/api/v1/auth/oauth2/google/callback")
40+
|| uri.startsWith("/api/v1/auth/password-reset/request")
41+
|| uri.startsWith("/api/v1/auth/password-reset/verify")
42+
|| uri.startsWith("/api/v1/auth/password-reset/confirm")
3543
|| uri.startsWith("/api/v1/test/")
3644
|| uri.startsWith("/api/v1/game/")
3745
|| uri.startsWith("/api/v1/apply/")

0 commit comments

Comments
 (0)