为此目的,不需要两个添加多个原则.您可以创建一个包含所需信息的简单对象(POJO),并将其作为唯一原则.
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;
}
Run Code Online (Sandbox Code Playgroud)
然后,当您需要登录用户的用户信息时,只需调用getPrinciple()并在将其转换为用户类(POJO)后使用其getter方法:
MyPrinciple LoggedInUser = (MyPrinciple ) SecurityUtils.getSubject().getPrinciple();
long uid = LoggedInUser.getId();
String username = LoggedInUser.getUsername();
Run Code Online (Sandbox Code Playgroud)