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)

Shiro基础的更多相关文章

  1. Shiro 基础教程

    原文地址:Shiro 基础教程 博客地址:http://www.extlight.com 一.前言 Apache Shiro 是 Java 的一个安全框架.功能强大,使用简单的Java安全框架,它为开 ...

  2. Apache Shiro:【1】Shiro基础及Web集成

    Apache Shiro:[1]Shiro基础及Web集成 Apache Shiro是什么 Apache Shiro是一个强大且易于使用的Java安全框架,提供了认证.授权.加密.会话管理,与spri ...

  3. Shiro框架 - 【shiro基础知识】

     转载:https://segmentfault.com/a/1190000013875092#articleHeader27  读完需要 63 分钟   前言 本文主要讲解的知识点有以下: 权限管理 ...

  4. Apache Shiro 快速入门教程,shiro 基础教程 (这篇文章非常好)

    第一部分 什么是Apache Shiro     1.什么是 apache shiro :   Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理 ...

  5. Apache Shiro 快速入门教程,shiro 基础教程

    第一部分 什么是Apache Shiro     1.什么是 apache shiro :   Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理 ...

  6. 【原】Spring整合Shiro基础搭建[3]

    1.前言 上个Shiro Demo基础搭建是基于官方的快速入门版本,没有集成其他框架,只是简单的通过Main方法来执行Shiro工作流程,并测试一下比较核心的函数:但在企业开发中一般都会集成Sprin ...

  7. Shiro基础知识08----拦截器介绍(转)

    1 拦截器介绍 Shiro使用了与Servlet一样的Filter接口进行扩展:所以如果对Filter不熟悉可以参考<Servlet3.1规范>http://www.iteye.com/b ...

  8. shiro基础学习(四)—shiro与项目整合

    一.认证 1.配置web.xml   2.配置applicationContext.xml      在applicationContext.xml中配置一个bean,ID和上面的过滤器的名称一致. ...

  9. shiro基础学习(三)—shiro授权

    一.入门程序 1.授权流程        2.授权的三种方式 (1)编程式: 通过写if/else 授权代码块完成. Subject subject = SecurityUtils.getSubjec ...

随机推荐

  1. windows server 2012 FTP连接报530 User 用户名 cannot log in home directory inaccessible的解决方法

    我最近在创建个人网站,经过了万网购买域名注册(www.lingcup.xyz ,www.lingcup.com),在主机屋购买免费云服务器(ip是49.4.142.41),域名别名解析(cname)到 ...

  2. angular4模块中标签添加背景图

    一.现象 一个全屏的“走马灯”每项需要添加背景图,在循环标签里需要动态添加行内样式 二.解决 1.首先有一个图片数组,如: export class AppComponent { array = [& ...

  3. Integer 比较忽略的问题

    Integer i1 = 100; Integer i2 = 100; i1==i2Integer i3 = 199; Integer i4 = 200; i3+1 == i4int i5 = 200 ...

  4. poj 3126 Prime Path bfs

    题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. https及证书

    本文试图以通俗易通的方式介绍Https的工作原理,不纠结具体的术语,不考证严格的流程.我相信弄懂了原理之后,到了具体操作和实现的时候,方向就不会错,然后条条大路通罗马.阅读文本需要提前大致了解对称加密 ...

  6. Linux学习笔记:重定向>和>>

    本文参考 https://www.cnblogs.com/piperck/p/6219330.html 重定向命令 > 和 >> 将命令的输出结果存储在指定文件中. 例如下面的例子就 ...

  7. Linq语言,由红色部分可直接代替绿色(List,dictionary)

    /// <summary> /// 获取最近5分钟缓存的车量 /// </summary> /// <param name="carNo">&l ...

  8. dubbo入门学习 五 provider端的编写

    1. 新建Maven Project, 里面只有接口(dubbo-service) 1.1 为什么这么做? RPC框架,不希望Consumer知道具体实现.如果实现类和接口在同一个项目中,Consum ...

  9. 倒谱(Cepstrum)和线性预测倒谱系数(LPCCs)

    倒谱是表示一帧语音数据特征的一个序列.从periodogram estimate of the power spectrum计算得到的倒谱系数,可以用于基音追踪(pitch tracking),然而, ...

  10. Linux编程基础——GDB(设置断点)

    启动GDB后,首先就是要设置断点,程序中断后才能调试.在gdb中,断点通常有三种形式: 断点(BreakPoint): 在代码的指定位置中断,这个是我们用得最多的一种.设置断点的命令是break,它通 ...