package com.framework.core.utils;

import net.sf.json.JSONObject;
import org.springframework.security.core.context.SecurityContextHolder;

public class AuthUtils {
    private static final String id = "id";

    private static final String username = "username";

    //    通过请求头的Authorization jwt获取用户id
    public static String getAccountId() {
        JSONObject fromObject = JSONObject.fromObject(SecurityContextHolder.getContext().getAuthentication().getPrincipal());//转换数据类型
        return fromObject.get(id).toString();
    }

    //    通过请求头的Authorization jwt获取用户账号的username
    public static String getUsername() {
        JSONObject fromObject = JSONObject.fromObject(SecurityContextHolder.getContext().getAuthentication().getPrincipal());//转换数据类型
        return fromObject.get(username).toString();
    }
}
