From 375c18a6d2713ff19b22093eec57315992d8333f Mon Sep 17 00:00:00 2001
From: Codex Assistant <codex@example.com>
Date: 星期四, 06 十一月 2025 13:33:52 +0800
Subject: [PATCH] 增加评审下载
---
backend/src/main/java/com/rongyichuang/common/util/UserContextUtil.java | 201 ++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 187 insertions(+), 14 deletions(-)
diff --git a/backend/src/main/java/com/rongyichuang/common/util/UserContextUtil.java b/backend/src/main/java/com/rongyichuang/common/util/UserContextUtil.java
index 99ee7b1..0ce0566 100644
--- a/backend/src/main/java/com/rongyichuang/common/util/UserContextUtil.java
+++ b/backend/src/main/java/com/rongyichuang/common/util/UserContextUtil.java
@@ -1,5 +1,8 @@
package com.rongyichuang.common.util;
+import com.rongyichuang.auth.util.JwtUtil;
+import com.rongyichuang.employee.entity.Employee;
+import com.rongyichuang.employee.repository.EmployeeRepository;
import com.rongyichuang.judge.entity.Judge;
import com.rongyichuang.judge.repository.JudgeRepository;
import org.slf4j.Logger;
@@ -8,7 +11,10 @@
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
/**
@@ -23,30 +29,179 @@
@Autowired
private JudgeRepository judgeRepository;
+ @Autowired
+ private EmployeeRepository employeeRepository;
+
+ @Autowired
+ private JwtUtil jwtUtil;
+
/**
- * 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛ID
- * 娉ㄦ剰锛氬綋鍓嶇郴缁熸殏鏃朵娇鐢ㄥ浐瀹氱敤鎴稩D锛屽悗缁渶瑕佹牴鎹疄闄呰璇佹満鍒朵慨鏀�
+ * 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛ID锛堝寘鎷尶鍚嶇敤鎴凤級
+ * 浠嶫WT token涓В鏋愮敤鎴稩D锛屽寘鎷礋鏁扮殑鍖垮悕鐢ㄦ埛ID
*
- * @return 鐢ㄦ埛ID
+ * @return 鐢ㄦ埛ID锛屽寘鎷尶鍚嶇敤鎴风殑璐熸暟ID
*/
- public Long getCurrentUserId() {
+ public Long getCurrentUserIdIncludingAnonymous() {
try {
+ // 棣栧厛灏濊瘯浠嶩TTP璇锋眰澶翠腑鑾峰彇JWT token
+ String token = getTokenFromRequest();
+ if (token != null && jwtUtil.validateToken(token)) {
+ Long userId = jwtUtil.getUserIdFromToken(token);
+ logger.debug("浠嶫WT token涓幏鍙栧埌鐢ㄦ埛ID锛堝寘鎷尶鍚嶇敤鎴凤級: {}", userId);
+ return userId;
+ }
+
+ if (token == null) {
+ logger.debug("鏈兘浠庤姹傚ご鑾峰彇鍒癑WT token");
+ } else {
+ logger.debug("浠庤姹傚ご鑾峰彇鍒皌oken浣嗘牎楠屽け璐�");
+ }
+
+ // 濡傛灉娌℃湁鏈夋晥鐨凧WT token锛屽皾璇曚粠Spring Security涓婁笅鏂囪幏鍙�
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
- if (authentication != null && authentication.isAuthenticated() &&
- !"anonymousUser".equals(authentication.getPrincipal())) {
- // TODO: 浠庤璇佷俊鎭腑鑾峰彇鐪熷疄鐨勭敤鎴稩D
- // 杩欓噷闇�瑕佹牴鎹疄闄呯殑璁よ瘉鏈哄埗鏉ュ疄鐜�
- // 渚嬪锛氫粠JWT token涓В鏋愮敤鎴稩D锛屾垨浠嶶serDetails涓幏鍙�
- logger.debug("鑾峰彇鍒拌璇佺敤鎴�: {}", authentication.getName());
- return 1L; // 涓存椂杩斿洖鍥哄畾鐢ㄦ埛ID
+ if (authentication != null && authentication.isAuthenticated()) {
+ String principal = authentication.getName();
+ logger.debug("鑾峰彇鍒拌璇佺敤鎴�: {}", principal);
+
+ // 妫�鏌ユ槸鍚︿负鍖垮悕鐢ㄦ埛
+ if ("anonymousUser".equals(principal)) {
+ logger.debug("妫�娴嬪埌Spring榛樿鍖垮悕鐢ㄦ埛锛岃繑鍥瀗ull");
+ return null;
+ } else if (principal.startsWith("anonymous_")) {
+ // 浠� "anonymous_-833488" 涓彁鍙栫敤鎴稩D
+ try {
+ String userIdStr = principal.substring("anonymous_".length());
+ Long userId = Long.parseLong(userIdStr);
+ logger.debug("浠庡尶鍚嶈璇佷腑瑙f瀽鍒扮敤鎴稩D: {}", userId);
+ return userId;
+ } catch (NumberFormatException e) {
+ logger.warn("鏃犳硶浠庡尶鍚嶈璇佷俊鎭腑瑙f瀽鐢ㄦ埛ID: {}", principal);
+ }
+ }
+
+ // 浠嶴pring Security涓婁笅鏂囦腑鑾峰彇鐢ㄦ埛ID
+ try {
+ return Long.parseLong(principal);
+ } catch (NumberFormatException e) {
+ logger.warn("鏃犳硶浠庤璇佷俊鎭腑瑙f瀽鐢ㄦ埛ID: {}", principal);
+ }
}
} catch (Exception e) {
logger.warn("鑾峰彇褰撳墠鐢ㄦ埛ID鏃跺彂鐢熷紓甯�: {}", e.getMessage());
}
- // 濡傛灉娌℃湁璁よ瘉淇℃伅锛岃繑鍥為粯璁ょ敤鎴稩D锛堝紑鍙戦樁娈典娇鐢級
- logger.debug("鏈壘鍒拌璇佷俊鎭紝浣跨敤榛樿鐢ㄦ埛ID");
- return 1L;
+ // 濡傛灉娌℃湁鏈夋晥鐨勮璇佷俊鎭紝杩斿洖null
+ logger.debug("娌℃湁鏈夋晥鐨勮璇佷俊鎭紝杩斿洖null");
+ return null;
+ }
+
+ /**
+ * 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛ID
+ * 浠嶫WT token涓В鏋愮敤鎴稩D
+ *
+ * @return 鐢ㄦ埛ID锛屽鏋滄槸鍖垮悕鐢ㄦ埛鍒欒繑鍥瀗ull
+ */
+ public Long getCurrentUserId() {
+ try {
+ // 棣栧厛灏濊瘯浠嶩TTP璇锋眰澶翠腑鑾峰彇JWT token
+ String token = getTokenFromRequest();
+ if (token != null && jwtUtil.validateToken(token)) {
+ Long userId = jwtUtil.getUserIdFromToken(token);
+ logger.debug("浠嶫WT token涓幏鍙栧埌鐢ㄦ埛ID: {}", userId);
+
+ // 妫�鏌ユ槸鍚︿负鍖垮悕鐢ㄦ埛锛堣礋鏁扮敤鎴稩D锛�
+ if (userId != null && userId < 0) {
+ logger.debug("妫�娴嬪埌鍖垮悕鐢ㄦ埛锛岃繑鍥瀗ull");
+ return null;
+ }
+
+ return userId;
+ }
+
+ if (token == null) {
+ logger.debug("鏈兘浠庤姹傚ご鑾峰彇鍒癑WT token");
+ } else {
+ logger.debug("浠庤姹傚ご鑾峰彇鍒皌oken浣嗘牎楠屽け璐�");
+ }
+
+ // 濡傛灉娌℃湁鏈夋晥鐨凧WT token锛屽皾璇曚粠Spring Security涓婁笅鏂囪幏鍙�
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+ if (authentication != null && authentication.isAuthenticated()) {
+ String principal = authentication.getName();
+ logger.debug("鑾峰彇鍒拌璇佺敤鎴�: {}", principal);
+
+ // 妫�鏌ユ槸鍚︿负鍖垮悕鐢ㄦ埛
+ if ("anonymousUser".equals(principal) || principal.startsWith("anonymous_")) {
+ logger.debug("妫�娴嬪埌鍖垮悕鐢ㄦ埛璁よ瘉锛岃繑鍥瀗ull");
+ return null;
+ }
+
+ // 浠嶴pring Security涓婁笅鏂囦腑鑾峰彇鐢ㄦ埛ID
+ try {
+ return Long.parseLong(principal);
+ } catch (NumberFormatException e) {
+ logger.warn("鏃犳硶浠庤璇佷俊鎭腑瑙f瀽鐢ㄦ埛ID: {}", principal);
+ }
+ }
+ } catch (Exception e) {
+ logger.warn("鑾峰彇褰撳墠鐢ㄦ埛ID鏃跺彂鐢熷紓甯�: {}", e.getMessage());
+ }
+
+ // 濡傛灉娌℃湁鏈夋晥鐨勮璇佷俊鎭紝杩斿洖null锛堟敮鎸佸尶鍚嶈闂級
+ logger.debug("娌℃湁鏈夋晥鐨勮璇佷俊鎭紝杩斿洖null锛堝尶鍚嶇敤鎴凤級");
+ return null;
+ }
+
+ /**
+ * 浠嶩TTP璇锋眰涓幏鍙朖WT token
+ */
+ public String getTokenFromRequest() {
+ try {
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+ if (attributes == null) {
+ logger.warn("RequestContextHolder涓棤ServletRequestAttributes锛屽彲鑳戒负寮傛鎵ц鎴栭潪Servlet鐜");
+ } else {
+ HttpServletRequest request = attributes.getRequest();
+ String authHeader = request.getHeader("Authorization");
+ logger.debug("璇诲彇鍒癆uthorization澶�: {}", authHeader);
+ if (authHeader != null && authHeader.startsWith("Bearer ")) {
+ String token = authHeader.substring(7);
+ logger.debug("浠嶢uthorization澶存彁鍙栧埌Bearer token锛岄暱搴�: {}", token != null ? token.length() : 0);
+ return token;
+ } else {
+ logger.debug("Authorization澶翠笉瀛樺湪鎴栦笉浠earer寮�澶�");
+ }
+ }
+ } catch (Exception e) {
+ logger.debug("鑾峰彇JWT token鏃跺彂鐢熷紓甯�: {}", e.getMessage());
+ }
+ return null;
+ }
+
+ /**
+ * 鑾峰彇褰撳墠鐢ㄦ埛鍏宠仈鐨勫憳宸ヤ俊鎭�
+ *
+ * @return 鍛樺伐淇℃伅锛屽鏋滃綋鍓嶇敤鎴蜂笉鏄憳宸ュ垯杩斿洖绌�
+ */
+ public Optional<Employee> getCurrentEmployee() {
+ Long userId = getCurrentUserId();
+ if (userId == null) {
+ logger.warn("鏃犳硶鑾峰彇褰撳墠鐢ㄦ埛ID");
+ return Optional.empty();
+ }
+
+ try {
+ Optional<Employee> employee = employeeRepository.findByUserId(userId);
+ if (employee.isPresent()) {
+ logger.debug("鎵惧埌褰撳墠鐢ㄦ埛鍏宠仈鐨勫憳宸�: {}", employee.get().getName());
+ } else {
+ logger.debug("褰撳墠鐢ㄦ埛(ID: {})涓嶆槸鍛樺伐", userId);
+ }
+ return employee;
+ } catch (Exception e) {
+ logger.error("鏌ヨ鍛樺伐淇℃伅鏃跺彂鐢熷紓甯�: {}", e.getMessage(), e);
+ return Optional.empty();
+ }
}
/**
@@ -76,6 +231,15 @@
}
/**
+ * 鑾峰彇褰撳墠鐢ㄦ埛鍏宠仈鐨勫憳宸D
+ *
+ * @return 鍛樺伐ID锛屽鏋滃綋鍓嶇敤鎴蜂笉鏄憳宸ュ垯杩斿洖null
+ */
+ public Long getCurrentEmployeeId() {
+ return getCurrentEmployee().map(Employee::getId).orElse(null);
+ }
+
+ /**
* 鑾峰彇褰撳墠鐢ㄦ埛鍏宠仈鐨勮瘎濮擨D
*
* @return 璇勫ID锛屽鏋滃綋鍓嶇敤鎴蜂笉鏄瘎濮斿垯杩斿洖null
@@ -85,6 +249,15 @@
}
/**
+ * 妫�鏌ュ綋鍓嶇敤鎴锋槸鍚︿负鍛樺伐
+ *
+ * @return true濡傛灉褰撳墠鐢ㄦ埛鏄憳宸ワ紝鍚﹀垯false
+ */
+ public boolean isCurrentUserEmployee() {
+ return getCurrentEmployee().isPresent();
+ }
+
+ /**
* 妫�鏌ュ綋鍓嶇敤鎴锋槸鍚︿负璇勫
*
* @return true濡傛灉褰撳墠鐢ㄦ埛鏄瘎濮旓紝鍚﹀垯false
--
Gitblit v1.8.0