package com.simuwang.shiro.utils;

import com.simuwang.shiro.core.ShiroUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;

/**
 * @author wangzaijun
 * @date 2024/9/14 15:39
 * @description 登录用户工具
 */
public class UserUtils {
    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    /**
     * 获取当前登录的用户信息
     *
     * @return 当前登录用户
     */
    public static ShiroUser getLoginUser() {
        Subject subject = SecurityUtils.getSubject();
        return getLoginUser(subject);
    }

    /**
     * 获取当前登录的用户信息
     *
     * @return 当前登录用户
     */
    public static ShiroUser getLoginUser(Subject subject) {
        Object principal = subject.getPrincipal();
        if (principal instanceof ShiroUser shiroUser) {
            return shiroUser;
        }
        throw new AuthenticationException();
    }
}
