Shiro源码(一)——整体组件讲解

一、核心功能

身份验证: 登录信息与数据库信息对比认证,判断是否要登录成功
授权: 判断登录用户有哪些权限,可以访问哪些资源,不可以访问哪些资源
密码: 用户密码的加密解密处理
会话管理:类似与http的session会话的管理功能。

从上面几个核心功能可以看出,shiro框架的作用就是帮助我们实现系统用户的登录和权限功能,也就是说,我们使用shiro框架,遵循shiro框架的规范,那么系统的用户权限功能,就不用再单独开发了,交由Shiro提供的功能完成即可。这就是框架所起到的作用。

shiro框架使用起来还算很简单的。那么,shiro的实现原理是什么呢?这就有必要进行深入研究一下了。

二、整体组件介绍

先看两张图,了解shiro的内外部结构。
在这里插入图片描述
这张图是shiro的宏观结构,应用程序使用shiro时,调用subject对象的api即可满足需求。subject内部,是通过SecurityManager对象进行工作身份认证,授权,密码和会话工作的。Realm组件是用于存储用户信息的组件,一般与数据库进行交互,获取用户的身份信息。

下面,看一张shiro内部结构图:
在这里插入图片描述
这张图分成三层来看,第一层,subject层,应用程序调用subject对象使用shiro框架,可以看到,shiro不仅支持java应用程序,还支持C/C++等多种应该程序使用,可见shiro框架的强大之处。
第二层:SecurityManager层,它里面包含了shiro核心功能的各个组件,如:Authenticator(认证)、Authorizer(授权)、Session(会话)、Cache(缓存)、Realm(域)。可见,SecurityManager在shiro中起到综合调控的作用。
第三层就是各种数据库层,从数据库中获取到用户身份信息,进行身份认证和授权等操作。

三、各组件源码

上面提到,shiro的核心功能和各个组件,我们有必要了解一下每个组件的内部源码,这样对理解shiro的工作原理和流程大有帮助。

  1. Subject

首先,看Subject类源码,应用程序调用的都是这个类,所以我们使用shiro打交道最多的就是这个类。

一个subject对象代表一个用户,subject的操作包括认证(登录,退出),授权和会话管理。
可以通过SecurityUtils的getSubject()方法获取到Subject对象

这是Subject类注释翻译,可以看出,subject里包含了认证,授权,会话等核心操作。下面看Subject接口定义的规范:

  • 获取当前用户登录信息:
Object getPrincipal();

这个方法返回当前用户的登录信息,返回类型之所以是Object,是因为,这个用户信息,可以是一个用户对象,也可以是用户名字符串,或者是一个用户id,等等。总之,可以是任何标识用户身份的信息,可以让应用程序自己去定义。

PrincipalCollection getPrincipals();

这个方法获取到用户信息的集合。在shiro中,Principal就代表用户信息。

  1. 权限相关API
    在这里插入图片描述
  2. 角色相关API:
    在这里插入图片描述
  3. 认证和session相关API
    在这里插入图片描述
    通过以上API可以看出,Subject里包含了用户,角色,权限相关的API,这三种元素,也构成了权限管理框架的核心。
  4. SecurityManager
    subject的功能是通过SecurityManager来实现的,下面看SecurityManager的类继承关系:
public interface SecurityManager extends Authenticator, Authorizer, SessionManager

可见,其继承了Authenticator(认证)、Authorizer(授权)和SessionManager(session)接口。所以,它起到了总指挥的作用,那我们就直接看这三个接口的源码。

  • Authenticator
    认证接口只定义了一个方法:
 public AuthenticationInfo authenticate(AuthenticationToken authenticationToken)
            throws AuthenticationException;

其就是获取到认证信息,返回值AuthenticationInfo 里包含了Principal信息,AuthenticationInfo 源码如下:

public interface AuthenticationInfo extends Serializable {
    
    

    /**
     * Returns all principals associated with the corresponding Subject.  Each principal is an identifying piece of
     * information useful to the application such as a username, or user id, a given name, etc - anything useful
     * to the application to identify the current <code>Subject</code>.
     * <p/>
     * The returned PrincipalCollection should <em>not</em> contain any credentials used to verify principals, such
     * as passwords, private keys, etc.  Those should be instead returned by {@link #getCredentials() getCredentials()}.
     *
     * @return all principals associated with the corresponding Subject.
     */
    PrincipalCollection getPrincipals();

    /**
     * Returns the credentials associated with the corresponding Subject.  A credential verifies one or more of the
     * {@link #getPrincipals() principals} associated with the Subject, such as a password or private key.  Credentials
     * are used by Shiro particularly during the authentication process to ensure that submitted credentials
     * during a login attempt match exactly the credentials here in the <code>AuthenticationInfo</code> instance.
     *
     * @return the credentials associated with the corresponding Subject.
     */
    Object getCredentials();

}
  • Authorizer
    授权接口,看定义了哪些方法:
    在这里插入图片描述
    可以看出,授权接口定义了角色和权限相关的API。Shiro通过权限和角色,来控制用户能够访问的资源。
  • Session
    shiro的session,在任何地方,都可以方便的获取到,不同于HttpSession那样,只能通过request获取。其API和HttpSession的API相似。

四、总结

通过上面的分析,可以知道,shiro大体的工作机制,获取到用户信息,存入Principal中,然后再根据用户的权限和角色,控制用户可以访问什么,不可以访问什么。接下来,我们具体看每个细节的实现。

おすすめ

転載: blog.csdn.net/qq1309664161/article/details/122251704