java – shiro with jdbc和散列密码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – shiro with jdbc和散列密码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3301字,纯文字阅读大概需要5分钟。
内容图文
这是我的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提供的散列工具并将确切的输出复制到数据库字段“password”中,整个字符串将包含使用什么算法的信息迭代次数等,例如:
$shiro1$SHA-256$500000$salthere$hashhere
解决方法:
是的,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具有相同的配置.
内容总结
以上是互联网集市为您收集整理的java – shiro with jdbc和散列密码全部内容,希望文章能够帮你解决java – shiro with jdbc和散列密码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。