代码注释
A {@code SecurityManager} executes all security operations for all Subjects (aka users) across a single application.
SecurityManager在单个应用程序中执行所有Subjects(也就是用户)的所有安全操作。
通过上面描述大概可知SecurityManager执行的是Susbject中所有的操作,也就是说Subject是借用SecurityManager中的方法进行数据处理的。
下图就是SecurityManager类图,可以看到,SecurityManager继承了Authenticator, Authorizer, SessionManager三个接口,并且向下泛化了WebSecurityManager接口;依次实现了CachingSecurityManager、RealmSecurityManager、AuthenticatingSecurityManager、AuthorizingSecurityManager、SessionsSecurityManager等抽象类和DefaultSecurityManager、DefaultWebSecurityManager两个具体类。
在这个过程中SecurityManager增强了CachingManager、Realm、Authenticating、Authorizing和SessionManager的功能。
SecurityManager API(接口)
-
Subject login(Subject subject, AuthenticationToken authenticationToken) throws AuthenticationException;
使用给定的authenticationToken在指定的Subject中进行身份验证,如果成功则返回一个更新验证状态的Subject实例,如果不成功则抛出AuthenticationException。是由DelegatingSubject中的login()方法对此方法进行调用,用于登录验证。建议不要直接调用该方法,而是使用Subject中的login()方法进行验证。
-
void logout(Subject subject);
从系统中注销指定的Subject。
-
Subject createSubject(SubjectContext context);
创建一个Subject实例,表明指定的上下文数据。SubjectContext 是SecurityManager创建Subject所需要的上下文数据,并且不应该直接调用该方法,而是借用Subject.Builder进行创建Subject,或者实现SubjectFactory接口,进行创建Subject。每次新建的Subject必须绑定到具体的上下文中,因为Subject实例只具有局部作用域,必须防止超出其作用域的方法进行调用。
CachingSecurityManager API(抽象类)
SecurityManager接口的一个非常基本的起点,它只提供日志和缓存支持。所有的SecurityManager方法实现都留给子类。
- 构造方法
提供了无参构造方法,在构造方法里面,新实例化了一个DefaultEventBus。
- private CacheManager cacheManager;
CachingSecurityManager 内部定义的缓存器管理类,用于执行缓存操作以提高性能。可以为空。在CachingSecurityManager 中实现了CacheManagerAware接口的setCacheManager()方法,并提供了get方法。需注意的是,在setCacheManager之后,会判断cacheManager是否实现EventBusAware接口,如果是则将eventBus加入cacheManager中。
- private EventBus eventBus;
EventBus用于发布和接收Shiro生命周期中的事件。在CachingSecurityManager实现了EventBusAware的setEventBus方法,并提供对应的getEventBus方法。同样,在setEventBus之后会判断cacheManager是否实现EventBusAware接口,如果是则将eventBus加入cacheManager中。
- public void destroy() {}
实现了Destroyable接口的destroy方法,在此方法中,将cacheManager置空,eventBus重新new一个实例。
RealmSecurityManager API(抽象类)
此抽象类是基于对Realm集合的支持,所有的SecurityManager方法实现都留给子类。
- private Collection realms;
RealmSecurityManager内部的Realm集合,提供了get和set方法,并且在set完成之后,对继承CacheManagerAware和EventBusAware的Realm分别设置CacheManager和EventBus。重新了CachingSecurityManager
的afterCacheManagerSet和afterEventBusSet方法,实现类容同上。
- 构造方法