【问题标题】:Shiro matching credentialsMatcher and user password creation not matchingShiro 匹配的 credentialsMatcher 和用户密码创建不匹配
【发布时间】:2017-04-25 12:43:33
【问题描述】:

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

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

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

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

我的用户创建代码

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=

【问题讨论】:

    标签: java security shiro sha256


    【解决方案1】:

    要获得与密码匹配的密码,必须在 ini 中创建 DefaultPasswordService 对象,然后将其设置为 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 2019-07-12
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      相关资源
      最近更新 更多