Shiro是怎么搞的?
你们系统的权限控制是如何实现的呢?在权限管理方面有两个比较出名的框架,一个是来自Spring 的Security ,一个则是本文的主角,来自Apache的 Shiro,本文旨在让读者对Shiro 有一个宏观的认识,了解到Shiro是如何设计和工作的。
Shiro架构
让我们分别从宏观和细节两方面来了解一下Shiro的设计里面
宏观结构
宏观
-
Subject
用户层面,这里的用户不单单指人,它也可能是第三方服务,甚至是定时任务等 -
SecrityManager
SecrityManager管理者全部的用户,以及实现协调各组件完成对用户的认证和授权 -
Realm
Realm是Shiro 跟应用安全数据之间的桥梁,你可以把它当做一个安全的数据源,这个数据源不限于数据库,也可以是配置文件,内存等
详细结构
细节
- Subject
org.apache.shiro.subject.Subject
Subject如上所述代表的是广义的用户,而不是狭义的人。
-
SecurityManager
org.apache.shiro.mgt.SecurityManager
SecurityManager是 Shiro 的核心,它协调各组件之间稳定工作,同时还管理用户,指导他们安全的执行用户操作。 -
Authenticator
org.apache.shiro.authc.Authenticator
Authenticator是一个负责对用户登录执行跟验证的组件,从其命名上也大概可以猜出是认证员 -
Authorizer
org.apache.shiro.session.mgt.SessionManager
Authorizer 是一个负责确认用户对应用访问权限的组件,即确认用户是否有操作权限,同样标准的命名然我们知道它是授权人 -
SessionManager
org.apache.shiro.session.mgt.SessionManager
SessionManager顾名思义是用于创建和管理用户Session 生命周期- SessionDAO
org.apache.shiro.session.mgt.eis.SessionDAO
SessionDAO 是用于执行 Session的持久化操作
- SessionDAO
-
CacheManager
org.apache.shiro.cache.CacheManager
CacheManager 用户创建和管理缓存实例供其他组件使用 -
Cryptography
org.apache.shiro.crypto.*
Cryptography 密码模块 -
Realms
org.apache.shiro.realm.Realm
Realms 是Shiro 与 应用安全数据 之间的桥梁,或者说连接器
Shiro 实现
SecrityManager解读
我们先来看看SecrityManager接口是如何设计的
public interface SecurityManager extends Authenticator, Authorizer, SessionManager
SecrityManager 接口继承了我们前文提到的Authenticator,Authorizer,SessionManager,如下是其继承实现关系;
SecrityManager
-
CachingSecurityManager实现了缓存管理 -
RealmSecurityManager在CachingSecurityManager支持了Realm集合 -
AuthenticatingSecurityManager则在其父类的基础上实现了认证,具体的认证工作是由Authenticator完成的 -
AuthorizingSecurityManager则是在其父类的基础上实现了授权,具体的授权工作是由Authorizer完成的 -
SessionsSecurityManager则是在其父类的基础上实现了会话管理,具体的会话管理是由SessionManager完成的 -
DefaultSecurityManager则是在SessionsSecurityManager的基础上扩展了RememberMeManager,SubjectDAO,SubjectFactory
Authenticator解读
同样的我们先看看Authenticator接口设计
public interface Authenticator {
/**认证**/
public AuthenticationInfo authenticate(AuthenticationToken authenticationToken)
throws AuthenticationException;
}
-
AuthenticationToken可以理解为登录信息,代表着即将被认证的信息,例如UsernamePasswordToken 包含着用于认证用户名,密码等信息 -
AuthenticationInfo代表认证成功以后的信息,此信息认证成功以后才会有,认证失败会抛出AuthenticationException异常
我们看看Authenticator的实现关系
Authenticator
-
AbstractAuthenticator实现authenticate认证方法,具体的实现则调用doAuthenticate方法 -
ModularRealmAuthenticator实现doAuthenticate方法,并且支持多Realm,并且支持AuthenticationStrategy策略,默认的策略是AtLeastOneSuccessfulStrategy至少一个成功。当有多个Realm时,调用doMultiRealmAuthentication方法。
Authorizer以及SessionManager解读
Authorizer的实现与Authenticator有些类似,之前提到的AuthorizingSecurityManager中,Authorizer的默认实现是ModularRealmAuthorizer,这里我们不做过多的说明。
Authorizer
对于SessionManager如何实现感兴趣的可以参考下文阅读core中的相关源码
SessionManager
SecurityUtils以及Subject
SecurityUtils的主要功能是设置和获取SecurityManager,以及获取Subject
public static Subject getSubject() {
Subject subject = ThreadContext.getSubject();
if (subject == null) {
subject = (new Subject.Builder()).buildSubject();
ThreadContext.bind(subject);
}
return subject;
}
....此处省略SecurityManager getter以及setter.....
/**Subject.Builder().buildSubject方法**/
public Subject buildSubject() {
return this.securityManager.createSubject(this.subjectContext);
}
Subject在core包下有一个实现是DelegatingSubject,而其实现的主要方式则是通过我们之前强调的核心securityManager。
总结
不知道读到这里您是否对Shiro有了一定的了解,以上便是Shiro core 包下的核心设计思想以及实现,由于篇幅的限制,并不能对每个组件进行详尽的描述,感兴趣的读者可以自行深入研究,欢迎一起来探讨交易。