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;
}
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();