/**
* 身份认证
* @param authenticationToken
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// 第一种方式
// 获取用户输入的账号和密码(一般只需要获取账号就可以)
String username = (String) authenticationToken.getPrincipal();
String password = new String((char[]) authenticationToken.getCredentials());
// 第二种方式(推荐第一种)
// UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
// String username = usernamePasswordToken.getUsername();
// String password = new String(usernamePasswordToken.getPassword());
// 通过username从数据库中查找 User对象,如果找到则进行验证
TUser user = userService.selectUserByName(username);
// 判断账号是否存在
if (user == null){
throw new AuthenticationException();
}
// 判断账号是否被冻结
if (user.getState() ==null || user.getState().equals("PROHIBIT")){
throw new LockedAccountException();
}
// 进行验证
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
// 用户名
user,
// 密码
user.getPassword(),
// 盐值
ByteSource.Util.bytes(user.getSalt()),
getName()
);
return authenticationInfo;
}
Shiro 身份认证中心获取token中账号密码的两种方式
最新推荐文章于 2024-06-18 07:28:07 发布