一.步骤总结
1.修改ShiroConfig
1)编写创建匹配器的方法
/**
* 获取md5凭证匹配器
* @return 凭证匹配器
*/
@Bean
public CredentialsMatcher md5CredentialsMatcher(){
HashedCredentialsMatcher credentialsMatcher=new HashedCredentialsMatcher();
//用的是MD5算法
credentialsMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);
//迭代次数
credentialsMatcher.setHashIterations(3);
return credentialsMatcher;
}
2.将凭证匹配器设置给myRealm
@Bean
public DefaultWebSecurityManager defaultWebSecurityManager(CredentialsMatcher md5CredentialsMatcher,
RememberMeManager rememberMeManager,
CacheManager cacheManager,
CredentialsMatcher sha1CredentialsMatcher){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//添加密码凭证
myRealm.setCredentialsMatcher(md5CredentialsMatcher);
securityManager.setRealm(myRealm);
return securityManager;
}
3.修改MyRealm中doGetAuthenticationInfo方法。把盐添加进去。(第三个参数)
SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(user,user.getPassword(),ByteSource.Util.bytes(user.getSalt()), "myrealm");