检索Shiro校长 [英] Retrieving Shiro Principals

查看:104
本文介绍了检索Shiro校长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:由于后来的研究,这个问题已经完全重组.

NOTE: Due to subsequent research this question has been completely restructured.

我正在尝试从Shiro的主题PrincipalCollection中检索值.我在集合中添加了两个主体. 用户名"和"UUID".当我尝试回忆这些时,我得到一个大小为1的SimplePrincipalCollection,而这又将其主体作为大小为2的LinkedHashMap.

I am trying to retrieve values from Shiro's subject PrincipalCollection. I have added two principals to the collection. 'Username' and 'UUID'. When I try to recall these I get a SimplePrincipalCollection of size = 1 and this in turn has the principals as a LinkedHashMap of size = 2.

问题是如何直接检索主体?

Question is how can I retrieve the principals directly?

推荐答案

为此,无需两个添加多个原则.您可以创建一个包含所有所需信息的简单对象(PO​​JO),并将其用作唯一原则.

There is no need two add multiple principles for this purpose. You can create a simple object (POJO) containing all the information you need and use it as the only principle.

public class MyRealm extends JdbcRealm {

...
enter code here


@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    SimpleAuthenticationInfo info = null;
    try {
        //GET USER INFO FROM DB etc. here
        MyPrinciple USER_OBJECT = new MyPrinciple();
        USER_OBJECT.setId(UUID);
        USER_OBJECT.setUsername(username);
        info = new SimpleAuthenticationInfo(USER_OBJECT, password.toCharArray(), getName());

    } catch (IOException | SQLException e) {
        logger.error(message, e);
        throw new AuthenticationException(message, e);
    }

    return info;
}

然后,当您需要登录用户的用户信息时,只需将其转换为用户类(POJO)后,只需调用getPrinciple()并使用其getter方法即可:

Then when you need the user info for the logged in user, you can simply call getPrinciple() and use its getter methods after casting it to your user class (POJO):

MyPrinciple LoggedInUser = (MyPrinciple ) SecurityUtils.getSubject().getPrinciple();
long uid = LoggedInUser.getId();
String username = LoggedInUser.getUsername();

这篇关于检索Shiro校长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