Apache Shiro源码(SecurityManager)

Apache Shiro的SecurityManager是应用程序中所有安全操作的核心,它集成了身份验证、授权、会话管理和缓存管理等功能。SecurityManager接口定义了如login、logout和createSubject等关键方法。其下包括CachingSecurityManager、RealmSecurityManager、AuthenticatingSecurityManager、AuthorizingSecurityManager和SessionsSecurityManager等抽象类,以及DefaultSecurityManager和DefaultWebSecurityManager的具体实现。这些实现类增强了缓存、Realm、认证、授权和会话管理的能力。在Web环境中,DefaultWebSecurityManager提供了与HTTP Session的集成。
摘要由CSDN通过智能技术生成

代码注释

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

SecurityManager API(接口)

  1. Subject login(Subject subject, AuthenticationToken authenticationToken) throws AuthenticationException;

    使用给定的authenticationToken在指定的Subject中进行身份验证,如果成功则返回一个更新验证状态的Subject实例,如果不成功则抛出AuthenticationException。是由DelegatingSubject中的login()方法对此方法进行调用,用于登录验证。建议不要直接调用该方法,而是使用Subject中的login()方法进行验证。

  2. void logout(Subject subject);

    从系统中注销指定的Subject。

  3. Subject createSubject(SubjectContext context);

    创建一个Subject实例,表明指定的上下文数据。SubjectContext 是SecurityManager创建Subject所需要的上下文数据,并且不应该直接调用该方法,而是借用Subject.Builder进行创建Subject,或者实现SubjectFactory接口,进行创建Subject。每次新建的Subject必须绑定到具体的上下文中,因为Subject实例只具有局部作用域,必须防止超出其作用域的方法进行调用。

CachingSecurityManager API(抽象类)

SecurityManager接口的一个非常基本的起点,它只提供日志和缓存支持。所有的SecurityManager方法实现都留给子类。

  1. 构造方法

提供了无参构造方法,在构造方法里面,新实例化了一个DefaultEventBus。

  1. private CacheManager cacheManager;

CachingSecurityManager 内部定义的缓存器管理类,用于执行缓存操作以提高性能。可以为空。在CachingSecurityManager 中实现了CacheManagerAware接口的setCacheManager()方法,并提供了get方法。需注意的是,在setCacheManager之后,会判断cacheManager是否实现EventBusAware接口,如果是则将eventBus加入cacheManager中。

  1. private EventBus eventBus;

EventBus用于发布和接收Shiro生命周期中的事件。在CachingSecurityManager实现了EventBusAware的setEventBus方法,并提供对应的getEventBus方法。同样,在setEventBus之后会判断cacheManager是否实现EventBusAware接口,如果是则将eventBus加入cacheManager中。

  1. public void destroy() {}

实现了Destroyable接口的destroy方法,在此方法中,将cacheManager置空,eventBus重新new一个实例。

RealmSecurityManager API(抽象类)

此抽象类是基于对Realm集合的支持,所有的SecurityManager方法实现都留给子类。

  1. private Collection realms;

RealmSecurityManager内部的Realm集合,提供了get和set方法,并且在set完成之后,对继承CacheManagerAware和EventBusAware的Realm分别设置CacheManager和EventBus。重新了CachingSecurityManager
的afterCacheManagerSet和afterEventBusSet方法,实现类容同上。

  1. 构造方法
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值