在作完预备的初始化和session测试后,到了作为一个权鉴别框架的核心功能部分,确认你是谁--身份认证(Authentication)。

通过提交给shiro身份信息来验证是否与储存的安全信息数据是否相符来判断用户身份的真实性

本文涉及到token的构建,框架结构下认证行为的调用,realm中授权数据的获取、登录信息比较,login过程中对已有有subject、session的处理

同样,本篇本文使用的是shiro 1.3.2版本,配合源码最佳~

官方例子代码流程如下

  1. if (!currentUser.isAuthenticated()) {
  2. UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
  3. token.setRememberMe(true);
  4. try {
  5. currentUser.login(token);
  6. } catch (UnknownAccountException uae) {
  7. log.info("There is no user with username of " + token.getPrincipal());
  8. } catch (IncorrectCredentialsException ice) {
  9. log.info("Password for account " + token.getPrincipal() + " was incorrect!");
  10. } catch (LockedAccountException lae) {
  11. log.info("The account for username " + token.getPrincipal() + " is locked. " +
  12. "Please contact your administrator to unlock it.");
  13. }
  14. // ... catch more exceptions here (maybe custom ones specific to your application?
  15. catch (AuthenticationException ae) {
  16. //unexpected condition? error?
  17. }
  18. }

3.1首先构造token,其中 UsernamePasswordToken 实现了HostAuthenticationToken和RememberMeAuthenticationToken接口,他们都继承自AuthenticationToken接口

HostAuthenticationToken接口指定了token的域,RememberMeAuthenticationToken接口指定了token是否实现RememberMe(认证跨session)

ps:shiro 认证状态区分Authenticated和Remembered,Authenticated指当前session(或流程)下已经过认证,Remembered指曾经获得认证,如果是web状态下RememberMe相关信息保存在cookie中

二者可以调用 subject.isAuthenticated() 及subject.isRemembered()区分

其中UsernamePasswordToken 的Principal(身份)概念即Username,Credentials(凭证)概念即Password

在token 中设置用户名和密码

  1. UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");

