Shiro与JDBC和哈希密码

4
这是我的shiro配置:
[main]
authc.loginUrl = /site/index.jsp
authc.usernameParam = user
authc.passwordParam = pass
authc.rememberMeParam = remember
authc.successUrl = /site/home.jsp


jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled=true
jdbcRealm.authenticationQuery = select password from users where username = ?
jdbcRealm.userRolesQuery = select role from users where username = ?

credentialsMatcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName = SHA-256
credentialsMatcher.storedCredentialsHexEncoded = true
credentialsMatcher.hashIterations = 5000
jdbcRealm.credentialsMatcher = $credentialsMatcher



jof = org.apache.shiro.jndi.JndiObjectFactory
jof.resourceName = jdbc/postgres
jof.requiredType = javax.sql.DataSource
jof.resourceRef = true
jdbcRealm.dataSource = $jof
securityManager.realms = jdbcRealm

[urls]
/theme/** = anon
/site/** = authc
/site/cards.jsp = roles[smoto,admin]
/site/jobs.jsp = roles[admin]

我用以下方式为管理员密码admin创建了哈希

String hashedPassword = new Sha256Hash("admin", "",5000).toHex();

我将哈希值插入数据库,但每次身份验证都失败,有人有使用Shiro进行此类设置的经验吗?另外,我该如何启用Shiro的调试或日志记录?

编辑: 在另一个stackoverflow帖子中找到了此类身份验证的正确设置。

[main]
authc.loginUrl = /site/index.jsp
authc.usernameParam = user
authc.passwordParam = pass
authc.rememberMeParam = remember
authc.successUrl = /site/home.jsp

jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled=false
jdbcRealm.authenticationQuery = select password from users where username = ?
jdbcRealm.userRolesQuery = select role from users where username = ?

ps = org.apache.shiro.authc.credential.DefaultPasswordService
pm = org.apache.shiro.authc.credential.PasswordMatcher
pm.passwordService = $ps

jof = org.apache.shiro.jndi.JndiObjectFactory
jof.resourceName = jdbc/postgres
jof.requiredType = javax.sql.DataSource
jof.resourceRef = true

jdbcRealm.dataSource = $jof
jdbcRealm.credentialsMatcher = $pm

#securityManager.realms = jdbcRealm

[urls]
/theme/** = anon
/site/** = authc
/site/cards.jsp = roles[smoto,admin]
/site/jobs.jsp = roles[admin]

关键是使用shiro提供的哈希工具,并将确切输出复制到数据库字段“密码”中,整个字符串将包含有关使用了哪种算法以及进行了多少次迭代等信息,例如:

$shiro1$SHA-256$500000$salthere$hashhere
1个回答

6

是的,HashedCredentialsMatcher虽然足够使用,但有点老旧。您可能会发现Shiro的新PasswordMatcher更容易使用。您可以很容易地配置其内部的PasswordService

[main]
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
#...
# Finally, set the matcher on a realm that requires password matching for account authentication:
myRealm = ...
myRealm.credentialsMatcher = $passwordMatcher

您可以在应用程序中使用PasswordService的实例,在创建帐户或更新帐户密码时创建密码哈希值:
String submittedPlaintextPassword = ...
String encryptedValue = passwordService.encryptPassword(submittedPlaintextPassword);
...
userAccount.setPassword(encryptedValue);
userAccount.save(); //create or update to your data store

请确保在shiro.ini中配置的passwordService与应用程序代码中使用的passwordService具有相同的配置。


有没有办法记录已传递的密码到shiro.ini?我的应用程序总是返回错误密码...我使用更复杂的哈希函数...我的数据库中的密码看起来像:$shiro1$SHA-256$1028$8Q4AlwW/3NloawqM4ijdQQ==$DWE96wyrASHjA/vKCDFtSanDrw44L3wF1/DXPrJrtio= - Marcel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接