ShiroUtils.java 3.95 KB
package com.huaheng.common.utils.security;

import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.bean.BeanUtils;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import com.huaheng.framework.shiro.realm.UserRealm;
import com.huaheng.pc.system.user.domain.User;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * shiro 工具类
 *
 * @author huaheng
 */
public class ShiroUtils
{

    @Resource
    private WarehouseService warehouseService;

    public static Subject getSubjct()
    {
        return SecurityUtils.getSubject();
    }

    public static Session getSession()
    {
        return SecurityUtils.getSubject().getSession();
    }

    public static void logout()
    {
        getSubjct().logout();
    }

    public static User getUser()
    {
        try {
            Object obj = getSubjct().getPrincipal();
            if (StringUtils.isNotNull(obj))        {
                User user = new User();
                BeanUtils.copyBeanProp(user, obj);
                return user;
            }
            else        {
                return null;
            }
        } catch (Exception e){
            return null;
        }
    }

    public static void setUser(User user)
    {
        Subject subject = getSubjct();
        PrincipalCollection principalCollection = subject.getPrincipals();
        String realmName = principalCollection.getRealmNames().iterator().next();
        PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
        // 重新加载Principal
        subject.runAs(newPrincipalCollection);
    }

    public static void clearCachedAuthorizationInfo()
    {
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        UserRealm realm = (UserRealm) rsm.getRealms().iterator().next();
        realm.clearCachedAuthorizationInfo();
    }

    public static Integer getUserId()
    {
        return getUser().getId();
    }

    public static String getLoginName()
    {
        if(getUser() == null) {
            return null;
        }
        return getUser().getLoginName();
    }

    public static String getWarehouseCode()
    {
        return  getUser() == null? QuantityConstant.WAREHOUSECODE :getUser().getWarehouseCode();
    }

    public static List<Integer> getCompanyIdList()
    {
        return getUser().getCompanyIdList();
    }

    public static List<String> getCompanyCodeList()
    {
        return getUser().getCompanyCodeList();
    }

    public static String getIp()
    {
        return getSubjct().getSession().getHost();
    }

    public static String getSessionId()
    {
        return String.valueOf(getSubjct().getSession().getId());
    }

    /**
     *  获取list重复数据
     * @param stream
     * @param <T>
     * @return
     */
    public static <T> List<T> getDuplicateElements(Stream<T> stream){
        return stream
                .collect(Collectors.toMap(e -> e, e -> 1, (a, b) -> a + b))
                // 获得元素出现频率的 Map,键为元素,值为元素出现的次数
                .entrySet().stream()
                // Set<Entry>转换为Stream<Entry>
                .filter(entry -> entry.getValue() > 1)
                // 过滤出元素出现次数大于 1 的 entry
                .map(entry -> entry.getKey())
                // 获得 entry 的键(重复元素)对应的 Stream
                .collect(Collectors.toList());
                // 转化为 List
    }
}