设置是否使用RememberMe(此案例中不具体起作用,只是作为示例在token中设置

  1. token.setRememberMe(true);

案例中不对host进行设置

3.2完成对token的构建设置后,便进行认证(Authentication)操作

  1. currentUser.login(token);

首先先借用官方的例图来了解一下整个的流程

1-调用subject的login入口传入token

2-subject实际调用SecurityManager的login

3-securityManager再调用authenticator实例的doAuthenticate方法,一般这个是ModularRealmAuthenticator的实例

4-判断realm的状况,如果是单realm则直接调用realm,如果是多realm则要判断认证策略(AuthenticationStrategy

5-调用对应realm中的getAuthenticationInfo实现进行认证

DelegatingSubject.login代码如下,下面对认证流程进行详细的解读

  1. public void login(AuthenticationToken token) throws AuthenticationException {
  2. clearRunAsIdentitiesInternal();
  3. Subject subject = securityManager.login(this, token);
  4.  
  5. PrincipalCollection principals;
  6.  
  7. String host = null;
  8.  
  9. if (subject instanceof DelegatingSubject) {
  10. DelegatingSubject delegating = (DelegatingSubject) subject;
  11. //we have to do this in case there are assumed identities - we don't want to lose the 'real' principals:
  12. principals = delegating.principals;
  13. host = delegating.host;
  14. } else {
  15. principals = subject.getPrincipals();
  16. }
  17.  
  18. if (principals == null || principals.isEmpty()) {
  19. String msg = "Principals returned from securityManager.login( token ) returned a null or " +
  20. "empty value. This value must be non null and populated with one or more elements.";
  21. throw new IllegalStateException(msg);
  22. }
  23. this.principals = principals;
  24. this.authenticated = true;
  25. if (token instanceof HostAuthenticationToken) {
  26. host = ((HostAuthenticationToken) token).getHost();
  27. }
  28. if (host != null) {
  29. this.host = host;
  30. }
  31. Session session = subject.getSession(false);
  32. if (session != null) {
  33. this.session = decorate(session);
  34. } else {
  35. this.session = null;
  36. }
  37. }

3.2.1首先是初始化

clearRunAsIdentitiesInternal();

如果subject已含session则获取session并清除其中参数

3.2.2调用securityManager中的()login()开始认证

  1. Subject subject = securityManager.login(this, token);

这里调用的是DefaultSecurityManager中的login

  1. public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {
  2. AuthenticationInfo info;
  3. try {
  4. info = authenticate(token);//调用AbstractAuthenticator.authenticate,
  5. } catch (AuthenticationException ae) {
  6. try {
  7. onFailedLogin(token, ae, subject);//认证失败,清除remenberMe的信息使之失效
  8. } catch (Exception e) {
  9. if (log.isInfoEnabled()) {
  10. log.info("onFailedLogin method threw an " +
  11. "exception. Logging and propagating original AuthenticationException.", e);
  12. }
  13. }
  14. throw ae; //propagate
  15. }
  16.  
  17. Subject loggedIn = createSubject(token, info, subject);
  18.  
  19. onSuccessfulLogin(token, info, loggedIn);
  20.  
  21. return loggedIn;
  22. }

3.2.2.1

调用AbstractAuthenticator.authenticate(),其中调用ModularRealmAuthenticator.doAuthenticate

  1. protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
  2. assertRealmsConfigured();
  3. Collection<Realm> realms = getRealms();
  4. if (realms.size() == 1) {
  5. return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
  6. } else {
  7. return doMultiRealmAuthentication(realms, authenticationToken);
  8. }
  9. }

判断realms可用由于是配置的是单realm

则调用ModularRealmAuthenticator.doSingleRealmAuthentication(),其中调用realm实现判断token类型是否支持,然后执行realm实现中的getAuthenticationInfo方法

  1. protected AuthenticationInfo doSingleRealmAuthentication(Realm realm, AuthenticationToken token) {
  2. if (!realm.supports(token)) {
  3. String msg = "Realm [" + realm + "] does not support authentication token [" +
  4. token + "]. Please ensure that the appropriate Realm implementation is " +
  5. "configured correctly or that the realm accepts AuthenticationTokens of this type.";
  6. throw new UnsupportedTokenException(msg);
  7. }
  8. AuthenticationInfo info = realm.getAuthenticationInfo(token);
  9. if (info == null) {
  10. String msg = "Realm [" + realm + "] was unable to find account data for the " +
  11. "submitted AuthenticationToken [" + token + "].";
  12. throw new UnknownAccountException(msg);
  13. }
  14. return info;
  15. }

从下图中看出这里的realm是配置后的iniRealm实例,其方法在AuthenticatingRealm中实现,由于inirealm直接在初始化onInit()时就在内存加载了所有授权数据信息,例子并没有使用cache。

  1. public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  2.  
  3. AuthenticationInfo info = getCachedAuthenticationInfo(token);
  4. if (info == null) {
  5. //otherwise not cached, perform the lookup:
  6. info = doGetAuthenticationInfo(token);
  7. log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", info);
  8. if (token != null && info != null) {
  9. cacheAuthenticationInfoIfPossible(token, info);
  10. }
  11. } else {
  12. log.debug("Using cached authentication info [{}] to perform credentials matching.", info);
  13. }
  14.  
  15. if (info != null) {
  16. assertCredentialsMatch(token, info);
  17. } else {
  18. log.debug("No AuthenticationInfo found for submitted AuthenticationToken [{}]. Returning null.", token);
  19. }
  20.  
  21. return info;
  22. }

3.2.2.1.1调用SimpleAccountRealm.doGetAuthenticationInfo通过用户名从Map users 取出对应的SimpleAccount,并判断是否被锁,是否过期

  1. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  2. UsernamePasswordToken upToken = (UsernamePasswordToken) token;
  3. SimpleAccount account = getUser(upToken.getUsername());
  4.  
  5. if (account != null) {
  6.  
  7. if (account.isLocked()) {
  8. throw new LockedAccountException("Account [" + account + "] is locked.");
  9. }
  10. if (account.isCredentialsExpired()) {
  11. String msg = "The credentials for account [" + account + "] are expired";
  12. throw new ExpiredCredentialsException(msg);
  13. }
  14.  
  15. }
  16.  
  17. return account;
  18. }

