一个困扰我n多回的大bug,
如果是配置了credentialsMatcher对密码进行加密,但是数据库存储的密码是明文的话,会报异常AuthenticationException,然后出一条odd ...
但是我的情况是配置了credentialsMatcher,密码也进行加密了,并且加密算法、散列次数都一一对应,但就是报错IncorrectCredentialsException,也就是密码认证错误。
首先在shiro.xml中配置的credentialsMatcher:
<bean id="userRealm" class="shiro.security.UserRealm">
<!--引入的credentialsMatcher对密码进行加密-->
<property name="credentialsMatcher" ref="credentialsMatcher" />
</bean>
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!--加密算法-->
<property name="hashAlgorithmName" value="md5" />
<!--散列次数-->
<property name="hashIterations" value="1" />
</bean>
没有毛病,然后给password加密的PasswordHelper.java:
public class PasswordHelper {
private String algorithmName = "md5";
private final int hashIterations = 1;
public String encryptPassword(String password) {
//没有使用salt
SimpleHash newPass = new SimpleHash(algorithmName, password,null, hashIterations);
return newPass.toString();
}
}
加密算法,散列次数都可以对象,也没有毛病。
然而输入正确的用户名、密码后,直接报错IncorrectCredentialsException,我都快崩溃了,
调用subject.login(),shiro会对用户名密码进行匹配,匹配的过程是比较存储在AuthenticationToken和AuthenticationInfo存储的用户名密码。
分别取出AuthenticationToken和AuthenticationInfo里面的credential在控制台打印,明明都是一样的,还是报错IncorrectCredentialsException。
实在是没有办法了,只能不再配置credentialsMatcher,然后手动对密码加密,也就是在前台接收password后,在LoginController中对密码进行加密,然后存入AuthenticationInfo,在从数据库中取出加密后的password,存入AuthenticationInfo就ok了。
这也算是曲线救国,没有办法的事。。。。