【问题标题】:shiro with jdbc and hashed passwords使用 jdbc 和散列密码的 shiro
【发布时间】:2014-01-11 14:53:24
【问题描述】:

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

【问题讨论】:

    标签: java jsp jdbc hash shiro


    【解决方案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
    

    只要确保shiro.ini 中配置的passwordService 与您的应用程序代码中使用的passwordService 具有相同的配置即可。

    【讨论】:

    • 有什么方法可以将传递的密码记录到 shiro.ini 中?我的应用程序总是返回,我传递的密码是错误的......我使用了更复杂的哈希......我的数据库中的密码看起来像: $shiro1$SHA-256$1028$8Q4AlwW/3NloawqM4ijdQQ==$DWE96wyrASHjA/vKCDFtSanDrw44L3wF1/ DXPrJrtio=
    猜你喜欢
    • 2011-07-27
    • 2013-03-31
    • 2014-12-29
    • 2014-11-04
    • 2012-04-19
    • 1970-01-01
    • 2019-02-15
    • 2013-11-23
    • 1970-01-01
    相关资源
    最近更新 更多