You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.5 KiB
Java
63 lines
1.5 KiB
Java
package com.ruoyi.common.utils.security;
|
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
import org.apache.shiro.subject.PrincipalCollection;
|
|
import org.apache.shiro.subject.SimplePrincipalCollection;
|
|
import org.apache.shiro.subject.Subject;
|
|
|
|
import com.ruoyi.project.system.user.domain.User;
|
|
|
|
/**
|
|
* shiro 工具类
|
|
*
|
|
* @author ruoyi
|
|
*/
|
|
public class ShiroUtils
|
|
{
|
|
|
|
public static Subject getSubjct()
|
|
{
|
|
return SecurityUtils.getSubject();
|
|
}
|
|
|
|
public static void logout()
|
|
{
|
|
getSubjct().logout();
|
|
}
|
|
|
|
public static User getUser()
|
|
{
|
|
return (User) getSubjct().getPrincipal();
|
|
}
|
|
|
|
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 Long getUserId()
|
|
{
|
|
return getUser().getUserId().longValue();
|
|
}
|
|
|
|
public static String getLoginName()
|
|
{
|
|
return getUser().getLoginName();
|
|
}
|
|
|
|
public static String getIp()
|
|
{
|
|
return getSubjct().getSession().getHost();
|
|
}
|
|
|
|
public static String getSessionId()
|
|
{
|
|
return String.valueOf(getSubjct().getSession().getId());
|
|
}
|
|
}
|