2020-10-22 17:13:10 +08:00
|
|
|
|
package com.ruoyi.common.utils;
|
2018-07-09 08:44:52 +08:00
|
|
|
|
|
|
|
|
|
import org.apache.shiro.SecurityUtils;
|
2018-10-07 14:16:47 +08:00
|
|
|
|
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
|
2018-07-09 08:44:52 +08:00
|
|
|
|
import org.apache.shiro.session.Session;
|
|
|
|
|
import org.apache.shiro.subject.Subject;
|
2018-11-22 09:28:06 +08:00
|
|
|
|
import org.apache.shiro.subject.PrincipalCollection;
|
|
|
|
|
import org.apache.shiro.subject.SimplePrincipalCollection;
|
2020-10-22 17:13:10 +08:00
|
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
2018-07-12 18:07:04 +08:00
|
|
|
|
import com.ruoyi.common.utils.bean.BeanUtils;
|
2018-07-09 08:44:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* shiro 工具类
|
|
|
|
|
*
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
public class ShiroUtils
|
|
|
|
|
{
|
2018-12-19 18:10:44 +08:00
|
|
|
|
public static Subject getSubject()
|
2018-07-09 08:44:52 +08:00
|
|
|
|
{
|
|
|
|
|
return SecurityUtils.getSubject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Session getSession()
|
|
|
|
|
{
|
|
|
|
|
return SecurityUtils.getSubject().getSession();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void logout()
|
|
|
|
|
{
|
2018-12-19 18:10:44 +08:00
|
|
|
|
getSubject().logout();
|
2018-07-09 08:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-18 18:17:40 +08:00
|
|
|
|
public static SysUser getSysUser()
|
2018-07-09 08:44:52 +08:00
|
|
|
|
{
|
2018-10-07 14:16:47 +08:00
|
|
|
|
SysUser user = null;
|
2018-12-19 18:10:44 +08:00
|
|
|
|
Object obj = getSubject().getPrincipal();
|
2018-08-03 10:32:00 +08:00
|
|
|
|
if (StringUtils.isNotNull(obj))
|
|
|
|
|
{
|
2018-10-07 14:16:47 +08:00
|
|
|
|
user = new SysUser();
|
2018-08-03 10:32:00 +08:00
|
|
|
|
BeanUtils.copyBeanProp(user, obj);
|
|
|
|
|
}
|
2018-07-12 18:07:04 +08:00
|
|
|
|
return user;
|
2018-07-09 08:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-18 18:02:20 +08:00
|
|
|
|
public static void setSysUser(SysUser user)
|
2018-07-09 08:44:52 +08:00
|
|
|
|
{
|
2018-12-19 18:10:44 +08:00
|
|
|
|
Subject subject = getSubject();
|
2018-11-22 09:28:06 +08:00
|
|
|
|
PrincipalCollection principalCollection = subject.getPrincipals();
|
|
|
|
|
String realmName = principalCollection.getRealmNames().iterator().next();
|
|
|
|
|
PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
|
|
|
|
|
// 重新加载Principal
|
|
|
|
|
subject.runAs(newPrincipalCollection);
|
2018-07-09 08:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Long getUserId()
|
|
|
|
|
{
|
2018-11-18 18:17:40 +08:00
|
|
|
|
return getSysUser().getUserId().longValue();
|
2018-07-09 08:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getLoginName()
|
|
|
|
|
{
|
2018-11-18 18:17:40 +08:00
|
|
|
|
return getSysUser().getLoginName();
|
2018-07-09 08:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getIp()
|
|
|
|
|
{
|
2018-12-19 18:10:44 +08:00
|
|
|
|
return getSubject().getSession().getHost();
|
2018-07-09 08:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getSessionId()
|
|
|
|
|
{
|
2018-12-19 18:10:44 +08:00
|
|
|
|
return String.valueOf(getSubject().getSession().getId());
|
2018-07-09 08:44:52 +08:00
|
|
|
|
}
|
2018-10-07 14:16:47 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成随机盐
|
|
|
|
|
*/
|
|
|
|
|
public static String randomSalt()
|
|
|
|
|
{
|
|
|
|
|
// 一个Byte占两个字节,此处生成的3字节,字符串长度为6
|
|
|
|
|
SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
|
|
|
|
|
String hex = secureRandom.nextBytes(3).toHex();
|
|
|
|
|
return hex;
|
|
|
|
|
}
|
2018-07-09 08:44:52 +08:00
|
|
|
|
}
|