Factory<T>接口(org.apache.shiro.util.Factory)

  接口的getInstance()方法是泛型方法,可以用来创建SecurityManager接口对象

 T getInstance() 
          Returns an instance of the required type.

SecurityManager接口(org.apache.shiro.mgt )

  可以保存的所有的认证数据信息

  实际上,大多数应用程序程序员不会经常与SecurityManager交互(如果有的话)。大多数应用程序程序员只关心当前执行的用户的安全操作,通常通过调用SecurityUtils.getSubject()来实现。

SecurityUtils类(org.apache.shiro.SecurityUtils)

    setSecurityManager(SecurityManager securityManager) 
          Sets a VM (static) singleton SecurityManager, specifically for transparent use in the getSubject() implementation.

应用程序中,通过setSecurityManager把securityManager  set进SecurityUtils 然后调用SecurityUtils.getSubject()方法拿到Subject

getSubject() 
          Returns the currently accessible Subject available to the calling code depending on runtime environment.

Subject

  A Subject represents state and security operations for a single application user. These operations include authentication (login/logout), authorization (access control), and session access. It is Shiro's   primary mechanism for single-user security functionality.

  通过SecurityUtils.getSubject()拿到Subject之后  可以通过Subject接口进行登录,授权,认证,等操作。(下面只摘录了一个登录的API接口)

  login(AuthenticationToken token) 
          Performs a login attempt for this Subject/user.

AuthenticationToken

  参数token: 身份验证令牌是用户在身份验证尝试期间提交的帐户主体和支持凭证的整合。该令牌通过authenticate(token)方法提交给身份验证者。然后身份验证器执行身份验证/登录过程。
  验证令牌的常见实现包括用户名/密码对、X.509证书、PGP密钥或您能想到的任何东西。令牌可以是身份验证器需要的任何东西,以便正确地进行身份验证

下面提供一个简单的DEMO

package com.sun.test;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory; public class TestLoginDemo {
public static void main(String[] args) {
// 取得Factory接口对象,主要的目的是通过配置文件加载文件之中的信息,这些信息暂时不能够成为认证信息
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
// 取得里面所保存的所有的认证数据信息
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
// 利用一个专门的认证操作的处理类,实现认证处理的具体的实现
SecurityUtils.setSecurityManager(securityManager);
// 获取进行用户名和密码认证的接口对象
Subject subject = SecurityUtils.getSubject() ;
// 定义了一个Token,里面保存要登录的用户名和密码信息
UsernamePasswordToken token = new UsernamePasswordToken("admin","hello") ;
// 实现用户登录处理
subject.login(token);
}
}

shiro.ini配置

[users]
admin=hello

Relam (org.apache.shiro.realm.Realm)

上面的权限认证都是在一个配置文件完成的(shiro.ini),如果想实现不同用户数据来源,并且统一这些来源的处理。准备了一个叫Realm的接口

Reaml实现类的授权接口

  doGetAuthorizationInfo(PrincipalCollection principals) 
          Retrieves the AuthorizationInfo for the given principals from the underlying data store. 

Reaml实现类的认证接口

  doGetAuthenticationInfo(AuthenticationToken token) 
          Retrieves authentication data from an implementation-specific datasource (RDBMS, LDAP, etc) for the given authentication token.

介绍完Shiro基础,下面我们讲一讲Shiro在SSM框架的应用(使用Relam)

最新文章

  1. git 忽略文件权限
  2. Convert string to binary and binary to string in C#
  3. shell各种执行方式区别
  4. (转)如何在高并发分布式系统中生成全局唯一Id
  5. react-native-router-flux 下部导航
  6. HTML5_1
  7. 解决 MySQL manager or server PID file could not be found! 的方法
  8. JSP 和 Servlet 有哪些相同点和不同点, 他们之间的联系是什么?
  9. TCP四次挥手
  10. R语言相关工具
  11. Tencent社会招聘scrapy爬虫 --- 已经解决
  12. 关于Hbase开启snappy压缩
  13. dede织梦系统接入熊掌号推送api,完整详细教程
  14. 小程序学习笔记三:页面文件详解之视图层WXML、WXS、WXSS文件
  15. Confluence 6 示例 - https://confluence.atlassian.com/
  16. POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)
  17. Liunx Pwd
  18. django项目一 分页器(前端分页和后端分页区别)
  19. Windows折腾之路 兼谈纯净强迫情节
  20. 【Nodejs】使用nimble串行化回调任务

热门文章

  1. 2018年 js 简易控制滚动条滚动的简单方法
  2. php的运行原理、cgi对比fastcgi以及php-cgi和php-fpm之间的联系区别
  3. Python调用Linux bash命令
  4. EasyPR源码剖析(8):字符分割
  5. Python学习:经典编程例题
  6. jquery datatables+MVC+WCF
  7. BaseDao.util(虎大将军)
  8. openwrt添加自动启动项
  9. Sqlite3数据库查看工具
  10. (摘录)String是值传递还是引用传递