一.概述
在实际应用中数据库密码都是加密的。Shiro内置了凭证匹配器,通过简单配置就可以实现明文数据和数据库中加密数据匹配的效果。
二.步骤总结
1.修改自定义的Realm
AuthenticationInfo 的构造方法由三个参数变成四个参数的。新增第三个参数表示加盐。一般都是拿用户数据的id作为盐。
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), "myrealm");
2.修改配置文件
[main]
md5CredentialsMatcher=org.apache.shiro.authc.credential.Md5CredentialsMatcher
#迭代次数
md5CredentialsMatcher.hashIterations=3
myrealm= com.yrp.realm.MyRealmAuthorizing
myrealm.credentialsMatcher=$md5CredentialsMatcher
securityManager.realms=$myrealm