From bec58fa7fe4fae2deac88200d8d939e12ec8a08f Mon Sep 17 00:00:00 2001
From: lrj <owen.stl@gmail.com>
Date: 星期五, 03 十月 2025 22:26:39 +0800
Subject: [PATCH] 修复小程序WXS日期显示问题并重新设计【我的】页面
---
backend/src/main/java/com/rongyichuang/auth/filter/JwtAuthenticationFilter.java | 72 +++++++++++++++++++++++++++++++++--
1 files changed, 67 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..96e6413 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;
@@ -35,35 +36,88 @@
@Autowired
private UserRepository userRepository;
+
+ /**
+ * 鍒ゆ柇鏄惁搴旇璺宠繃JWT璁よ瘉
+ */
+ private boolean shouldSkipAuthentication(String requestURI) {
+ // 杩欎簺璺緞涓嶉渶瑕丣WT璁よ瘉锛堝凡鍘绘帀context path锛�
+ String[] skipPaths = {
+ "/auth/",
+ "/actuator/",
+ "/test/",
+ "/cleanup/",
+ "/upload/",
+ "/graphql",
+ "/graphiql"
+ };
+
+ for (String path : skipPaths) {
+ if (requestURI.startsWith(path)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
- FilterChain filterChain) throws ServletException, IOException {
+ FilterChain filterChain) throws ServletException, IOException {
+ String requestURI = request.getRequestURI();
+ String contextPath = request.getContextPath();
+
+ // 鍘绘帀context path锛屼笌Spring Security鐨勮涓轰繚鎸佷竴鑷�
+ String pathWithoutContext = requestURI;
+ if (contextPath != null && !contextPath.isEmpty() && requestURI.startsWith(contextPath)) {
+ pathWithoutContext = requestURI.substring(contextPath.length());
+ }
+
+ System.out.println("=== JWT杩囨护鍣ㄨ璋冪敤 === 鍘熷URI: " + requestURI + ", 鍘绘帀context path鍚�: " + pathWithoutContext);
+ logger.debug("JWT杩囨护鍣ㄥ紑濮嬪鐞嗚姹�: {}", pathWithoutContext);
+
+ // 璺宠繃涓嶉渶瑕佽璇佺殑璺緞
+ if (shouldSkipAuthentication(pathWithoutContext)) {
+ logger.debug("璺宠繃JWT璁よ瘉锛岃矾寰�: {}", pathWithoutContext);
+ filterChain.doFilter(request, response);
+ return;
+ }
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 +130,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