package com.artfess.base.util; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.JsonNode; /** * 用于获取用户信息 * @author liyanggui * */ @Service public class AuthenticationUtil implements ContextThread { /** * 当前用户的主组织 */ public final static String CURRENT_USER_MAIN_ORGID = "CURRENT_USER_MAIN_ORGID"; /** * 当前用户的组织idS 1,2,3 */ public final static String CURRENT_USER_ORGIDS = "CURRENT_USER_ORGIDS"; /** * 当前用户的组织idS 1,2,3 以及下级组织id */ public final static String CURRENT_USER_SUB_ORGIDS = "CURRENT_USER_SUB_ORGIDS"; // 用户信息的线程变量 private static ThreadLocal userThreadLocal = new ThreadLocal(); // security认证对象的线程变量 Authentication private static ThreadLocal authenticationThreadLocal = new ThreadLocal(); // 数据过滤的线程变量 private static ThreadLocal msIdsThreadLocal = new ThreadLocal(); private static ThreadLocal> mapThreadLocal = new ThreadLocal>(); public static void setMapThreadLocal(Map map){ mapThreadLocal.set(map); } public static Map getMapThreadLocal(){ Map resultMap = mapThreadLocal.get(); if(BeanUtils.isEmpty(resultMap)){ resultMap = new HashMap(); } return resultMap; } public static void removeMapThreadLocal(){ mapThreadLocal.remove(); } public static void setMsIdsThreadLocal( String[] msIds ){ msIdsThreadLocal.set(msIds); } public static String[] getMsIdsThreadLocal(){ return msIdsThreadLocal.get(); } public static void removeMsIdsThreadLocal(){ msIdsThreadLocal.remove(); } public static JsonNode getUserThreadLocal() { JsonNode jsonNode = userThreadLocal.get(); if(BeanUtils.isEmpty(jsonNode)) { return JsonUtil.getMapper().createObjectNode(); } return jsonNode; } public static void setAuthentication( Authentication authentication ){ authenticationThreadLocal.set(authentication); Object principal = authentication.getPrincipal(); try { if(principal instanceof UserDetails) { UserDetails ud = (UserDetails)principal; String json = JsonUtil.toJson(ud); JsonNode jsonNode = JsonUtil.toJsonNode(json); userThreadLocal.set(jsonNode); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } } public static String getCurrentUserId(){ JsonNode jsonNode = getUserThreadLocal().get("userId"); if(BeanUtils.isEmpty(jsonNode) || !jsonNode.isTextual()) return null; return jsonNode.asText(); } /** * 获取当前用户的名称 * @return */ public static String getCurrentUserFullname(){ JsonNode jsonNode = getUserThreadLocal().get("fullname"); if(BeanUtils.isEmpty(jsonNode) || !jsonNode.isTextual()) return null; return jsonNode.asText(); } /** * 获取当前用户的账号信息 * @return */ public static String getCurrentUsername(){ JsonNode jsonNode = getUserThreadLocal().get("account"); if(BeanUtils.isEmpty(jsonNode) || !jsonNode.isTextual()) return null; return jsonNode.asText(); } /** * 获取当前用户的主组织 * @return */ public static String getCurrentUserMainOrgId(){ JsonNode jsonNode = getUserThreadLocal().get("attributes"); if(BeanUtils.isNotEmpty(jsonNode) && jsonNode.has(CURRENT_USER_MAIN_ORGID)){ return jsonNode.get(CURRENT_USER_MAIN_ORGID).asText(); } return null; } /** * 获取当前用户所有组织 1,2,3 * @return */ public static String getCurrentUserOrgIds(){ JsonNode jsonNode = getUserThreadLocal().get("attributes"); if(BeanUtils.isNotEmpty(jsonNode) && jsonNode.has(CURRENT_USER_ORGIDS)){ return jsonNode.get(CURRENT_USER_ORGIDS).asText(); } return null; } /** * 获取当前用户所有组织 1,2,3以及下级组织 * @return */ public static String getCurrentUserSubOrgIds(){ JsonNode jsonNode = getUserThreadLocal().get("attributes"); if(BeanUtils.isNotEmpty(jsonNode) && jsonNode.has(CURRENT_USER_SUB_ORGIDS)){ return jsonNode.get(CURRENT_USER_SUB_ORGIDS).asText(); } return null; } /** * 获取当前用户具有的角色别名 * @return */ public static Set getCurrentUserRolesAlias(){ Authentication authentication = authenticationThreadLocal.get(); Set set = new HashSet(); if(BeanUtils.isEmpty(authentication)) { return set; } @SuppressWarnings("unchecked") Collection authorities = (Collection) authentication.getAuthorities(); for (SimpleGrantedAuthority simpleGrantedAuthority : authorities) { set.add(simpleGrantedAuthority.getAuthority()); } return set; } /** * */ public static void removeAll(){ userThreadLocal.remove(); authenticationThreadLocal.remove(); msIdsThreadLocal.remove(); mapThreadLocal.remove(); } /** * 判断当前是否为匿名请求 * @param authentication * @return */ public static boolean isAnonymous(Authentication authentication) { if(BeanUtils.isEmpty(authentication)) return true; if(authentication instanceof AnonymousAuthenticationToken) return true; return false; } @Override public void cleanAll() { removeAll(); } }