`
chyou1988
  • 浏览: 6777 次
社区版块
存档分类
最新评论

身份认证流程及原理

 
阅读更多

验证身份的对象元素

在shiro中,用户需要提供principals (身份)和credentials(证明)给shiro,从而应用能验证用户身份:
principals:身份,即主体的标识属性,可以是任何东西,如用户名、邮箱等,唯一即可。一个主体可以有多个principals,但只有一个Primary principals,一般是用户名/密码/手机号。
credentials:证明/凭证,即只有主体知道的安全值,如密码/数字证书等。

认证流程

securiyManager是验证开始的地方,但从数据源取数据并作比较的工作是由Realm来进行的

ModularRealmAuthenticator:

protected AuthenticationInfo doMultiRealmAuthentication(Collection<Realm> realms, AuthenticationToken token) {
    //获取认证策略
    AuthenticationStrategy strategy = getAuthenticationStrategy();
    AuthenticationInfo aggregate = strategy.beforeAllAttempts(realms, token);
    if (log.isTraceEnabled()) {
      log.trace("Iterating through {} realms for PAM authentication", realms.size());
    }
    //循环realm
    for (Realm realm : realms) {

      aggregate = strategy.beforeAttempt(realm, token, aggregate);

      if (realm.supports(token)) {

        log.trace("Attempting to authenticate token [{}] using realm [{}]", token, realm);

        AuthenticationInfo info = null;
        Throwable t = null;
        try {

          //关键:调用realm的getAuthenticationInfo进行认证,token为用户的token
          info = realm.getAuthenticationInfo(token);
        } catch (Throwable throwable) {
          t = throwable;
          if (log.isDebugEnabled()) {
            String msg = "Realm [" + realm + "] threw an exception during a multi-realm authentication attempt:";
            log.debug(msg, t);
          }
        }

        aggregate = strategy.afterAttempt(realm, token, info, aggregate, t);

      } else {
        log.debug("Realm [{}] does not support token {}.  Skipping realm.", realm, token);
      }
    }

    aggregate = strategy.afterAllAttempts(token, aggregate);

    return aggregate;
  }

 在Realm开始处理验证的逻辑之前,Authenticator将调用Realm的 supports 方法去验证当前Realm是否支持获得的AuthenticationToken。

boolean supports (AuthenticationToken token);

通常,Realm检查的是token的类型,比如在 AuthenticatingRealm 中检查类型是否相同。

    public boolean supports(AuthenticationToken token) {
        return token != null && getAuthenticationTokenClass().isAssignableFrom(token.getClass());
    }

 
另外,AuthenticatingRealm的constructor中类型默认为

authenticationTokenClass = UsernamePasswordToken .class;

如果当前Realm支持提交过来的token,authenticator则调用 getAuthenticationInfo (token) 方法。

 以 AuthenticatingRealm 为例(注意是final):

  public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

  	//先从缓存中取
  	//token为需验证的用户信息(前台传来的需要验证的用户)
  	//info为根据需验证的用户信息(前台传来的需要验证的用户)获得校验的用户验证信息(数据源中获得的正确的用户信息)
    AuthenticationInfo info = getCachedAuthenticationInfo(token);
    if (info == null) {
      //otherwise not cached, perform the lookup:
      //自定义Realm的扩展点
      //根据需要验证的用户信息获得正确的用户信息(获得方式:数据库,配置文件等)
      info = doGetAuthenticationInfo(token);
      log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", info);
      if (token != null && info != null) {
        cacheAuthenticationInfoIfPossible(token, info);
      }
    } else {
      log.debug("Using cached authentication info [{}] to perform credentials matching.", info);
    }

    if (info != null) {
      //真正的验证,token,info
      //此验证内,如果验证不对,则抛出异常
      assertCredentialsMatch(token, info);
    } else {
      log.debug("No AuthenticationInfo found for submitted AuthenticationToken [{}].  Returning null.", token);
    }

    return info;
  }

 有些人直接在doGetAuthenticationInfo该方法中完成也验证,验证通过时返回SimpleAuthenticationInfo实例,失败则抛出相应的验证异常。

但下面有个 assertCredentialsMatch ,说明doGetAuthenticationInfo本没有打算这样用,这种使用方式会让 CredentialMatcher 失去意义。

protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
    //获得CredentialsMatcher,有多种实现
    CredentialsMatcher cm = getCredentialsMatcher();
    if (cm != null) {
      //扩展点
      //认证失败抛出异常
      if (!cm.doCredentialsMatch(token, info)) {
        //not successful - throw an exception to indicate this:
        String msg = "Submitted credentials for token [" + token + "] did not match the expected credentials.";
        throw new IncorrectCredentialsException(msg);
      }
    } else {
      throw new AuthenticationException("A CredentialsMatcher must be configured in order to verify " +
          "credentials during authentication.  If you do not wish for credentials to be examined, you " +
          "can configure an " + AllowAllCredentialsMatcher.class.getName() + " instance.");
    }
  }

 AuthenticatingRealm的默认CredentialMatcher是SimpleCredentialsMatcher

public AuthenticatingRealm() {
    this(null, new SimpleCredentialsMatcher());
}

 

protected boolean equals(Object tokenCredentials, Object accountCredentials) {
  if (log.isDebugEnabled()) {
      log.debug("Performing credentials equality check for tokenCredentials of type [" +
        tokenCredentials.getClass().getName() + " and accountCredentials of type [" +
        accountCredentials.getClass().getName() + "]");
  }
  if (isByteSource(tokenCredentials) && isByteSource(accountCredentials)) {
      if (log.isDebugEnabled()) {
    	log.debug("Both credentials arguments can be easily converted to byte arrays.  Performing " +
      	"array equals comparison");
      }
      byte[] tokenBytes = toBytes(tokenCredentials);
      byte[] accountBytes = toBytes(accountCredentials);
      return Arrays.equals(tokenBytes, accountBytes);
  } else {
      return accountCredentials.equals(tokenCredentials);
  }
}

 当然,实现类还有HashedCredentialsMatcher,使用密码加密的方式提高安全性

<!--EndFragment-->

 

分享到:
评论

相关推荐

    YD T 3532-2019 移动应用身份认证总体技术要求 工业和信息化部.pdf

    本标准描述了移动互联网时代移动应用对用户进行身份认证的技术原理,规定了移动应用身份认证 (MAA)体系结构,制定了移动应用身份认证(MAA)所需要支持的组件功能、消息流程、接口协议 等方面的业务能力总体技术要求...

    一次性身份验证.rar

    1. 一次性身份验证的工作原理 2. 挑战应战的过程 3. 用C#实现一次性身份验证 4. 散列算法MD5应用 5. winform界面化设计

    基于用户令牌实现Web服务身份验证

    !"# 服务技术被称为下一代!"# 的主流技术,但!"# 服务的安全性问题一直是人们..."# 服务调用者进行身份验证是其中的一个主要问题。本文将主要介绍在!+, 环境下使用用户令牌实现!"# 服务访问 控制的基本原理和实现过程。

    JBPM工作原理及表结构详解

    3.3身份认证表结构 4 4、数据库逻辑关系: 5 4.1 资源库与运行时的表: 5 4.2 历史数据表: 6 5、表结构: 7 5.1表JBPM4_DEPLOYMENT 7 5.2 表JBPM4_DEPLOYPROP 7 5.3 表JBPM4_EXECUTION 8 5.4 表JBPM4_HIST_ACTINST ...

    java8源码-GoogleAuth_Android:仿谷歌身份验证器

    仿谷歌身份验证器 查找资料 验证器的适用场景: 用户的密码可能会因为各种原因泄漏。 为此谷歌推出了 Google Authenticator 服务, 其原理是在登录时除了输入密码外, 还需根据 Google Authenticator APP 输入一个实时...

    深入研究Windows内部原理系列之七:开机引导过程.zip

    讲座内容:Windows的启动是一个复杂的过程,从加载器(NTLDR...本讲座将解析以上各个步骤的来龙去脉,并探讨驱动的加载顺序、用户登录(Gina,SAM数据库,域身份验证)、系统服务程序、Shell等等启动过程密切相关的问题。

    数据库原理及应用技术(论文+源代码)

    2.《数据库原理及应用技术》课程指导平台系统分析 2.1 系统需求分析 2.1.1 用户身份级别要求 2.1.2 系统功能模块设置 2.2 系统设计目标 2.3 系统的开发工具 2.3.1 开发环境 2.3.2 编程运行环境 3.《数据库...

    windows 内部原理(一)

    本讲座将解析以上各个步骤的来龙去脉,并探讨驱动的加载顺序、用户登录(Gina,SAM数据库,域身份验证)、系统服务程序、Shell等等启动过程密切相关的问题。 深入研究Windows内部原理系列之八:内存管理揭秘 讲师...

    WINDOWS 内部原理 (八)

    本讲座将解析以上各个步骤的来龙去脉,并探讨驱动的加载顺序、用户登录(Gina,SAM数据库,域身份验证)、系统服务程序、Shell等等启动过程密切相关的问题。 深入研究Windows内部原理系列之八:内存管理揭秘 讲师...

    WINDOWS 内部原理(九)

    本讲座将解析以上各个步骤的来龙去脉,并探讨驱动的加载顺序、用户登录(Gina,SAM数据库,域身份验证)、系统服务程序、Shell等等启动过程密切相关的问题。 深入研究Windows内部原理系列之八:内存管理揭秘 讲师...

    基于WindowsServer2003配置IIS站点的身份验证研究 (2013年)

    介绍了信息服务器(InternetInformationServices,IIS)的安装流程,详细阐述了IIS的身份验证机制。实例分析表明,采用基于WindowsServer2003配置IIS站点的身份验证方法,能够确保站点在Internet网络中处于安全可靠的状态...

    SSL&TLS认证原理详解

    加密方式分为对称加密和不对称加密。...本文章会详细讲解SSL/TLS密钥签发与公钥分发过程,SSL/TLS基本的运行过程(客户端发出请求、服务器回应、客户端回应过程、服务器的最后回应过程及整个过程图解)

    Windows Server 2016 联合身份验证管理

    掌握 Windows Server 2016 中联合身份验证的部署,群集,升级过程,掌握 ADFS 基于声明的身份验证的原理和配置过程,掌握 ADFS 与 Azure Active Directory 的集成,实现的单点登录,多重身份验证,设备验证以及代理...

    WINDOWS 内部原理(十)驱动和硬件的管理

    本讲座将解析以上各个步骤的来龙去脉,并探讨驱动的加载顺序、用户登录(Gina,SAM数据库,域身份验证)、系统服务程序、Shell等等启动过程密切相关的问题。 深入研究Windows内部原理系列之八:内存管理揭秘 讲师...

    7、DRF实战总结:JWT认证原理和使用,以及第三方库simplejwt 的详解源码

    JSON Web Token(JWT)是一种用于认证和授权的开放标准,允许在客户端和服务器之间传递信息,以验证用户身份和授权访问特定资源。它定义了一种紧凑且自包含的方式,用于各方之间安全地将信息以JSON对象传输。由于此...

    蓝牙工作原理全面解析-LawrenceHarte.zip

    您将了解蓝牙设备如何自动定位附近的蓝牙设备,对其进行身份验证,发现其功能以及用于与其建立连接的过程。 了解蓝牙的扩频技术如何使其与其他设备一起运行,包括无线局域网,微波炉,无绳电话和无线摄像机。您...

    django-allauth:解决身份验证,注册,帐户管理以及第三方(社交)帐户身份验证的集成Django应用程序集

    这种方法将本地身份验证和社会身份验证分开。 但是,在这两个世界中都有一些常见的场景需要处理。 例如,由OpenID提供程序传递的电子邮件地址不能保证得到验证。 因此,在将OpenID帐户挂接到本地帐户之前,必须先...

    模拟实现了TLS通信过程的全流程

    原理可概括为:client通过验证server身份并分享会话密钥,然后通过只有server可client知道的会话密钥进行通信。全流程如下(client建立与server的TLS通信): 1. client验证server证书合法性,client对server的可信...

    Windows内部原理(十一):存储和文件系统

    本讲座将解析以上各个步骤的来龙去脉,并探讨驱动的加载顺序、用户登录(Gina,SAM数据库,域身份验证)、系统服务程序、Shell等等启动过程密切相关的问题。 深入研究Windows内部原理系列之八:内存管理揭秘 讲师...

    IPv4IPv6安全网关原理及应用分析.doc

    IPv6取代IPv4已经成为公认的事实,然而这将是一个长期的、渐进的过程。IPv6的部署大致要经历一个过程。初始阶段,在IPv4的网络海洋中,会出现若干局部零散的IPv6孤岛,为了保持通信,这些孤岛通过跨越IPv4的隧道彼此...

Global site tag (gtag.js) - Google Analytics