how to get/set the salt for a JdbcRealm

故事扮演 提交于 2019-12-06 01:53:07
Max

With Shiro 1.2.3 all you need to do is:

  1. Extend JdbcRealm and set salt style.

    public class JdbcSaltRealm extends JdbcRealm {
        public JdbcSaltRealm() {
            setSaltStyle(SaltStyle.COLUMN);
        }
    }
    
  2. Update shiro.ini to use extended realm and to get salt column from DB

    credentialsMatcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
    credentialsMatcher.hashAlgorithmName = SHA-256
    jdbcRealm = com.mypackage.JdbcSaltRealm
    jdbcRealm.authenticationQuery = SELECT password, salt FROM user WHERE username = ?
    jdbcRealm.credentialsMatcher = $credentialsMatcher
    
  3. Hash & salt current / new user passwords. This should be done for all existing users as well as on new user registrations.

    private void saltHashPassword(String password) {
    
        String salt = new BigInteger(250, new SecureRandom()).toString(32);
    
        //TODO: save salt value to "salt" column in user table
    
        Sha256Hash hash = new Sha256Hash(password, 
                              (new SimpleByteSource(salt)).getBytes());
        String saltedHashedPassword = hash.toHex();
    
        //TODO: save saltedHashedPassword value to "password" column in user table
    }
    

I hope my answer is clear and understandable.

Maybe a bit late:

Have a look at this tutorial.
Meri, the guy who owns the blog, describes exactly how to create an own salted JDBC Realm.

This is also an acknowledged improvement in the community for version 1.3.0 .

Hope this helpes, have Fun!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!