From 9f8395fab13ca4b230a0f7d62636e209745c91d4 Mon Sep 17 00:00:00 2001
From: lrj <owen.stl@gmail.com>
Date: 星期日, 28 九月 2025 14:16:18 +0800
Subject: [PATCH] feat: 完善注册流程的文件上传功能
---
backend/src/main/java/com/rongyichuang/common/util/UserContextUtil.java | 99 +++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 91 insertions(+), 8 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..0bf65c7 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,89 @@
@Autowired
private JudgeRepository judgeRepository;
+ @Autowired
+ private EmployeeRepository employeeRepository;
+
+ @Autowired
+ private JwtUtil jwtUtil;
+
/**
* 鑾峰彇褰撳墠鐧诲綍鐢ㄦ埛ID
- * 娉ㄦ剰锛氬綋鍓嶇郴缁熸殏鏃朵娇鐢ㄥ浐瀹氱敤鎴稩D锛屽悗缁渶瑕佹牴鎹疄闄呰璇佹満鍒朵慨鏀�
+ * 浠嶫WT token涓В鏋愮敤鎴稩D
*
* @return 鐢ㄦ埛ID
*/
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);
+ return userId;
+ }
+
+ // 濡傛灉娌℃湁鏈夋晥鐨凧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
+ // 濡傛灉璁よ瘉淇℃伅涓寘鍚敤鎴稩D锛屽彲浠ュ湪杩欓噷瑙f瀽
+ // 鏆傛椂杩斿洖鍥哄畾鐢ㄦ埛ID鐢ㄤ簬鍏煎鎬�
+ return 1L;
}
} catch (Exception e) {
logger.warn("鑾峰彇褰撳墠鐢ㄦ埛ID鏃跺彂鐢熷紓甯�: {}", e.getMessage());
}
- // 濡傛灉娌℃湁璁よ瘉淇℃伅锛岃繑鍥為粯璁ょ敤鎴稩D锛堝紑鍙戦樁娈典娇鐢級
- logger.debug("鏈壘鍒拌璇佷俊鎭紝浣跨敤榛樿鐢ㄦ埛ID");
- return 1L;
+ // 濡傛灉娌℃湁璁よ瘉淇℃伅锛岃繑鍥瀗ull琛ㄧず鏈櫥褰�
+ logger.debug("鏈壘鍒版湁鏁堢殑璁よ瘉淇℃伅");
+ return null;
+ }
+
+ /**
+ * 浠嶩TTP璇锋眰涓幏鍙朖WT token
+ */
+ private String getTokenFromRequest() {
+ try {
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+ if (attributes != null) {
+ HttpServletRequest request = attributes.getRequest();
+ String authHeader = request.getHeader("Authorization");
+ if (authHeader != null && authHeader.startsWith("Bearer ")) {
+ return authHeader.substring(7);
+ }
+ }
+ } 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 +141,15 @@
}
/**
+ * 鑾峰彇褰撳墠鐢ㄦ埛鍏宠仈鐨勫憳宸D
+ *
+ * @return 鍛樺伐ID锛屽鏋滃綋鍓嶇敤鎴蜂笉鏄憳宸ュ垯杩斿洖null
+ */
+ public Long getCurrentEmployeeId() {
+ return getCurrentEmployee().map(Employee::getId).orElse(null);
+ }
+
+ /**
* 鑾峰彇褰撳墠鐢ㄦ埛鍏宠仈鐨勮瘎濮擨D
*
* @return 璇勫ID锛屽鏋滃綋鍓嶇敤鎴蜂笉鏄瘎濮斿垯杩斿洖null
@@ -85,6 +159,15 @@
}
/**
+ * 妫�鏌ュ綋鍓嶇敤鎴锋槸鍚︿负鍛樺伐
+ *
+ * @return true濡傛灉褰撳墠鐢ㄦ埛鏄憳宸ワ紝鍚﹀垯false
+ */
+ public boolean isCurrentUserEmployee() {
+ return getCurrentEmployee().isPresent();
+ }
+
+ /**
* 妫�鏌ュ綋鍓嶇敤鎴锋槸鍚︿负璇勫
*
* @return true濡傛灉褰撳墠鐢ㄦ埛鏄瘎濮旓紝鍚﹀垯false
--
Gitblit v1.8.0