Shiro源码分析(十)——鉴权流程

2021SC@SDUSC

一.Demo代码

在shiro源码的quickstart目录下有QuickStart.java文件。其中有对Shiro最简单使用的Demo。

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.ini.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.lang.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Quickstart {

    private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);


    public static void main(String[] args) {
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();

        SecurityUtils.setSecurityManager(securityManager);

        Subject currentUser = SecurityUtils.getSubject();

        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
            token.setRememberMe(true);
            try {
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                log.info("There is no user with username of " + token.getPrincipal());
            } catch (IncorrectCredentialsException ice) {
                log.info("Password for account " + token.getPrincipal() + " was incorrect!");
            } catch (LockedAccountException lae) {
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
            }
            catch (AuthenticationException ae) {
            }
        }

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

		// 测试一个角色:
        if (currentUser.hasRole("schwartz")) {
            log.info("May the Schwartz be with you!");
        } else {
            log.info("Hello, mere mortal.");
        }

        // 测试类型化权限(而不是实例级)
        if (currentUser.isPermitted("lightsaber:wield")) {
            log.info("You may use a lightsaber ring.  Use it wisely.");
        } else {
            log.info("Sorry, lightsaber rings are for schwartz masters only.");
        }

        // 实例级权限:
        if (currentUser.isPermitted("winnebago:drive:eagle5")) {
            log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                    "Here are the keys - have fun!");
        } else {
            log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
        }

        currentUser.logout();

        System.exit(0);
    }
}

二.解读ini文件

认证和鉴权是shiro的两大主要功能,在demo中,各个角色的权限在一份ini文件中进行定义,其中包括了每个用户的用户名、密码,该用户的角色以及该角色的权限。

# =============================================================================
# Quickstart 的 INI Realm 配置
#
# 对于那些可能不理解本文件中的引用的人来说,这些定义都是基于Mel Brooks的经典电影《Spaceballs》。
# =============================================================================

# -----------------------------------------------------------------------------
# 用户及其分配的角色
#
# 每一行都符合org.apache.shiro.realm.text中定义的格式。
# -----------------------------------------------------------------------------
[users]
# 用户root的密码是secret,角色是admin
root = secret, admin

# 用户guest的密码是guest,角色是guest
guest = guest, guest

# 用户presidentskroob的密码是12345,角色是president
presidentskroob = 12345, president

# 用户darkhelmet的密码是ludicrousspeed,角色为darklord和schwartz
darkhelmet = ludicrousspeed, darklord, schwartz

# 用户lonestarr的密码是vespa,角色为goodguy和schwartz
lonestarr = vespa, goodguy, schwartz

# -----------------------------------------------------------------------------
# 各个角色的权限
# 
# 每一行都符合org.apache.shiro.realm.text中定义的格式。
# -----------------------------------------------------------------------------
[roles]
# admin角色拥有所有权限,用通配符*表示
admin = *

# schwartz角色可以用任何lightsaber做任何事情
schwartz = lightsaber:*

# goodguy角色被允许驾驶(动作)牌照为eagle5(实例的特定id)的winnebago(类型)
goodguy = winnebago:drive:eagle5

三.源码分析

我们针对鉴权部分的源码进行分析

		// 测试一个角色:
		// 判断当前用户的角色是否为schwartz
        if (currentUser.hasRole("schwartz")) { // 1
            log.info("May the Schwartz be with you!");
        } else {
            log.info("Hello, mere mortal.");
        }

        // 测试类型化权限(而不是实例级)
        // 判断当前用户是否被允许使用lightsaber执行wield动作
        if (currentUser.isPermitted("lightsaber:wield")) { // 2
            log.info("You may use a lightsaber ring.  Use it wisely.");
        } else {
            log.info("Sorry, lightsaber rings are for schwartz masters only.");
        }

        // 实例级权限:
        // 判断当前用户是否被允许驾驶牌照为eagle5的winnebago
        if (currentUser.isPermitted("winnebago:drive:eagle5")) { // 3
            log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                    "Here are the keys - have fun!");
        } else {
            log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
        }

本文先就用户和角色的各项权限的定义做简要的解释,以及简要解释demo源码执行了什么,1、2、3部分的源代码由接下来两篇文章进行分析。
(完)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值