对应getUser方法使用了读锁保证了map读操作的原子性

  1. protected SimpleAccount getUser(String username) {
  2. USERS_LOCK.readLock().lock();
  3. try {
  4. return this.users.get(username);
  5. } finally {
  6. USERS_LOCK.readLock().unlock();
  7. }
  8. }

3.2.2.1.2调用AuthenticatingRealm.assertCredentialsMatch,获取密码比较器(CredentialsMatcher),其中调用doCredentialsMatch获取对比token和info(上面从realm通过用户名获取的AuthenticationInfo实例)中的密码(核心的一步),并返回结果

  1. public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
  2. Object tokenCredentials = getCredentials(token);
  3. Object accountCredentials = getCredentials(info);
  4. return equals(tokenCredentials, accountCredentials);
  5. }

3.2.2.2随后通知认证监听器AuthenticationListener(本例未设置)层层返回info至DefaultSecurityManager创建新的有登录信息的subject

  1. Subject loggedIn = createSubject(token, info, subject);

并将现有subject中的信息赋予新的subject

  1. protected Subject createSubject(AuthenticationToken token, AuthenticationInfo info, Subject existing) {
  2. SubjectContext context = createSubjectContext();
  3. context.setAuthenticated(true);
  4. context.setAuthenticationToken(token);
  5. context.setAuthenticationInfo(info);
  6. if (existing != null) {
  7. context.setSubject(existing);
  8. }
  9. return createSubject(context);
  10. }

3.2.2.3设置rememberMe(本例未实现

  1. onSuccessfulLogin(token, info, loggedIn);

3.2.3 继续获取信息principals,host(本例无),设置session到subject,

  1. if (subject instanceof DelegatingSubject) {
  2. DelegatingSubject delegating = (DelegatingSubject) subject;
  3. //we have to do this in case there are assumed identities - we don't want to lose the 'real' principals:
  4. principals = delegating.principals;
  5. host = delegating.host;
  6. } else {
  7. principals = subject.getPrincipals();
  8. }
  9.  
  10. if (principals == null || principals.isEmpty()) {
  11. String msg = "Principals returned from securityManager.login( token ) returned a null or " +
  12. "empty value. This value must be non null and populated with one or more elements.";
  13. throw new IllegalStateException(msg);
  14. }
  15. this.principals = principals;
  16. this.authenticated = true;
  17. if (token instanceof HostAuthenticationToken) {
  18. host = ((HostAuthenticationToken) token).getHost();
  19. }
  20. if (host != null) {
  21. this.host = host;
  22. }
  23. Session session = subject.getSession(false);
  24. if (session != null) {
  25. this.session = decorate(session);
  26. } else {
  27. this.session = null;
  28. }

3.3完成认证后测试一下Principal(用户名)是否正确

  1. log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

可见用户信息正确

至此完成了一个简单的Authentication过程

参考:

http://shiro.apache.org/10-minute-tutorial.html

http://shiro.apache.org/authentication.html

http://www.apache.org/dyn/closer.cgi/shiro/1.3.2/shiro-root-1.3.2-source-release.zip

转载请注明作者及来源:https://www.cnblogs.com/codflow/

Shiro官方快速入门10min例子源码解析框架3-Authentication(身份认证)的更多相关文章

  1. Shiro官方快速入门10min例子源码解析框架2-Session

    Shiro自身维护了一套session管理组件,它可以独立使用,并不单纯依赖WEB/Servlet/EJB容器等环境,使得它的session可以任何应用中使用. 2-Session)主要介绍在quic ...

  2. Shiro官方快速入门10min例子源码解析框架1-初始化

    Shiro,一个易用的Java安全框架,主要集合身份认证.授权.加密和session管理的功能. 这系文章主要简介Shiro架构,并通过官方的quickstart例程分析最简实现下Shiro的工作流程 ...

  3. Orleans例子源码

    这是Orleans系列文章中的一篇.首篇文章在此 我共享以下我现在写教程的简单的Orleans例子源码. 这个代码已经是我为了写word改动过了的.不过大体内容是通用的. 我写博客总体想法是:去除所有 ...

  4. 【Activiti工作流引擎】官方快速入门demo

    Activiti官方快速入门demo 地址: https://www.activiti.org/quick-start 0. 版本 activiti 5.22.0 JDK 1.8 1. 介绍 这个快速 ...

  5. 分布式 PostgreSQL 集群(Citus),官方快速入门教程

    多租户应用程序 在本教程中,我们将使用示例广告分析数据集来演示如何使用 Citus 来支持您的多租户应用程序. 注意 本教程假设您已经安装并运行了 Citus. 如果您没有运行 Citus,则可以使用 ...

  6. 使用C#类向数据库添加数据的例子源码

    在上一篇中,增加了sql server数据库操作类SqlOperator,用于操作sql server数据库.还有一个SqlStringHelper类,用于处理sql语句的单引号.那么这两个类怎么使用 ...

  7. jackson官方快速入门文档

    官方地址: http://jackson.codehaus.org/ http://wiki.fasterxml.com/JacksonInFiveMinutes http://wiki.faster ...

  8. TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之一

    Marionette牵线木偶,Backbone是脊骨的意思,Marionette是基于Backbone做扩展库,可以理解为把脊骨骨架绑线扯着变成牵线木偶动起来哈哈,使backbone更易使用呵呵! 构 ...

  9. Android例子源码非第三方实现根据字母排序的城市列表

    values 下dimens.xml <resources> <!-- Default screen margins, per the Android Design guidelin ...

