Shiro源码学习之一

本文详细解析了Shiro框架中SecurityManager的创建过程,包括Maven依赖、源码分析,重点关注createInstance()、getIni()、createSecurityManager()等关键方法。同时,探讨了Subject的获取以及UsernamePasswordToken的使用。通过对Shiro源码的学习,有助于深入了解其内部工作机制。
摘要由CSDN通过智能技术生成

一.最基本的使用


1.Maven依赖

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.2.4</version>
        </dependency>
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-api</artifactId>  
            <version>1.7.13</version>  
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>1.7.13</version>  
        </dependency>

2.配置文件

[users]
user1 = http://blog.csdn.net/unix21
admin1 = 11111


3.调用代码
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloShiro {
    private static final Logger logger =  LoggerFactory.getLogger(HelloShiro.class);
    
    public static void main(String[] args) {
        // 初始化 SecurityManager
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
 
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
 
        // 登录
        UsernamePasswordToken token = new UsernamePasswordToken("user1", "http://blog.csdn.net/unix21");
        try {
            subject.login(token);
        } catch (AuthenticationException ae) {
            logger.info("登录失败!");
            return;
        }
        logger.info("登录成功!Hello " + subject.getPrincipal());
 
        // 注销
        subject.logout();
    }
}


参考:Shiro 那点事儿

跟我学Shiro目录贴


二.源码学习

1.SecurityManager

SecurityManager securityManager = factory.getInstance();


进入public T getInstance()


进入public T createInstance()

public T createInstance() {
        Ini ini = resolveIni();

        T instance;

        if (CollectionUtils.isEmpty(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 {
            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;
    }


进入protected Ini resolveIni()
protected Ini resolveIni() {
        Ini ini = getIni();
        if (CollectionUtils.isEmpty(ini)) {
            log.debug("Null or empty Ini instance.  Falling back to the default {} fil
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
跟我学shiro文档和示例源代码 第一章 SHIRO 简介....................................................................................................................... 5 简介 ................................................................................................................................................... 5 第二章 身份验证 .......................................................................................................................... 9 环境准备............................................................................................................................................ 9 登录/退出........................................................................................................................................ 10 身份认证流程.................................................................................................................................. 12 REALM................................................................................................................................................ 12 AUTHENTICATOR 及 AUTHENTICATIONSTRATEGY........................................................................................ 16 第三章 授权................................................................................................................................ 20 授权方式.......................................................................................................................................... 21 授权 ................................................................................................................................................. 21 PERMISSION......................................................................................................................................... 24 授权流程.......................................................................................................................................... 28 AUTHORIZER、PERMISSIONRESOLVER 及 ROLEPERMISSIONRESOLVER .......................................................... 29 第四章 INI 配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值