Java 使用jdbc和哈希密码的shiro
这是我的shiro配置Java 使用jdbc和哈希密码的shiro,java,jsp,jdbc,hash,shiro,Java,Jsp,Jdbc,Hash,Shiro,这是我的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.permissionsLookup
[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密码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的更新版本更容易使用。您可以非常轻松地配置其内部配置:
[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
创建帐户或更新帐户密码时,可以在应用程序中使用密码服务的实例来创建密码哈希:
String submittedPlaintextPassword = ...
String encryptedValue = passwordService.encryptPassword(submittedPlaintextPassword);
...
userAccount.setPassword(encryptedValue);
userAccount.save(); //create or update to your data store
只需确保在shiro.ini
中配置的密码服务与应用程序代码中使用的passwordService
具有相同的配置。是否有方法将传递的密码记录到shiro.ini?我的应用程序总是返回,我传递的密码是错误的。。。我使用了更复杂的哈希。。。my db中的密码如下所示:$shiro1$SHA-256$1028$8Q4AlwW/3NloawqM4ijdQQ==$DWEW96WYRASHJA/vKCDFtSanDrw44L3wF1/DXPrJrtio=