apache shiro 无法使用 PasswordMatcher 检查 md5 密码
apache shiro cannot check md5 password with PasswordMatcher
我正在使用 shiro 1.4.0。
我的密码是MD5,如果我使用HashedCredentialsMatcher
,那么我可以登录成功:
[main]
shiro.loginUrl = /login.jsp
shiro.successUrl = /home.jsp
passwordMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
passwordMatcher.hashAlgorithmName=MD5
passwordMatcher.storedCredentialsHexEncoded=true
ds = com.mchange.v2.c3p0.ComboPooledDataSource
ds.driverClass = com.mysql.jdbc.Driver
ds.jdbcUrl = jdbc:mysql://localhost:3306/simple_shiro_web_app
ds.user = test
ds.password = 123456
jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled = true
jdbcRealm.authenticationQuery = SELECT password FROM USERS WHERE username = ?
jdbcRealm.userRolesQuery = SELECT role_name FROM USERS_ROLES WHERE username = ?
jdbcRealm.permissionsQuery = SELECT permission_name FROM ROLES_PERMISSIONS WHERE role_name = ?
jdbcRealm.credentialsMatcher = $passwordMatcher
jdbcRealm.dataSource=$ds
securityManager.realm = $jdbcRealm
但是如果我使用PasswordMatcher
(在tomcat启动时没有任何错误消息),那么我登录失败:
passwordMatcher = org.apache.shiro.authc.credential.PasswordMatcher
passwordService = org.apache.shiro.authc.credential.DefaultPasswordService
passwordService.hashService.hashAlgorithmName=MD5
passwordMatcher.passwordService = $passwordService
好像还是用默认的SHA-256
,为什么?
此外,在1.4中,在shiro-core.jar
和shiro-crypto-hash.jar
中有相同的class和相同的包名(例如org.apache.shiro.crypto.hash.DefaultHashService.class
),有什么区别和为什么?
---------------- 已更新 ------------------ -----
有一条日志消息:
TRACE ClassUtils.forName - Unable to load class named [e10adc3949ba59abbe56e057f20f] from the current ClassLoader. Trying the system/application ClassLoader...
虽然e10adc3949ba59abbe56e057f20f
是我的md5密码
您的日志中有任何警告吗?如果不是 LdapRealm.doGetAuthenticationInfo()
,看看您是否得到了您期望的密码。
在 1.4 中,一些代码已移至其他模块(尽管它们将以与 1.4 之前相同的方式解析)
通过debug发现DefaultPasswordService
使用了base64
hash格式。
在我将哈希格式更改为 hex
后,它就可以工作了。
<bean id="hexFormat" class="org.apache.shiro.crypto.hash.format.HexFormat">
</bean>
<bean id="passwordService" class="org.apache.shiro.authc.credential.DefaultPasswordService">
<property name="hashService" ref="hashService" />
<property name="hashFormat" ref="hexFormat" />
</bean>
我正在使用 shiro 1.4.0。
我的密码是MD5,如果我使用HashedCredentialsMatcher
,那么我可以登录成功:
[main]
shiro.loginUrl = /login.jsp
shiro.successUrl = /home.jsp
passwordMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
passwordMatcher.hashAlgorithmName=MD5
passwordMatcher.storedCredentialsHexEncoded=true
ds = com.mchange.v2.c3p0.ComboPooledDataSource
ds.driverClass = com.mysql.jdbc.Driver
ds.jdbcUrl = jdbc:mysql://localhost:3306/simple_shiro_web_app
ds.user = test
ds.password = 123456
jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled = true
jdbcRealm.authenticationQuery = SELECT password FROM USERS WHERE username = ?
jdbcRealm.userRolesQuery = SELECT role_name FROM USERS_ROLES WHERE username = ?
jdbcRealm.permissionsQuery = SELECT permission_name FROM ROLES_PERMISSIONS WHERE role_name = ?
jdbcRealm.credentialsMatcher = $passwordMatcher
jdbcRealm.dataSource=$ds
securityManager.realm = $jdbcRealm
但是如果我使用PasswordMatcher
(在tomcat启动时没有任何错误消息),那么我登录失败:
passwordMatcher = org.apache.shiro.authc.credential.PasswordMatcher
passwordService = org.apache.shiro.authc.credential.DefaultPasswordService
passwordService.hashService.hashAlgorithmName=MD5
passwordMatcher.passwordService = $passwordService
好像还是用默认的SHA-256
,为什么?
此外,在1.4中,在shiro-core.jar
和shiro-crypto-hash.jar
中有相同的class和相同的包名(例如org.apache.shiro.crypto.hash.DefaultHashService.class
),有什么区别和为什么?
---------------- 已更新 ------------------ -----
有一条日志消息:
TRACE ClassUtils.forName - Unable to load class named [e10adc3949ba59abbe56e057f20f] from the current ClassLoader. Trying the system/application ClassLoader...
虽然e10adc3949ba59abbe56e057f20f
是我的md5密码
您的日志中有任何警告吗?如果不是 LdapRealm.doGetAuthenticationInfo()
,看看您是否得到了您期望的密码。
在 1.4 中,一些代码已移至其他模块(尽管它们将以与 1.4 之前相同的方式解析)
通过debug发现DefaultPasswordService
使用了base64
hash格式。
在我将哈希格式更改为 hex
后,它就可以工作了。
<bean id="hexFormat" class="org.apache.shiro.crypto.hash.format.HexFormat">
</bean>
<bean id="passwordService" class="org.apache.shiro.authc.credential.DefaultPasswordService">
<property name="hashService" ref="hashService" />
<property name="hashFormat" ref="hexFormat" />
</bean>