美文网首页
shiro小记

shiro小记

作者: 盛京最帅得仔 | 来源:发表于2018-05-11 13:19 被阅读0次

shiro介绍

Shiro是一个Java平台的开源权限框架,用于认证和访问授权

shiro用户验证流程

流程1:主要初始化一些关于shiro需要用到的相关对象;例如:初始化SecurityManager工厂

流程2:suject.login(token)验证用户名密码;token中带有用户名和密码信息

流程3:调用SecurityManager.login(token)

流程4:AuthenticatingRealm.getAuthenticationInfo(AuthenticationToken token)为主要的验证用户方法;AuthenticatingRealm属于抽象类,通过getAuthenticationInfo(AuthenticationToken token)方法已经封装好了验证用户信息的流程,但是暴露出一个抽象方法,即:AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token);让自定义的reamls根据自己的逻辑去获取AuthenticationInfo对象

流程5:就是流程4里面验证的具体主要信息

subject.login(token)里面的具体细节

从源码可以得知Subject是接口,实现该接口的类有:

1.Subject(接口):

    DelegatingSubject(实现类):从源码可以得知是subject默认实现,因为有login(token)方法

        WebDelegatingSubject(实现类):

2.DelegatingSubject.login():方法会调用securityManager.login(this, token)方法;

调用此方法中最重要的一个过程是调用new ModularRealmAuthenticator()对象的doAuthenticate(AuthenticationToken authenticationToken)方法

protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {            assertRealmsConfigured(); Collection realms = getRealms();

        if (realms.size() == 1) {

            return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);

        } else {

            return doMultiRealmAuthentication(realms, authenticationToken); 

        }

    }

从以上方法可以看出  getRealms()获取我们自定义配置的realms,然后依次调用

shiro里面一些主要的接口以及实现

接口:

securityManager:

    主要方法:

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

    void logout(Subject subject);

    Subject createSubject(SubjectContext context);

Realm:

    主要方法:

    String getName();

    boolean supports(AuthenticationToken token);

    AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException;

CredentialsMatcher:

      主要方法:

    boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info);

接口实现:

实现securityManager接口实现关系:

securityManager:

    CachingSecurityManager(抽象实现类):

        RealmSecurityManager(抽象实现类):

            AuthenticatingSecurityManager:(抽象实现类)

                AuthorizingSecurityManager(抽象实现类):

                    SessionsSecurityManager(抽象实现类):

                        DefaultSecurityManager(实现类):   

实现Realm接口实现关系:

Realm(接口)

    CachingReaml(抽象类):只是添加了cacheManager属性并没有实现Realm接口方法

        AuthenticatingRealm(抽象类):实现了Realm接口getAuthenticationInfo(AuthenticationToken token)方法并定义为final类型,也就是说从该层开始以下的类不能在重写该方法只能重写新引入doGetAuthenticationInfo(token)抽象方法获取AuthenticationInfo对象;引入CredentialsMatcher接口对象,通过调用该对象的doCredentialsMatch(token, info)方法实现验证;(回调函数) 注意:/**getAuthenticationInfo方法定义为final,不能被重写**/  啰嗦这么多总结一句话:就是AuthenticatingRealm类已经对getAuthenticationInfo(AuthenticationToken token)方法定制好了业务逻辑,只需要实现里面的细节

            AuthorizingRealm(抽象类):doGetAuthenticationInfo(token)实现该方法,实现自己的业务逻辑

实现CredentialsMatcher接口实现关系:

CredentialsMatcher:

    AllowAllCredentialsMatcher(实现类):doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) return true; 所有都通过验证

    PasswordMatcher(实现类):该类有passwordService对象,通过passwordService.passwordsMatch(submittedPassword, formatted)实现加密密码的验证;

    SimpleCredentialsMatcher(实现类):简单的身份验证

关联的一些重要接口:

PasswordService(接口):

    主要方法:

    //顾名思义对密码加密

    String encryptPassword(Object plaintextPassword) throws IllegalArgumentException;

    //加密密码的验证

    boolean passwordsMatch(Object submittedPlaintext, String encrypted);

    实现关系:

    HashingPasswordService(接口):实现PasswordService

        DefaultPasswordService(实现类):默认:DEFAULT_HASH_ALGORITHM = "SHA-256"

shiro权限验证流程

流程2:获取一些拦截器;其中包括:

1)RoleAnnotationMethodInterceptor;

2)PermissionAnnotationMethodInterceptor;

3)AuthenticatedAnnotationMethodInterceptor;

4)UserAnnotationMethodInterceptor;

5)GuestAnnotationMethodInterceptor;

以上就是shiro大体的用户验证与授权流程;具体细节还得看源码~

相关文章

  • shiro小记

    shiro介绍 Shiro是一个Java平台的开源权限框架,用于认证和访问授权 shiro用户验证流程 流程1:主...

  • 权限管理框架--Shiro

    1.shiro概述 1.1shiro是什么 1.2shiro能做什么 1.3Shiro特性 1.4shiro架构 ...

  • SpringBoot+Shiro+JWT

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

  • Java框架--Shiro入门

    目录 shiro 是什么? shiro 可以解决什么问题? shiro 名词解释&认证流程 shiro 快速入门 ...

  • Shiro入门

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

  • springboot + shiro +cas 集成

    shiro 认证流程图: shiro 授权流程图: shiro 认证时序图: shiro + cas 认证时序图:...

  • Shiro安全框架

    Shiro Session管理Shiro 缓存管理Shiro集成SpringShiro集成SpringBoot:h...

  • Apache Shiro 是什么 & Shiro 框架介绍

    Apache Shiro 是什么 & Shiro 框架介绍 官方介绍 Apache Shiro™ is a pow...

  • 20170927-shiro 集成spring(四)

    前景提要:shiro 集成spring(一)shiro 集成spring(二)shiro 集成spring(三) ...

  • SpringBoot整合Shiro

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

网友评论

      本文标题:shiro小记

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