

250616 김혜민 사용자 ID조회 에러 수정
@ece82a6d8e2f7e64979c88888bbe8951733d5cfd
--- src/main/java/com/takensoft/common/service/VerificationService.java
+++ src/main/java/com/takensoft/common/service/VerificationService.java
... | ... | @@ -2,10 +2,16 @@ |
2 | 2 |
|
3 | 3 |
import com.takensoft.cms.mber.vo.MberVO; |
4 | 4 |
import com.takensoft.common.exception.CustomAccessDeniedException; |
5 |
+import com.takensoft.common.util.JWTUtil; |
|
5 | 6 |
import lombok.RequiredArgsConstructor; |
6 | 7 |
import org.springframework.security.core.Authentication; |
7 | 8 |
import org.springframework.security.core.context.SecurityContextHolder; |
8 | 9 |
import org.springframework.stereotype.Service; |
10 |
+import org.springframework.web.context.request.RequestContextHolder; |
|
11 |
+import org.springframework.web.context.request.ServletRequestAttributes; |
|
12 |
+import jakarta.servlet.http.HttpServletRequest; |
|
13 |
+import jakarta.servlet.http.HttpSession; |
|
14 |
+import jakarta.servlet.http.Cookie; |
|
9 | 15 |
|
10 | 16 |
/** |
11 | 17 |
* @author takensoft |
... | ... | @@ -19,6 +25,9 @@ |
19 | 25 |
@Service("authorizationService") |
20 | 26 |
@RequiredArgsConstructor |
21 | 27 |
public class VerificationService { |
28 |
+ |
|
29 |
+ private final JWTUtil jwtUtil; |
|
30 |
+ |
|
22 | 31 |
|
23 | 32 |
/** |
24 | 33 |
* @return 현재 인증된 사용자 정보 |
... | ... | @@ -78,23 +87,69 @@ |
78 | 87 |
*/ |
79 | 88 |
public String getCurrentUserId() { |
80 | 89 |
String userId = null; |
81 |
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
|
82 | 90 |
|
91 |
+ try { |
|
92 |
+ // 1. SecurityContext에서 시도 |
|
93 |
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
|
83 | 94 |
|
84 |
- // 디버깅용 로그 추가 |
|
85 |
- System.out.println("Authentication: " + authentication); |
|
86 |
- if(authentication != null) { |
|
87 |
- System.out.println("Is Authenticated: " + authentication.isAuthenticated()); |
|
88 |
- System.out.println("Principal Type: " + authentication.getPrincipal().getClass().getName()); |
|
89 |
- System.out.println("Principal: " + authentication.getPrincipal()); |
|
90 |
- } |
|
91 |
- |
|
92 |
- if(authentication != null && authentication.isAuthenticated()) { |
|
93 |
- Object principal = authentication.getPrincipal(); |
|
94 |
- if(principal instanceof MberVO) { |
|
95 |
- userId = ((MberVO) authentication.getPrincipal()).getMbrId(); |
|
95 |
+ if(authentication != null && authentication.isAuthenticated()) { |
|
96 |
+ Object principal = authentication.getPrincipal(); |
|
97 |
+ if(principal instanceof MberVO) { |
|
98 |
+ userId = ((MberVO) principal).getMbrId(); |
|
99 |
+ } |
|
96 | 100 |
} |
101 |
+ |
|
102 |
+ // 2. SecurityContext에서 조회 실패시 세션에서 직접 조회 |
|
103 |
+ if (userId == null) { |
|
104 |
+ try { |
|
105 |
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); |
|
106 |
+ HttpServletRequest request = attributes.getRequest(); |
|
107 |
+ HttpSession session = request.getSession(false); |
|
108 |
+ |
|
109 |
+ if (session != null) { |
|
110 |
+ userId = (String) session.getAttribute("mbrId"); |
|
111 |
+ } |
|
112 |
+ } catch (Exception e) { |
|
113 |
+ e.printStackTrace(); |
|
114 |
+ } |
|
115 |
+ } |
|
116 |
+ |
|
117 |
+ // 3. JWT 토큰에서 조회 시도 (JWT 모드인 경우) |
|
118 |
+ if (userId == null) { |
|
119 |
+ try { |
|
120 |
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); |
|
121 |
+ HttpServletRequest request = attributes.getRequest(); |
|
122 |
+ |
|
123 |
+ // Authorization 헤더에서 토큰 추출 |
|
124 |
+ String authHeader = request.getHeader("Authorization"); |
|
125 |
+ String token = null; |
|
126 |
+ |
|
127 |
+ if (authHeader != null && authHeader.startsWith("Bearer ")) { |
|
128 |
+ token = authHeader.substring(7); |
|
129 |
+ } else if (request.getCookies() != null) { |
|
130 |
+ // 쿠키에서 토큰 추출 |
|
131 |
+ for (Cookie cookie : request.getCookies()) { |
|
132 |
+ if ("Authorization".equals(cookie.getName()) || "refresh".equals(cookie.getName())) { |
|
133 |
+ token = cookie.getValue(); |
|
134 |
+ if (token.startsWith("Bearer ")) { |
|
135 |
+ token = token.substring(7); |
|
136 |
+ } |
|
137 |
+ break; |
|
138 |
+ } |
|
139 |
+ } |
|
140 |
+ } |
|
141 |
+ if (token != null && jwtUtil != null) { |
|
142 |
+ userId = (String) jwtUtil.getClaim(token, "mbrId"); |
|
143 |
+ } |
|
144 |
+ |
|
145 |
+ } catch (Exception e) { |
|
146 |
+ e.printStackTrace(); |
|
147 |
+ } |
|
148 |
+ } |
|
149 |
+ } catch (Exception e) { |
|
150 |
+ e.printStackTrace(); |
|
97 | 151 |
} |
152 |
+ |
|
98 | 153 |
return userId; |
99 | 154 |
} |
100 | 155 |
} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?