/**
 * 安全上下文工具类。
 *
 * <p>用于从 Spring Security 上下文中获取当前登录用户信息。</p>
 */
package com.hertz.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public final class SecurityUtils {
  private SecurityUtils() {
  }

  public static Long getCurrentUserId() {
    return getCurrentUserId(SecurityContextHolder.getContext().getAuthentication());
  }

  public static Long getCurrentUserId(Authentication authentication) {
    if (authentication == null) {
      return null;
    }
    var details = authentication.getDetails();
    if (details instanceof Number n) {
      return n.longValue();
    }
    return null;
  }
}

