Getting the basic principles

NOTE. Due to subsequent research, this issue has been completely restructured.

I am trying to get values ​​from a Shiro PrincipalCollection object. I added two principals to the collection. 'Username' and 'UUID'. When I try to recall them, I get SimplePrincipalCollection size = 1, and this, in turn, has principles like LinkedHashMap size = 2.

Question: how can I directly get the principles?

+3
source share
1 answer

Two additional principles are not necessary for this purpose. You can create a simple object (POJO) containing all the necessary information and use it as a single 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;
}

, , getPrinciple() getter (POJO):

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

All Articles