第九讲 Shiro密码的比对时机
参考网址:http://blog.csdn.net/acmman/article/details/78446008
从第二讲我们可以看到,shiro的密码比对是在main()中给出账号和密码,交给realm,realm再组成认证信息SimpleAuthenticationInfo交给管验证的CredentialsMatcher。
管验证的CredentialsMatcher又分两种验证方法,一个是随机生成盐的PasswordMatcher,另一个是自定义盐的HashedCredentialsMatcher。具体使用哪一个,要看你的配置文件中是如何配置的了,比如:
passwordMatcher=org.apache.shiro.authc.credential.PasswordMatcher
passwordMatcher.passwordService=$passwordService
如果是这样的配置,那么就说,你使用的验证方式是随机生成的盐。具体的对比,要看PasswordMatcher中调用了哪个函数。
web编程模式下,Shiro认证流程是怎样的呢?前端页面form表单提交账号和密码,Controller获得账号密码封装成token,再获取当前用户的Subject对象,执行了Subject.login(token)进行登录shiro权限系统,登录后,由框架转到认证的Realm类,该Realm类继承了AuthenticatingRealm,实现了doGetAuthenticationInfo方法,在doGetAuthenticationInfo方法中通过token获取用户的账号密码后,加上当前realm的名字,形成认证信息SimpleAuthenticationInfo,并返回给框架。
这里要注意的是,我们的realm是继承了AuthorizingRealm,而AuthorizingRealm双继承了AuthenticatingRealm。在AuthenticatingRealm中有一个assertCredentialsMatch()方法,它调用了doCredentialsMatch(token, info)方法,这个方法的实现是:
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
Object tokenHashedCredentials = hashProvidedCredentials(token, info);
Object accountCredentials = getCredentials(info);
return equals(tokenHashedCredentials, accountCredentials);
}
可以看到它对token和info进行了对比。
这里的AuthenticationToken token 是在账号,密码,realm名的基础上加了角色,权限后封装成的对象;
AuthenticationInfo info相当于是用户输入的账号密码和realm组成的信息;
这样就完成了认证过程,注意这个过程并没有涉及到角色和权限,加密问题。关于加密请关注后续的文章。
金庭波于2018年3月17日上午。
还没有评论,来说两句吧...