shiro与jdbc和散列密码

 don't 发布于 2023-02-08 14:55

这是我的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

Les Hazlewoo.. 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

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

1 个回答
  • 是的,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
    

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

    2023-02-08 14:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有