Shiro匹配credentialsMatcher和用户密码创建不匹配

时间:2016-12-09 23:45:17

标签: java security shiro sha256

我正在使用shiro作为我的java 1.8应用程序的身份验证。我的用户创建将是sha256和salt。

Shiro只会在输入的数据库中准确匹配密码。例如,如果数据库密码是纯文本密码并且密码是'我输入了密码'它会起作用。

如果我输入了密码'当密码在数据库中加密时,它不匹配并且会失败。

如何让shiro根据输入的内容创建sha256和salted密码,以便密码匹配?

我的用户创建代码

EntityManagerFactory factory = 
                    Persistence.createEntityManagerFactory("e");

            EntityManager em = factory.createEntityManager();
            em.getTransaction().begin();

            com.e.dto.User user = new com.e.dto.User();

            DefaultPasswordService a = new DefaultPasswordService();
            password = a.encryptPassword(password);

            user.setUsername(username);
            user.setPassword(password);

            em.persist(user);

        em.getTransaction().commit();

shiro.ini

jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.authenticationQuery = SELECT password from user where username = ?
jdbcRealm.userRolesQuery = select role from userroles where userID = (select id FROM user WHERE username = ?)

ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
ds.serverName = localhost
ds.user = root
ds.password = password
ds.databaseName = myDatabase
jdbcRealm.dataSource= $ds

credentialsMatcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName = SHA-256
credentialsMatcher.storedCredentialsHexEncoded = true
credentialsMatcher.hashIterations = 10000
credentialsMatcher.hashSalted = true

新用户密码

$shiro1$SHA-256$500000$xRvz5dByhvAtFG7VHlCjHA==$xxakvEZdBF6cI+UmyR1OY098tAlscOKhpwQuT7THijw=

1 个答案:

答案 0 :(得分:1)

要获取与DefaultPasswordService对象匹配的密码必须在ini中创建,然后设置为org.apache.shiro.authc.credential.PasswordMatcher passwordService

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

passwordService = org.apache.shiro.authc.credential.DefaultPasswordService
# configure the passwordService to use the settings you desire

passwordMatcher = org.apache.shiro.authc.credential.PasswordMatcher
passwordMatcher.passwordService = $passwordService