权限调整

This commit is contained in:
RuoYi
2020-10-22 17:13:10 +08:00
parent 207126847e
commit 685e090f2f
54 changed files with 152 additions and 120 deletions

View File

@ -0,0 +1,86 @@
package com.ruoyi.common.utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
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.core.domain.entity.SysUser;
import com.ruoyi.common.utils.bean.BeanUtils;
/**
* 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 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;
}
}