我是 Apache Shiro 的初学者。我一直在关注文档和许多其他教程、博客等,但我就是无法让身份验证工作。当我尝试使用有效的用户名和密码登录时,总是会抛出 InvalidCredentialsException
。我使用 DynamoDB 作为存储用户凭据的自定义领域,但我真的认为这并不重要。显然,我存储和/或进行凭证匹配的方式不正确。这是我的设置:
Shiro.ini:
[main]
myRealm = com.enki.closing.users.DynamoDBRealm
credentialsMatcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
credentialsMatcher.storedCredentialsHexEncoded = false
credentialsMatcher.hashIterations = 1024
myRealm.credentialsMatcher = $credentialsMatcher
创建用户帐户:
String password = ...
ByteSource passwordSalt = new SecureRandomNumberGenerator().nextBytes();
String hashedPasswordBase64 = new Sha256Hash(password, passwordSalt, 1024).toBase64();
// store the hashedPassword and salt in DynamoDB...
// I've tried storing the salt with and without base64 encoding.
密码和盐在 DynamoDB 中良好存储,值看起来没问题。这是用于身份验证的自定义领域:
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken userPass = (UsernamePasswordToken) token;
String username = userPass.getUsername();
...
// pull the matching password and salt out of DynamoDB, no problems...
ByteSource passwordSalt = ByteSource.Util.bytes( storedPasswordSalt );
return new SimpleAuthenticationInfo(username, passwordHash, passwordSalt, getName());
}
这几乎就是文档告诉我要做的事情,但有些地方不对。当我尝试登录时,出现 InvalidCredentialsException
。
最佳答案
我想出了如何让它发挥作用。我必须更改此设置(在我的自定义领域实现中):
ByteSource passwordSalt = ByteSource.Util.bytes( storedPasswordSalt );
对此:
ByteSource passwordSalt = ByteSource.Util.bytes(
Base64.decode( storedPasswordSalt) );
关于java - Apache Shiro 的身份验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26317659/