Shiro是怎么搞的?

作者: 爪哇部落格 | 来源:发表于2019-07-10 00:06 被阅读12次

你们系统的权限控制是如何实现的呢?在权限管理方面有两个比较出名的框架,一个是来自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的持久化操作
  • 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 接口继承了我们前文提到的AuthenticatorAuthorizerSessionManager,如下是其继承实现关系;

SecrityManager
  • CachingSecurityManager实现了缓存管理
  • RealmSecurityManagerCachingSecurityManager支持了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 包下的核心设计思想以及实现,由于篇幅的限制,并不能对每个组件进行详尽的描述,感兴趣的读者可以自行深入研究,欢迎一起来探讨交易。

参考文章:
shiro官方http://shiro.apache.org/architecture.html

相关文章

  • Shiro是怎么搞的?

    你们系统的权限控制是如何实现的呢?在权限管理方面有两个比较出名的框架,一个是来自Spring 的Security ...

  • Shiro入门

    Shiro 什么是Shiro? Apache Shiro 是一个Java的安全框架。 关于shiro的相关概念: ...

  • SpringBoot+Shiro+JWT

    SpringBoot+Shiro+JWT 一、Shiro 1、什么是shiro? Apache Shiro是一个强...

  • Shiro初识

    以下为Shiro的初识,主要讲解的是什么是Shiro以及使用Shiro与其他相似的框架的好处 什么是Shiro? ...

  • Shiro1-基础知识

    什么是Shiro Apache Shiro 是 Java 的一个安全框架。目前,使用 Apache Shiro 的...

  • SpringBoot整合Shiro

    SpringBoot整合Shiro Apache Shiro Apache Shiro是一个功能强大、灵活的...

  • shiro1.1_介绍

    原文翻译 Apache Shiro 介绍 什么是Apache Shiro? Apache Shiro是一个强大并且...

  • Shiro简介

    1、什么是Shiro Apache Shiro是Java的一个安全框架。目前,使用Apache Shiro的人越来...

  • 1、shiro基础概念

    shiro简介 1. 什么是shiro Aparche shiro 是java的一个安全框架,属于Aparche...

  • Shiro框架的入门介绍

    什么是Shiro? Apache Shiro是Java的一个安全框架。目前,使用Apache Shiro的人越来越...

网友评论

    本文标题:Shiro是怎么搞的?

    本文链接:https://www.haomeiwen.com/subject/tmptkctx.html