package com.ym.mec.common.security;

import java.util.Map;

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

import com.alibaba.fastjson.JSON;

public class SecurityUtils {
	/**
	 * 获取Authentication
	 */
	public static Authentication getAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();
	}

	/**
	 * 获取用户
	 */
	public static AuthUser getUser(Authentication authentication) {
		Object principal = authentication.getPrincipal();
		if (principal instanceof AuthUser) {
			return (AuthUser) principal;
		}
		return null;
	}

	/**
	 * 获取用户
	 */
	public static Object getPrincipal() {
		Authentication authentication = getAuthentication();
		if (authentication == null) {
			return null;
		}
		return authentication.getPrincipal();
	}

	/**
	 * 获取用户
	 */
	public static AuthUser getUser() {
		Authentication authentication = getAuthentication();
		return getUser(authentication);
	}

	public static String getAuthenticationValue(){
		return "bearer " + JSON.parseObject(JSON.toJSONString(getAuthentication().getDetails()), Map.class).get("tokenValue").toString();
	}

	public static String getToken(){
		return JSON.parseObject(JSON.toJSONString(getAuthentication().getDetails()), Map.class).get("tokenValue").toString();
	}
}
