apache shiro 使用 Hashing Credentials 无法成功登录
Posted
技术标签:
【中文标题】apache shiro 使用 Hashing Credentials 无法成功登录【英文标题】:apache shiro using Hashing Credentials can not make login successfully 【发布时间】:2013-06-07 12:59:48 【问题描述】:我正在使用 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;
但是当我使用新生成的帐户登录时,我从来没有成功过。 调试结果是我正确获得了用户对象。有什么想法吗?
非常感谢。
【问题讨论】:
【参考方案1】:HashedCredentialsMatcher
是一个较旧的 Shiro 概念。相反,我强烈建议使用PasswordService
,它对应的PasswordMatcher
如此处所述:
https://shiro.apache.org/static/1.2.2/apidocs/org/apache/shiro/authc/credential/PasswordService.html
【讨论】:
谢谢。那么credentialsMatcher和PasswordMatcher的关系呢? @neptunePasswordMatcher
是 CredentialsMatcher
的实现,它只是委托给 PasswordService
。您仍然需要它 - 它只是从通用CredentialsMatcher
接口到特定密码PasswordService
的桥梁。
@Recurse 这个答案是如何被否决的?这是没有意义的。答案本身是有效且有用的——您对文档的任何抱怨都是一个单独的问题,应该在用户列表中表达出来。或者更好的是,鉴于 Shiro 是一个开源项目,我们很乐意接受补丁以提供您希望提供的任何帮助。 (我们真的很乐意提供您可能愿意提供的任何帮助)。
@Recurse 也是,你是什么意思几乎完全没有记录。链接的 JavaDoc 非常详尽 - 它甚至提供了多个代码示例!【参考方案2】:
问题已解决,我需要在我的自定义 EnvironmentLoaderListener 中设置凭据匹配器:
WebEnvironment environment = super.createEnvironment(pServletContext);
RealmSecurityManager rsm = (RealmSecurityManager) environment
.getSecurityManager();
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
matcher.setHashAlgorithmName(Sha256Hash.ALGORITHM_NAME);
matcher.setHashIterations(1024);
matcher.setStoredCredentialsHexEncoded(false);
【讨论】:
以上是关于apache shiro 使用 Hashing Credentials 无法成功登录的主要内容,如果未能解决你的问题,请参考以下文章