zoukankan      html  css  js  c++  java
  • ruoyi ShiroUtils

    package com.ruoyi.framework.util;
    
    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.crypto.SecureRandomNumberGenerator;
    import org.apache.shiro.mgt.RealmSecurityManager;
    import org.apache.shiro.session.Session;
    import org.apache.shiro.subject.Subject;
    import org.apache.shiro.subject.PrincipalCollection;
    import org.apache.shiro.subject.SimplePrincipalCollection;
    
    import com.ruoyi.common.utils.StringUtils;
    import com.ruoyi.common.utils.bean.BeanUtils;
    import com.ruoyi.framework.shiro.realm.UserRealm;
    import com.ruoyi.system.domain.SysUser;
    
    /**
     * shiro 工具类
     * 
     * @author ruoyi
     */
    public class ShiroUtils
    {
        public static Subject getSubject()
        {
            return SecurityUtils.getSubject();
        }
    
        public static Session getSession()
        {
            return SecurityUtils.getSubject().getSession();
        }
    
        public static void logout()
        {
            getSubject().logout();
        }
    
        public static SysUser getSysUser()
        {
            SysUser user = null;
            Object obj = getSubject().getPrincipal();
            if (StringUtils.isNotNull(obj))
            {
                user = new SysUser();
                BeanUtils.copyBeanProp(user, obj);
            }
            return user;
        }
    
        public static void setSysUser(SysUser user)
        {
            Subject subject = getSubject();
            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 Long getUserId()
        {
            return getSysUser().getUserId().longValue();
        }
    
        public static String getLoginName()
        {
            return getSysUser().getLoginName();
        }
    
        public static String getIp()
        {
            return getSubject().getSession().getHost();
        }
    
        public static String getSessionId()
        {
            return String.valueOf(getSubject().getSession().getId());
        }
    
        /**
         * 生成随机盐
         */
        public static String randomSalt()
        {
            // 一个Byte占两个字节,此处生成的3字节,字符串长度为6
            SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
            String hex = secureRandom.nextBytes(3).toHex();
            return hex;
        }
    }
  • 相关阅读:
    第五周作业
    作业4
    20182302 2019-2020-1 《数据结构与面向对象程序设计》实验3报告
    作业四
    实验二
    实验一
    排序大集合java
    阿里面试——运筹优化工程师
    树的子结构判断
    剑指offer——合并两个排序的链表——对象、引用和赋值初接触
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11788769.html
Copyright © 2011-2022 走看看