会话获取Principal

dep_wangsr
RuoYi 6 years ago committed by Limy
parent 9569430059
commit 5dcfb8343a

@ -127,8 +127,7 @@ public class UserRealm extends AuthorizingRealm
log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage()); log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
throw new AuthenticationException(e.getMessage(), e); throw new AuthenticationException(e.getMessage(), e);
} }
ShiroUtils.getSession().setAttribute("sysUser", user); SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getLoginName(), password, getName());
return info; return info;
} }

@ -5,6 +5,8 @@ import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.mgt.RealmSecurityManager; import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session; import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject; 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.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils; import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.shiro.realm.UserRealm; import com.ruoyi.framework.shiro.realm.UserRealm;
@ -35,7 +37,7 @@ public class ShiroUtils
public static SysUser getSysUser() public static SysUser getSysUser()
{ {
SysUser user = null; SysUser user = null;
Object obj = getSession().getAttribute("sysUser"); Object obj = getSubjct().getPrincipal();
if (StringUtils.isNotNull(obj)) if (StringUtils.isNotNull(obj))
{ {
user = new SysUser(); user = new SysUser();
@ -46,7 +48,12 @@ public class ShiroUtils
public static void setSysUser(SysUser user) public static void setSysUser(SysUser user)
{ {
ShiroUtils.getSession().setAttribute("sysUser", 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() public static void clearCachedAuthorizationInfo()

Loading…
Cancel
Save