示例代码
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
String username = token.getUsername();
// token中获得username,再去查数据库
Users users = userService.findUsersByUsername(username);
if (users == null) {
throw new UnknownAccountException("不存在用户");
} else {
// 肯定有这个用户就比对密码
//当前realm对象的name
String name = this.getName();
System.out.println("realmName:" + name);
ByteSource credentialsSalt = ByteSource.Util.bytes(users.getUserName());
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(users, users.getPassword(), credentialsSalt, name);
return info;
}
new SimpleAuthenticationInfo(users, users.getPassword(), credentialsSalt, name)解释参数
参数1:从数据库获得的用户对象,包括用户名和密码等信息
参数2:从对象中取密码,users.getPassword()是这个用户的数据库中的密码
参数3:盐,可以为空
参数4:当前realm的名字