From 93eb6b470773bc49ea6e1a9d4cbd914eb95d525b Mon Sep 17 00:00:00 2001 From: lrj <owen.stl@gmail.com> Date: 星期二, 30 九月 2025 17:38:04 +0800 Subject: [PATCH] feat: 完善比赛晋级功能并清理测试文件 --- backend/src/main/java/com/rongyichuang/common/util/UserContextUtil.java | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 102 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..e659917 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,100 @@ @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 + // 鍦ㄥ紑鍙戠幆澧冧笅锛岃繑鍥炰竴涓湁鏁堢殑璇勫鐢ㄦ埛ID + // 鏌ユ壘绗竴涓湁鏁堢殑璇勫璁板綍骞惰繑鍥炲叾user_id + try { + Optional<Judge> firstJudge = judgeRepository.findAll().stream().findFirst(); + if (firstJudge.isPresent() && firstJudge.get().getUserId() != null) { + Long userId = firstJudge.get().getUserId(); + logger.debug("寮�鍙戠幆澧冿細浣跨敤璇勫鐢ㄦ埛ID: {}", userId); + return userId; + } + } catch (Exception e) { + logger.warn("鏌ユ壘璇勫鐢ㄦ埛ID鏃跺彂鐢熷紓甯�: {}", e.getMessage()); + } + // 濡傛灉娌℃湁鎵惧埌璇勫锛岃繑鍥炲浐瀹氱敤鎴稩D + 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 +152,15 @@ } /** + * 鑾峰彇褰撳墠鐢ㄦ埛鍏宠仈鐨勫憳宸D + * + * @return 鍛樺伐ID锛屽鏋滃綋鍓嶇敤鎴蜂笉鏄憳宸ュ垯杩斿洖null + */ + public Long getCurrentEmployeeId() { + return getCurrentEmployee().map(Employee::getId).orElse(null); + } + + /** * 鑾峰彇褰撳墠鐢ㄦ埛鍏宠仈鐨勮瘎濮擨D * * @return 璇勫ID锛屽鏋滃綋鍓嶇敤鎴蜂笉鏄瘎濮斿垯杩斿洖null @@ -85,6 +170,15 @@ } /** + * 妫�鏌ュ綋鍓嶇敤鎴锋槸鍚︿负鍛樺伐 + * + * @return true濡傛灉褰撳墠鐢ㄦ埛鏄憳宸ワ紝鍚﹀垯false + */ + public boolean isCurrentUserEmployee() { + return getCurrentEmployee().isPresent(); + } + + /** * 妫�鏌ュ綋鍓嶇敤鎴锋槸鍚︿负璇勫 * * @return true濡傛灉褰撳墠鐢ㄦ埛鏄瘎濮旓紝鍚﹀垯false -- Gitblit v1.8.0