Apache Shiro的身份验证问题

时间:2014-10-11 18:06:52

标签: java apache authentication shiro

我是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

1 个答案:

答案 0 :(得分:1)

我想出了如何让它发挥作用。我不得不改变这个(在我的自定义领域中):

ByteSource passwordSalt = ByteSource.Util.bytes( storedPasswordSalt );

到此:

ByteSource passwordSalt = ByteSource.Util.bytes( 
                                  Base64.decode( storedPasswordSalt) );