shiro登录过程学习

边看项目代码边学习shiro
业务代码中获得主题对象
shiro登录过程学习
subject.login()方法会跳转到securityManager执行,我们这里绑定的是defaultWebSecurityManager。类图如下。
shiro登录过程学习
defaultWebSecurityManager调用父类defaultSecurityManager的login方法
shiro登录过程学习
其中的authenticate方法来自于AuthenticationgSecurityManager中的authenticator,abstractAuthenticator(ModularRealmAuthenticator)调用doAuthenticate,根据realm数量决定时single还是multi。
shiro登录过程学习
Authenticator的职责是验证用户帐号,是Shiro API中身份验证核心的入口点:shiro登录过程学习
ModularRealmAuthenticator实现,可委托给多个Realm进行验证shiro登录过程学习
验证规则通过AuthenticationStrategy接口指定,默认提供的实现:shiro登录过程学习
FirstSuccessfulStrategy:只要有一个Realm验证成功即可,只返回第一个Realm身份验证成功的认证信息,其他的忽略;

AtLeastOneSuccessfulStrategy:只要有一个Realm验证成功即可,和FirstSuccessfulStrategy不同,返回所有Realm身份验证成功的认证信息;

AllSuccessfulStrategy:所有Realm验证成功才算成功,且返回所有Realm身份验证成功的认证信息,如果有一个失败就失败了。
realm通过AuthenticationRealm的getAuthenticationInfo得到登录信息
shiro登录过程学习
自定义的类通过继承AuthorizingRealm类,重写doGetAuthorizationInfo和doGetAuthenticationInfo这两个方法就行了。
shiro登录过程学习
shiro登录过程学习
总结:
1、业务代码中 subject.login();
2、自动委托给securityManager:
自动委托是如何实现的:在delegatingSubject(subject接口的一个实现类)中:
shiro登录过程学习
3、在securityManager的login方法中,核心是authenticate方法,这个方法是由authenticator接口定义的,抽象类abstractAuthenticator实现了这个方法,定义了抽象方法doAuthenticate.
4、ModularRealmAuthenticator继承了abstractAuthenticator。实现了doAuthenticate.其中的核心方法是Realm接口定义的getAuthenticationInfo。
5、realm的实现类AuthenticationRealm和AuthorizingRealm分别定义了两个抽象方法,doGetAuthenticationInfo和doGetAuthorizingInfo,自定义realm需要做的就是继承AuthorizingRealm,并实现这两个方法。