Apache Shiro 学习

第一章

Apache 初次接触

Apache Shiro 是一个Java安全框架,提供了授权,认证,授权加密和会话管理等功能.

  • 认证(AuthentiCation) - 身份识别和验证
  • 授权(AuthoriZation) - 访问控制
  • 密码加密 
  • 会话管理

首先看下简单实例

依赖如下:

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.3</version>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.2.2</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

shiro.ini

[users]
chen=123
he=123

测试代码

public void testLoginLogout(){
        //1.获取Security工厂,此处处理Ini配置文件初始化SecurityManager
        Factory<SecurityManager> facoty = new IniSecurityManagerFactory("classpath:shiro-capter2.ini");
        //2.得到Security实例,并绑定到SecurityUtils
        SecurityManager securityManager = facoty.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        //3.得到Subject及创建用户名/密码身份验证Token
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("chen","123");

        try {
            //4.登录,即身份验证
            subject.login(token);
        }catch (AuthenticationException e){
            e.printStackTrace();
        }
        //断言用户已经登录
        Assert.assertEquals(true,subject.isAuthenticated());
        //6.退出
        subject.logout();

        //7.断言用户已经退出
        Assert.assertEquals(false,subject.isAuthenticated());

    }

分析:

IniSecurityManagerFactory继承关系如图,由上至下分析


1.Factory

    Factory是一个接口,提供工厂模式的支持,它仅有一个方法

 T getInstance();

2.AbstractFactory

  •    AbstractFactory 实现了 Factory接口,看下它的变量
private boolean singleton; //默认是单例 用来控制getInstance方法是创建一个新的Instance还是使用单例模式
private T singletonInstance; //getInstance的返回类型主要方法如下:
  • 主要方法如下
public T getInstance() {
        T instance;
        if (isSingleton()) {//单例模式,只创建一个对象
            if (this.singletonInstance == null) {
                this.singletonInstance = createInstance();
            }
            instance = this.singletonInstance;
        } else {
            instance = createInstance();//非单例模式,每次都新创建一个对象
        }
        if (instance == null) {
            String msg = "Factory 'createInstance' implementation returned a null object.";
            throw new IllegalStateException(msg);
        }
        return instance;
    }
protected abstract T createInstance();

3.IniFactorySupport

    IniFactorySupport继承自AbstoryFactory,并实现了createInstance()方法;

    看下createInstance()

public T createInstance() {
        Ini ini = resolveIni();//ini对象是否存在
        T instance;
        if (CollectionUtils.isEmpty(ini)) {//不存在ini对象,通过在默认classpath:shiro.ini创建ini对象
            log.debug("No populated Ini available.  Creating a default instance.");
            instance = createDefaultInstance();
            if (instance == null) {
                String msg = getClass().getName() + " implementation did not return a default instance in " +
                        "the event of a null/empty Ini configuration.  This is required to support the " +
                        "Factory interface.  Please check your implementation.";
                throw new IllegalStateException(msg);
            }
        } else { //ini存在,则创建实例
            log.debug("Creating instance from Ini [" + ini + "]");
            instance = createInstance(ini);
            if (instance == null) {
                String msg = getClass().getName() + " implementation did not return a constructed instance from " +
                        "the createInstance(Ini) method implementation.";
                throw new IllegalStateException(msg);
            }
        }
        return instance;
    }

    主要成员变量:

public static final String DEFAULT_INI_RESOURCE_PATH = "classpath:shiro.ini"; //默认ini配置文件路径
private Ini ini;//init配置文件解析

4.IniSecurityManagerFactory

    它继承自IniFactorySupport,给泛型限定了类型<SecurityManager>

public class IniSecurityManagerFactory extends IniFactorySupport<SecurityManager> {...}

    实现了createInstance(Ini ini);方法

    protected SecurityManager createInstance(Ini ini) {
        if (CollectionUtils.isEmpty(ini)) {
            throw new NullPointerException("Ini argument cannot be null or empty.");
        }
        SecurityManager securityManager = createSecurityManager(ini);
        if (securityManager == null) {
            String msg = SecurityManager.class + " instance cannot be null.";
            throw new ConfigurationException(msg);
        }
        return securityManager;
    }

5.SecurityManager

    它是Shiro框架的核心,典型的Facade模式,Shiro通过SecurityManager来管理内部组件实例,并通过它来提供安全管理的各种服务。 

继承关系如下


    三个接口分别为认证管理(Authenticator),授权管理(Authorizer),会话管理(SessionManager)

SecurityManager除了提供父接口的方法外,还提供了三个其它

Subject login(Subject subject, AuthenticationToken authenticationToken) throws AuthenticationExceptio;
void logout(Subject subject);
Subject createSubject(SubjectContext context);

6.SecurityUtils

SecurityUtils是一个抽象的工具类,提供了SecurityManager实例的保存和获取的方法,以及创建Subject.

7.Subject

即“当前操作用户”。但是,在Shiro中,Subject这一概念并不仅仅指人,也可以是第三方进程、后台帐户(Daemon Account)或其他类似事物。它仅仅意味着“当前跟软件交互的东西”。但考虑到大多数目的和用途,你可以把它认为是Shiro的“用户”概念。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值