先粘出登录的代码

1.

可以看到已经获取到了username和password ,为了接下来的认证过程,我们需要获取subject对象,也就是代表当前登录用户,并且要将username和password两个变量设置到UsernamePasswordToken对象的token中, 调用SecurityUtils.getSubject().login(token)方法,将 token传入

接下来看看login方法的实现:

主要还是用到了securityManager安全管理器

进入securityManager里边的login方法,看看他的实现:

在这个方法中定义了AuthenticationInfo对象来接受从Realm传来的认证信息

进入authenticate方法中

发现调用了authenticator的authenticate这个方法

进入this.authenticator.authenticate(token)这个方法中

在这个 类中调用了这个方法,再进去看他的实现

  1.  
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
  2.  
    this.assertRealmsConfigured();
  3.  
    Collection<Realm> realms = this.getRealms();
  4.  
    return realms.size() == 1?this.doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken):this.doMultiRealmAuthentication(realms, authenticationToken);
  5.  
    }

在这里才是刚才前边的那个authenticator的实现, this.assertRealmsConfigured() 这个方法是判断realm是否存在,不存在则抛出异常,他会根据realm的个数来判断执行哪个方法,上篇中springboot整合shiro我只配置了一个realm,所以他只会执行this.doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken)这个方法,并且会将 realm和token作为参数传入,这里的realm其实就是自己定义的MyShiroRealm

接下来再进入doSingleRealmAuthentication这个方法中,

在这里 他会先判断realm是否支持token

接下来执行else中的getAuthenticationInfo方法

this.getCachedAuthenticationInfo(token)这个方法是从shiro缓存中读取用户信息,如果没有,才从realm中获取信息。如果是第一次登陆,缓存中肯定没有认证信息,所以会执行this.doGetAuthenticationInfo(token)这个方法。

查看this.doGetAuthenticationInfo(token)方法,会发现有这么几个类提供我们选择

其中就有我们自定义的realm,进去

所以 ,上边的doGetAuthorizationInfo是 执行的我们自定义realm中重写的doGetAuthorizationInfo这个方法。这个方法就会从数据库中读取我们所需要的信息,最后封装成SimpleAuthorizationInfo返回去。

现在获取到认证信息了,接下来就是shiro怎么去进行认证,我们返回去看

获取 完信息之后就是进行密码匹配,进入assertCredentialsMatch方法中看一下,

首先 获取一个CredentialsMatcher对象,译为凭证匹配器,这个类的主要作用就是将用户输入的密码一某种计算加密。

再看一下cm.doCredentialsMatch(token,info)

这里会用到equals方法对token中加密的密码和从数据库中取出来的info中的密码进行对比,如果认证相同就返回true,失败就返回false,并抛出AuthenticationException,将info返回到defaultSecurityManager中,到此认证过程结束。

原文链接:https://blog.csdn.net/caoyang0105/article/details/82769293

shiro登录认证过程讲解的更多相关文章

  1. spring boot(十四)shiro登录认证与权限管理

    这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...

  2. Shiro 登录认证源码详解

    Shiro 登录认证源码详解 Apache Shiro 是一个强大且灵活的 Java 开源安全框架,拥有登录认证.授权管理.企业级会话管理和加密等功能,相比 Spring Security 来说要更加 ...

  3. springboot+mybatis+shiro——登录认证和权限控制

    转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...

  4. Linux的SSH免密登录认证过程研究

    一.先看下SSH免密登录使用到的工具和生成的文件 工具:ssh-keygen用于生成秘钥文件,其中秘钥分为公钥和私钥.ssh-copy-id用于复制公钥文件到被控制机. 文件:ssh-keygen生成 ...

  5. shiro登录认证

    新建maven文件create a simple project shiro.ini文件 [users] root=123456 log4j.properties log4j.rootLogger=I ...

  6. Spring Cloud之路:(七)SpringBoot+Shiro实现登录认证和权限管理

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sage_wang/article/details/79592269一.Shiro介绍1.Shiro是 ...

  7. 学习Spring Boot:(十三)配置 Shiro 权限认证

    经过前面学习 Apache Shiro ,现在结合 Spring Boot 使用在项目里,进行相关配置. 正文 添加依赖 在 pom.xml 文件中添加 shiro-spring 的依赖: <d ...

  8. 2、Shiro的认证

    Shiro的认证流程大体可以参考下面这幅图: 但是没有接触过shiro的同学看到上面的图片也不明白,下面我们来在代码中尝试体验Shiro的认证过程: 1.新建一个SpringBoot项目项目结构如下: ...

  9. springboot系列(十)springboot整合shiro实现登录认证

    关于shiro的概念和知识本篇不做详细介绍,但是shiro的概念还是需要做做功课的要不无法理解它的运作原理就无法理解使用shiro: 本篇主要讲解如何使用shiro实现登录认证,下篇讲解使用shiro ...

随机推荐

  1. PHP错误日志文件Warning:PHP Startup: Unable to load dynamic library...

    由于我的环境是通过源码编译安装的,安装的时候配置信息和一些其他扩展没安装或设置好: php.err文件一直有这些提示,虽然不影响服务启动,但是看着心好累啊,决定要消灭他们. 问题描述: 出现原因: 上 ...

  2. SpringMVC简单小结

    一.MVC的的大概解释: MVC的核心思想是业务数据抽取和业务数据呈现相分离. MVC:M(Model)+V(View)+C(Controller) M(模型层):业务数据的信息表示,通常是业务实体 ...

  3. 洛谷P1551 亲戚 (并查集模板题)

    链接 https://www.luogu.org/problemnew/show/P1551 代码 #include<bits/stdc++.h> using namespace std; ...

  4. pytest之assert断言

    assert pytest允许您使用标准Python断言来验证Python测试中的期望和值.例如,你可以写下 # content of test_assert1.py def f(): return ...

  5. nginx proxy_set_header Host $host 和 proxy_set_header Host $http_host 的作用对比

    转载自https://www.jianshu.com/p/7a8a7eb3707a 1.浏览器直接访问服务,获取到的 Host 包含浏览器请求的 IP 和端口 测试服务器,centos 7 sudo ...

  6. [Code+#4] 最短路 - 建图优化,最短路

    最短路问题,然而对于任意\(i,j\),从\(i\)到\(j\)可以只花费\((i xor j) \cdot C\) 对每个点\(i\),只考虑到\(j\)满足\(j=i xor 2^k, j \le ...

  7. [P1361] 小M的作物 - 最小割

    没想到今天早上的第一题网络流就血了这么多发 从经典的二选一问题上魔改 仍然考虑最小割 #include <bits/stdc++.h> using namespace std; #defi ...

  8. Wannafly Camp 2020 Day 1F 乘法 - 字符串

    一开始想根据单调性双指针 后来血了才想起来负负得正 于是暴力二分答案即可 #include <bits/stdc++.h> using namespace std; #define int ...

  9. JDBC——DriverManager驱动管理对象

    功能 1.注册驱动 注册驱动:告诉程序使用哪个驱动jar包 写代码使用:Class.forName("com.mysql.jdbc.Driver"); 查看源码 mysql-con ...

  10. HTML link标签

    <link> 标签定义文档与外部资源的关系. 最常见的用途是链接 样式表.CSS