2013-06-11 39 views
2

我正在使用shiro,並使用散列憑證作爲我的憑據。apache 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; 
     } 
    } 

但是當我使用我的新生成的帳戶登錄時,我從來沒有成功。 調試結果是我正確得到了用戶對象。任何想法?

非常感謝。

回答

2

HashedCredentialsMatcher是一個老概念四郎。相反,我強烈建議使用PasswordService和它對應PasswordMatcher如下記載:

https://shiro.apache.org/static/1.2.2/apidocs/org/apache/shiro/authc/credential/PasswordService.html

+0

謝謝。那麼credentialsMatcher和PasswordMatcher之間的關係怎麼樣? – neptune

+0

@neptune'PasswordMatcher'是'CredentialsMatcher'的一個實現,它只是委託給'PasswordService'。您仍然需要它 - 它只是從通用的CredentialsMatcher接口到特定於密碼的PasswordService的橋樑。 –

+0

@Recurse這個答案是如何downvoted?這是沒有意義的。答案本身是有效的和有用的 - 你可能對文檔的任何抱怨都是一個單獨的問題,應該在用戶列表中表達出來。或者更好的是,鑑於Shiro是一個開源項目,我們很樂意接受你希望提供的任何幫助的補丁。 (我們真的很想愛你可能願意提供的任何幫助)。 –

0

問題解決了,我需要設置的憑據匹配器在我的自定義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);