随机推荐

  1. PHP全局变量与SESSION 漏洞(global 与 session)

    先看这一段简单的代码 <?php session_start();$_SESSION[‘isadmin’]=’yes’;$isadmin=’no’;echo $_SESSION[‘isadmin ...

  2. tf.nn.conv2d()需要搞清楚的几个变量。

    惯例先展示函数: tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 除去name参数用以指 ...

  3. MySQL数据库管理

    好记性不如烂笔头 1.MySQL启动基本原理 /etc/init.d/mysqld 是一个shell启动脚本,启动后会调用mysqld_safe脚本,最后调用的是mysqld主程序启动mysql. 单 ...

  4. 架构师养成记--17.disrunptor 多生产者多消费者

    入口: import java.nio.ByteBuffer; import java.util.UUID; import java.util.concurrent.CountDownLatch; i ...

  5. QuantLib 金融计算——随机过程之一般 Black Scholes 过程

    目录 QuantLib 金融计算--随机过程之一般 Black Scholes 过程 一般 Black Scholes 过程 如果未做特别说明,文中的程序都是 Python3 代码. QuantLib ...

  6. gradle第一篇:初入门

    有的时候,感觉技术真是一年一个样,之前呢ant式微,被maven取代,而现在呢,maven慢慢式微,又渐渐要被gradle取代,不说多了我们现在就来讲一讲gradle吧. 第一步——安装: ①安装所需 ...

  7. 物联网学习之路——物联网通信技术:NBIoT

    NBIoT是什么 NB-IoT,Narrow Band Internet of Things,窄带物联网,是一种专为万物互联打造的蜂窝网络连接技术.顾名思义,NB-IoT所占用的带宽很窄,只需约180 ...

  8. PowerDesigner16 生成的备注脚本,在sql server 2008 中报“对象名 'sysproperties' 无效”的错误的解决办法

    主要是在建模时我们对表.列增加了些说明注释,而Sql2005之后系统表sysproperties已废弃删除而改用sys.extended_properties所致. 1.修改Table TableCo ...

  9. JS框架设计之对象类型判断一种子模块

    Javascript有两套数据类型,一套是基础数据类型,一套是对象数据类型.基础数据类型包括5种基本数据类型,分别是null,bool,undefined,number,string,也叫简单数据类型 ...

  10. Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. 调用函数约定不同

    Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is ...