Codex Assistant
1 天以前 afeeed281e60466b576fbe74d339634cc5d07b82
backend/src/main/java/com/rongyichuang/common/util/UserContextUtil.java
@@ -36,18 +36,18 @@
    private JwtUtil jwtUtil;
    /**
     * 获取当前登录用户ID
     * 从JWT token中解析用户ID
     * 获取当前登录用户ID(包括匿名用户)
     * 从JWT token中解析用户ID,包括负数的匿名用户ID
     * 
     * @return 用户ID
     * @return 用户ID,包括匿名用户的负数ID
     */
    public Long getCurrentUserId() {
    public Long getCurrentUserIdIncludingAnonymous() {
        try {
            // 首先尝试从HTTP请求头中获取JWT token
            String token = getTokenFromRequest();
            if (token != null && jwtUtil.validateToken(token)) {
                Long userId = jwtUtil.getUserIdFromToken(token);
                logger.debug("从JWT token中获取到用户ID: {}", userId);
                logger.debug("从JWT token中获取到用户ID(包括匿名用户): {}", userId);
                return userId;
            }
@@ -59,37 +59,103 @@
            // 如果没有有效的JWT token,尝试从Spring Security上下文获取
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null && authentication.isAuthenticated() &&
                !"anonymousUser".equals(authentication.getPrincipal())) {
                logger.debug("获取到认证用户: {}", authentication.getName());
                // 在开发环境下,返回一个有效的评委用户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);
            if (authentication != null && authentication.isAuthenticated()) {
                String principal = authentication.getName();
                logger.debug("获取到认证用户: {}", principal);
                // 检查是否为匿名用户
                if ("anonymousUser".equals(principal)) {
                    logger.debug("检测到Spring默认匿名用户,返回null");
                    return null;
                } else if (principal.startsWith("anonymous_")) {
                    // 从 "anonymous_-833488" 中提取用户ID
                    try {
                        String userIdStr = principal.substring("anonymous_".length());
                        Long userId = Long.parseLong(userIdStr);
                        logger.debug("从匿名认证中解析到用户ID: {}", userId);
                        return userId;
                    } catch (NumberFormatException e) {
                        logger.warn("无法从匿名认证信息中解析用户ID: {}", principal);
                    }
                } catch (Exception e) {
                    logger.warn("查找评委用户ID时发生异常: {}", e.getMessage());
                }
                // 如果没有找到评委,返回固定用户ID
                return 1L;
                // 从Spring Security上下文中获取用户ID
                try {
                    return Long.parseLong(principal);
                } catch (NumberFormatException e) {
                    logger.warn("无法从认证信息中解析用户ID: {}", principal);
                }
            }
        } catch (Exception e) {
            logger.warn("获取当前用户ID时发生异常: {}", e.getMessage());
        }
        
        // 如果没有认证信息,返回null表示未登录
        logger.debug("未找到有效的认证信息");
        // 如果没有有效的认证信息,返回null
        logger.debug("没有有效的认证信息,返回null");
        return null;
    }
    /**
     * 获取当前登录用户ID
     * 从JWT token中解析用户ID
     *
     * @return 用户ID,如果是匿名用户则返回null
     */
    public Long getCurrentUserId() {
        try {
            // 首先尝试从HTTP请求头中获取JWT token
            String token = getTokenFromRequest();
            if (token != null && jwtUtil.validateToken(token)) {
                Long userId = jwtUtil.getUserIdFromToken(token);
                logger.debug("从JWT token中获取到用户ID: {}", userId);
                // 检查是否为匿名用户(负数用户ID)
                if (userId != null && userId < 0) {
                    logger.debug("检测到匿名用户,返回null");
                    return null;
                }
                return userId;
            }
            if (token == null) {
                logger.debug("未能从请求头获取到JWT token");
            } else {
                logger.debug("从请求头获取到token但校验失败");
            }
            // 如果没有有效的JWT 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("检测到匿名用户认证,返回null");
                    return null;
                }
                // 从Spring Security上下文中获取用户ID
                try {
                    return Long.parseLong(principal);
                } catch (NumberFormatException e) {
                    logger.warn("无法从认证信息中解析用户ID: {}", principal);
                }
            }
        } catch (Exception e) {
            logger.warn("获取当前用户ID时发生异常: {}", e.getMessage());
        }
        // 如果没有有效的认证信息,返回null(支持匿名访问)
        logger.debug("没有有效的认证信息,返回null(匿名用户)");
        return null;
    }
    /**
     * 从HTTP请求中获取JWT token
     */
    private String getTokenFromRequest() {
    public String getTokenFromRequest() {
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attributes == null) {