From dc643ba44fd2a426263015491268a0f0d6b4671d Mon Sep 17 00:00:00 2001 From: lrj <owen.stl@gmail.com> Date: 星期三, 01 十月 2025 08:39:29 +0800 Subject: [PATCH] 删除包含test、check、fix的文件名的文件 --- backend/src/main/java/com/rongyichuang/auth/filter/JwtAuthenticationFilter.java | 32 +++++++++++++++++++++++++++----- 1 files changed, 27 insertions(+), 5 deletions(-) diff --git a/backend/src/main/java/com/rongyichuang/auth/filter/JwtAuthenticationFilter.java b/backend/src/main/java/com/rongyichuang/auth/filter/JwtAuthenticationFilter.java index 32c4aa5..4fef1a3 100644 --- a/backend/src/main/java/com/rongyichuang/auth/filter/JwtAuthenticationFilter.java +++ b/backend/src/main/java/com/rongyichuang/auth/filter/JwtAuthenticationFilter.java @@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; @@ -38,32 +39,45 @@ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, - FilterChain filterChain) throws ServletException, IOException { + FilterChain filterChain) throws ServletException, IOException { + System.out.println("=== JWT杩囨护鍣ㄨ璋冪敤 === URI: " + request.getRequestURI()); + logger.debug("JWT杩囨护鍣ㄥ紑濮嬪鐞嗚姹�: {}", request.getRequestURI()); String authHeader = request.getHeader("Authorization"); String token = null; Long userId = null; + logger.debug("Authorization澶�: {}", authHeader); + // 浠庤姹傚ご涓彁鍙朖WT token if (authHeader != null && authHeader.startsWith("Bearer ")) { token = authHeader.substring(7); + logger.debug("鎻愬彇鍒癑WT token: {}", token.substring(0, Math.min(20, token.length())) + "..."); try { userId = jwtUtil.getUserIdFromToken(token); + logger.debug("浠巘oken涓В鏋愬埌鐢ㄦ埛ID: {}", userId); } catch (Exception e) { - logger.debug("JWT token瑙f瀽澶辫触: {}", e.getMessage()); + logger.error("JWT token瑙f瀽澶辫触: {}", e.getMessage(), e); } + } else { + logger.debug("娌℃湁鎵惧埌Authorization澶存垨鏍煎紡涓嶆纭�"); } - // 濡傛灉token鏈夋晥涓斿綋鍓嶆病鏈夎璇佷俊鎭� - if (userId != null && SecurityContextHolder.getContext().getAuthentication() == null) { + // 濡傛灉token鏈夋晥涓斿綋鍓嶆槸鍖垮悕鎴栨棤璁よ瘉锛屽垯杩涜璁よ瘉 + Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication(); + boolean isAnonymous = (existingAuth == null) || ("anonymousUser".equals(String.valueOf(existingAuth.getPrincipal()))); + if (userId != null && isAnonymous) { + logger.debug("寮�濮嬮獙璇乼oken鏈夋晥鎬�"); // 楠岃瘉token鏄惁鏈夋晥 if (jwtUtil.validateToken(token)) { + logger.debug("Token楠岃瘉鎴愬姛锛屾煡鎵剧敤鎴蜂俊鎭�"); // 鏌ユ壘鐢ㄦ埛淇℃伅 Optional<User> userOpt = userRepository.findById(userId); if (userOpt.isPresent()) { User user = userOpt.get(); + logger.debug("鎵惧埌鐢ㄦ埛: userId={}, phone={}", user.getId(), user.getPhone()); // 鍒涘缓璁よ瘉瀵硅薄 UsernamePasswordAuthenticationToken authToken = @@ -76,9 +90,17 @@ authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authToken); - logger.debug("鐢ㄦ埛璁よ瘉鎴愬姛: userId={}, phone={}", user.getId(), user.getPhone()); + logger.info("鐢ㄦ埛璁よ瘉鎴愬姛: userId={}, phone={}", user.getId(), user.getPhone()); + } else { + logger.warn("鐢ㄦ埛涓嶅瓨鍦�: userId={}", userId); } + } else { + logger.warn("Token楠岃瘉澶辫触"); } + } else if (userId == null) { + logger.debug("娌℃湁瑙f瀽鍒扮敤鎴稩D"); + } else { + logger.debug("宸插瓨鍦ㄩ潪鍖垮悕璁よ瘉淇℃伅锛岃烦杩嘕WT璁よ瘉"); } filterChain.doFilter(request, response); -- Gitblit v1.8.0