【问题标题】:Authenticating plain text passwords against md5 hash in DB using Apache Shiro使用 Apache Shiro 针对 DB 中的 md5 哈希验证纯文本密码
【发布时间】:2015-08-23 12:04:06
【问题描述】:

这是我的 shiro.ini。

[main]

ds = org.apache.shiro.jndi.JndiObjectFactory   
ds.requiredType = javax.sql.DataSource  
ds.resourceName = java:/comp/env/jdbc/at

credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName=MD5

credentialsMatcher.hashIterations=1024

credentialsMatcher.storedCredentialsHexEncoded=true

jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm

jdbcRealm.permissionsLookupEnabled = true 

jdbcRealm.dataSource = $ds

jdbcRealm.authenticationQuery = SELECT password FROM accounts.users WHERE username = ?

jdbcRealm.userRolesQuery = SELECT role_id FROM accounts.org_user_roles WHERE user_id = (SELECT id FROM accounts.users WHERE username = ?)

jdbcRealm.permissionsQuery = SELECT feature_id FROM accounts.role_features WHERE role_id = ?

jdbcRealm.credentialsMatcher = $credentialsMatcher


shiro.loginUrl = /at/login.htm

authc = org.apache.shiro.web.filter.authc.FormAuthenticationFilter

authc.loginUrl = /at/login.htm

logout.redirectUrl = /at/login.htm

[urls]

/at/login.htm = authc

/at/forgotpw.htm  = anon

/at/resources/** = authc

/at/tss/** = authc

/at/tde/** = authc

/at/lcs/** = authc

/at/cdt/** = authc

/at/tp/** = authc

/at/ip/** = authc

/at/dashboard/** = authc

/at/logout.htm = logout

当我进入登录页面并输入我的用户名和密码时,出现异常:

org.apache.shiro.authc.IncorrectCredentialsException:提交的令牌凭据 [org.apache.shiro.authc.UsernamePasswordToken - root, rememberMe=false] 与预期凭据不匹配。

我的数据库包含密码的 MD5 哈希值。看来验证不起作用。我不明白为什么。

【问题讨论】:

    标签: java security shiro


    【解决方案1】:

    删除以下属性使这项工作。我认为对于 MD5 哈希,我们不需要指定以下属性。

    credentialsMatcher.hashIterations=1024
    

    【讨论】:

    • 您是否使用单次哈希迭代构造了密码哈希? constructor 采用第四个参数int hashIterations,但如果省略则默认为 1。存储密码的最佳做法是salt and make the computation expensive。我建议您使用SHA256 或更好的值以及原始的hashIterations 值。
    猜你喜欢
    • 2012-03-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2022-08-03
    相关资源
    最近更新 更多