我正在使用 shiro,并且我使用哈希凭证作为我的凭证。
这是我的 shiro.ini 配置:
credentialsMatcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
credentialsMatcher.storedCredentialsHexEncoded = false
credentialsMatcher.hashIterations = 1024
realmA.credentialsMatcher = $credentialsMatcher
securityManager.realms = $realmA
下面是我生成盐和散列密码的方法:
RandomNumberGenerator rng = new SecureRandomNumberGenerator();
ByteSource salt = rng.nextBytes();
String passwordsalt=salt.toBase64();
String hashedPasswordBase64 = new Sha256Hash(user.getPassword(),
salt, 1024).toBase64();
user.setPassword(hashedPasswordBase64);
user.setByteTabSalt(passwordsalt);
dao.createUser(user);
这是我扩展的领域:
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authToken;
User user = dao.getForUsername(token.getUsername());
if (user != null) {
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
user.getEmail_account(), user.getPassword(), getName());
ByteSource salt = new SimpleByteSource(Base64.decode(user
.getByteTabSalt()));
info.setCredentialsSalt(salt);
return info;
} else {
return null;
}
}
但是当我使用新生成的帐户登录时,我从未成功。 调试结果是我正确地获得了用户对象。有什么想法吗?
非常感谢。
最佳答案
HashedCredentialsMatcher
是一个较旧的 Shiro 概念。相反,我强烈建议使用 PasswordService
及其对应的 PasswordMatcher
,如下所述:
https://shiro.apache.org/static/1.2.2/apidocs/org/apache/shiro/authc/credential/PasswordService.html
关于jsf - apache shiro 使用哈希凭证无法成功登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17048153/