hmkim 06-16
250616 김혜민 사용자 ID조회 에러 수정
@ece82a6d8e2f7e64979c88888bbe8951733d5cfd
src/main/java/com/takensoft/common/service/VerificationService.java
--- src/main/java/com/takensoft/common/service/VerificationService.java
+++ src/main/java/com/takensoft/common/service/VerificationService.java
@@ -2,10 +2,16 @@
 
 import com.takensoft.cms.mber.vo.MberVO;
 import com.takensoft.common.exception.CustomAccessDeniedException;
+import com.takensoft.common.util.JWTUtil;
 import lombok.RequiredArgsConstructor;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Service;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpSession;
+import jakarta.servlet.http.Cookie;
 
 /**
  * @author takensoft
@@ -19,6 +25,9 @@
 @Service("authorizationService")
 @RequiredArgsConstructor
 public class VerificationService {
+
+    private final JWTUtil jwtUtil;
+
 
     /**
      * @return 현재 인증된 사용자 정보
@@ -78,23 +87,69 @@
      */
     public String getCurrentUserId() {
         String userId = null;
-        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
 
+        try {
+            // 1. SecurityContext에서 시도
+            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
 
-        // 디버깅용 로그 추가
-        System.out.println("Authentication: " + authentication);
-        if(authentication != null) {
-            System.out.println("Is Authenticated: " + authentication.isAuthenticated());
-            System.out.println("Principal Type: " + authentication.getPrincipal().getClass().getName());
-            System.out.println("Principal: " + authentication.getPrincipal());
-        }
-
-        if(authentication != null && authentication.isAuthenticated()) {
-            Object principal = authentication.getPrincipal();
-            if(principal instanceof MberVO) {
-                userId = ((MberVO) authentication.getPrincipal()).getMbrId();
+            if(authentication != null && authentication.isAuthenticated()) {
+                Object principal = authentication.getPrincipal();
+                if(principal instanceof MberVO) {
+                    userId = ((MberVO) principal).getMbrId();
+                }
             }
+
+            // 2. SecurityContext에서 조회 실패시 세션에서 직접 조회
+            if (userId == null) {
+                try {
+                    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
+                    HttpServletRequest request = attributes.getRequest();
+                    HttpSession session = request.getSession(false);
+
+                    if (session != null) {
+                        userId = (String) session.getAttribute("mbrId");
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+
+            // 3. JWT 토큰에서 조회 시도 (JWT 모드인 경우)
+            if (userId == null) {
+                try {
+                    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
+                    HttpServletRequest request = attributes.getRequest();
+
+                    // Authorization 헤더에서 토큰 추출
+                    String authHeader = request.getHeader("Authorization");
+                    String token = null;
+
+                    if (authHeader != null && authHeader.startsWith("Bearer ")) {
+                        token = authHeader.substring(7);
+                    } else if (request.getCookies() != null) {
+                        // 쿠키에서 토큰 추출
+                        for (Cookie cookie : request.getCookies()) {
+                            if ("Authorization".equals(cookie.getName()) || "refresh".equals(cookie.getName())) {
+                                token = cookie.getValue();
+                                if (token.startsWith("Bearer ")) {
+                                    token = token.substring(7);
+                                }
+                                break;
+                            }
+                        }
+                    }
+                    if (token != null && jwtUtil != null) {
+                        userId = (String) jwtUtil.getClaim(token, "mbrId");
+                    }
+
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
         }
+
         return userId;
     }
 }
Add a comment
List