百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Shiro框架:认证和授权原理(shiro框架认证流程)

ccwgpt 2025-05-08 17:09 19 浏览 0 评论

优质文章,及时送达

前言

Shiro作为解决权限问题的常用框架,常用于解决认证、授权、加密、会话管理等场景。本文将对Shiro的认证和授权原理进行介绍:

  • Shiro可以做什么?、

  • Shiro是由什么组成的?

  • 举个Shiro的例子呗?

  • Shiro认证的原理是咋样的?

  • Shiro授权的原理是咋样的?

1. Shiro可以做什么?

在构建一个网络应用的时候,权限检验管理作为非常重要的安全措施,需要包含以下几点:

  • 用户认证— 用户身份识别。得知道来的人是谁;
  • 用户授权— 用户权限访问控制。得知道来的人有没有资格进来,又不是“我家大门常打开”;
  • 密码加密— 加密敏感数据,防止被偷窥。就像是家里上锁,上几把锁,原子锁还是电子锁?
  • 会话管理— 与用户相关的时间敏感的状态信息。类似于阅后即焚的信件。

Shiro支持以上的功能,而且它提供的API可以帮助我们很容易就开发出足够好的应用。Shiro具备认证、授权、加密、会话管理、集成Web、缓存等功能,相当好用。

2. Shiro是由什么组成的?

下图是Shiro的架构图:

可以看出来,Security Manager是Shiro的核心,连认证、授权、会话管理等都是在这里面执行的。接下来我们来看看这些组件分别是做什么用的:

  • Subject:主体,可以是用户或程序,主体可以访问Security Manager以获得认证、授权、会话等服务;
  • Security Manager:安全管理器,主体所需的认证、授权功能都是在这里进行的,是Shiro的核心;
    • Authenticator:认证器,主体的认证过程通过Authenticator进行;

    • Authorizer:授权器,主体的授权过程通过Authorizer进行;

    • Session Manager:shiro的会话管理器,与web应用提供的Session管理分隔开;

    • Session DAO:通过Session DAO管理Session数据,针对个性化的Session数据存储,需要使用到Session DAO;

    • Cache Manager:缓存管理器,主要对Session和授权数据进行缓存。因为这些数据不怎么改变,为了提高访问速度选择将其缓存起来;

    • Realm:域,可以有一个或多个域,可通过Realm存储授权和认证的逻辑;

  • Cryptography:密码管理,Shiro提供了一套加密/解密的组件,如MD5散列算法等。

3. 举个Shiro的例子呗?

笔者采用Shiro官网给的例子,来梳理Shiro认证、授权的过程:

shiro.ini

创建一个文件shiro.ini,其功用相当于Realm。shiro默认使用的也是IniRealm:

# =============================================================================
# Tutorial INI configuration
#
# Usernames/passwords are based on the classic Mel Brooks' film "Spaceballs" :)
# =============================================================================

# -----------------------------------------------------------------------------
# Users and their (optional) assigned roles
# username = password, role1, role2, ..., roleN
# -----------------------------------------------------------------------------
[users]
root = secret, admin
guest = guest, guest
presidentskroob = 12345, president
darkhelmet = ludicrousspeed, darklord, schwartz
lonestarr = vespa, goodguy, schwartz

# -----------------------------------------------------------------------------
# Roles with assigned permissions
# roleName = perm1, perm2, ..., permN
# -----------------------------------------------------------------------------
[roles]
admin = *
schwartz = lightsaber:*
goodguy = winnebago:drive:eagle5

在上面这串代码中,已经给出了用户和角色信息。root = secret, admin表示,用户名为root,密码是secret,角色是admin;**schwartz = lightsaber:**表示角色schwartz拥有权限lightsaber:

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

public class Tutorial {

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

public static void main(String[] args) {
log.info("My First Apache Shiro Application");

//引用shiro.ini的配置信息
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
//获取SecurityManager安全管理器
SecurityManager securityManager = factory.getInstance;
SecurityUtils.setSecurityManager(securityManager);

// get the currently executing user:
//获取单签主体(用户)
Subject currentUser = SecurityUtils.getSubject;

// Do some stuff with a Session (no need for a web or EJB container!!!)
//获取当前主体的会话
Session session = currentUser.getSession;
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
log.info("Retrieved the correct value! [" + value + "]");
}

// let's login the current user so we can check against roles and permissions:
//判断当前用户是否认证
if (!currentUser.isAuthenticated) {
//将账号和密码封装为UsernamePasswordToken中
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 more exceptions here (maybe custom ones specific to your application?
catch (AuthenticationException ae) {
//unexpected condition? error?
}
}

//say who they are:
//print their identifying principal (in this case, a username):
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

//test a role:
//检查是否有相应角色权限
if (currentUser.hasRole("schwartz")) {
log.info("May the Schwartz be with you!");
} else {
log.info("Hello, mere mortal.");
}

//test a typed permission (not instance-level)
//判断是否有资源操作权限
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.");
}

//a (very powerful) Instance Level permission:
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!");
}

//all done - log out!
//退出
currentUser.logout;

System.exit(0);
}
}

我们可以从示例代码中提取出认证、授权过程中比较常用的API:

认证

//获取当前主体(用户)
Subject currentUser = SecurityUtils.getSubject;
//判断当前用户是否认证
currentUser.isAuthenticated
//将账号和密码封装为UsernamePasswordToken中
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
//记住我
token.setRememberMe(true);
//进行登录操作
currentUser.login(token);

判断权限

//检查是否有相应角色权限
currentUser.hasRole("schwartz")
//判断是否有资源操作权限
currentUser.isPermitted("lightsaber:wield")

从上面代码我们可以看出,Shiro中认证过程大概是:

  • 先获取当前用户信息,判断当前用户是否认证

  • 如果没认证,就将账号和密码封装成UserPasswordToken,进行登录操作

接下来,我们通过读源码,来看看Shiro的认证和授权原理是怎么样的。

4. Shiro认证的原理是咋样的?

通过从login方法使用Debug追踪Shiro的认证逻辑,可以得到以下代码执行顺序:

//在构造了Security Manager之后,提交认证,参数是封装了用户信息的token
currentUser.login(token);

//Security Manager执行认证
Subject subject = this.securityManager.login(this, token);

AuthenticationInfo info = this.authenticate(token);

//Security Manager委托给Authenticator执行认证逻辑
this.authenticator.authenticate(token);

AuthenticationInfo info = this.doAuthenticate(token);

//获取多Realm进行身份认证
Collection<Realm> realms = this.getRealms;
doSingleRealmAuthentication(realm, token);

AuthenticationInfo info = realm.getAuthenticationInfo(token);

//调用对应的Realm进行校验,认证成功则返回用户属性
AuthenticationInfo info = realm.doGetAuthenticationInfo(token);

简单说,认证过程的流程是这样的:

5. Shiro授权的原理是咋样的?

我们还是追踪一下源码,可以知道以下执行顺序:

//判断当前主体是否有角色限制
currentUser.hasRole("schwartz")
//判断当前主体是否具备资源操作权限
currentUser.isPermitted("lightsaber:wield")

//委托给securityManager判断角色是否与既定的角色匹配
this.securityManager.hasRole(this.getPrincipals, roleIdentifier)

//securityManager委托Authorizer进行角色检验
this.authorizer.hasRole(principals, roleIdentifier)

//Authorizer判断Realm的角色/权限是否和传入的匹配
AuthorizationInfo info = this.getAuthorizationInfo(principal);
return info.getRoles.contains(roleIdentifier)

//执行Realm进行授权操作
info = this.doGetAuthorizationInfo(principals);

简单点看,Shiro的授权过程是这样的:

综上,我们可以知道,无论是认证还是授权操作,都需要SecurityManager中的Realm来定义用户认证时需要的账户信息,以及授权时所需的权限信息。而Realm一般不会采用Shiro官网介绍的“ini配置文件”方式,而是通过自定义Realm组件来实现:

public class UserRealm extends AuthorizingRealm {
/**
* 执行授权逻辑
*
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
User user = (User) principalCollection.getPrimaryPrincipal;
SecurityUtils.getSubject.getSession.setAttribute(String.valueOf(user), SecurityUtils.getSubject.getPrincipals);

SimpleAuthorizationInfo info = new SimpleAuthorizationInfo;

info.addRole(...);
return info;
}

/**
* 执行认证逻辑
*
* @param authenticationToken
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
User user = userService.findByName(token.getUsername);
if (user == || !token.getUsername.equals(user.getUsername)) {
throw new UnknownAccountException("此账号不存在");
}
...
return new SimpleAuthenticationInfo(user);
}
}

如果采用上述方式来定义Realm,那么还需要自定义ShiroConfig作为组件。实现Shiro实现认证和授权的资料很多,大家也可以参考一下官网的资料:
http://shiro.apache.org/tutorial.html

参考资料:
shiro原理及其运行流程介绍
极简入门,Shiro的认证与授权流程解析

-END-

如果看到这里,说明你喜欢这篇文章,请 转发、点赞。同时 标星(置顶)本公众号可以第一时间接受到博文推送。

1. 今天我们来聊一聊 Spring 中的线程安全性

强烈推荐 16 款 IDEA 插件

3.30 分钟学会如何使用 Shiro

Spring Boot+Redis实现接口自动幂等

最近整理一份面试资料《Java技术栈学习手册》,覆盖了Java技术、面试题精选、Spring全家桶、Nginx、SSM、微服务、数据库、数据结构、架构等等。

相关推荐

2025南通中考作文解读之四:结构框架

文题《继续走,迈向远方》结构框架:清晰叙事,层层递进示例结构:1.开头(点题):用环境描写或比喻引出“走”与“远方”,如“人生如一条长路,每一次驻足后,都需要继续走,才能看见更美的风景”。2.中间...

高中数学的知识框架(高中数学知识框架图第三章)

高中数学的知识框架可以划分为多个核心板块,每个板块包含具体的知识点与内容,以下为详细的知识框架结构:基础知识1.集合与逻辑用语:涵盖集合的概念、表示方式、性质、运算,以及命题、四种命题关系、充分条件...

决定人生的六大框架(决定人生的要素)

45岁的自己混到今天,其实是失败的,要是早点意识到影响人生的六大框架,也不至于今天的模样啊!排第一的是环境,不是有句话叫人是环境的产物,身边的环境包括身边的人和事,这些都会对一个人产生深远的影响。其次...

2023年想考过一级造价师土建计量,看这30个知识点(三)

第二章工程构造考点一:工业建筑分类[考频分析]★★★1.按厂房层数分:(1)单层厂房;(2)多层厂房;(3)混合层数厂房。2.按工业建筑用途分:(1)生产厂房;(2)生产辅助厂房;(3)动力用厂房;(...

一级建造师习题集-建筑工程实务(第一章-第二节-2)

建筑工程管理与实务题库(章节练习)第一章建筑工程技术第二节结构设计与构造二、结构设计1.常见建筑结构体系中,适用建筑高度最小的是()。A.框架结构体系B.剪力墙结构体系C.框架-剪力墙结构体系D...

冷眼读书丨多塔斜拉桥,这么美又这么牛

”重大交通基础设施的建设是国民经济和社会发展的先导,是交通运输行业新技术集中应用与创新的综合体现。多塔斜拉桥因跨越能力强、地形适应性强、造型优美等特点,备受桥梁设计者的青睐,在未来跨越海峡工程中将得...

2021一级造价师土建计量知识点:民用建筑分类

2021造价考试备考开始了,学霸君为大家整理了一级造价师备考所用的知识点,希望对大家的备考道路上有所帮助。  民用建筑分类  一、按层数和高度分  1.住宅建筑按层数分类:1~3层为低层住宅,4~6层...

6个建筑结构常见类型,你都知道吗?

建筑结构是建筑物中支承荷载(作用)起骨架作用的体系。结构是由构件组成的。构件有拉(压)杆、梁、板、柱、拱、壳、薄膜、索、基础等。常见的建筑结构类型有6种:砖混结构、砖木结构、框架结构、钢筋混凝土结构、...

框架结构设计经验总结(框架结构设计应注意哪些问题)

1.结构设计说明主要是设计依据,抗震等级,人防等级,地基情况及承载力,防潮抗渗做法,活荷载值,材料等级,施工中的注意事项,选用详图,通用详图或节点,以及在施工图中未画出而通过说明来表达的信息。2.各...

浅谈混凝土框架结构设计(混凝土框架结构设计主要内容)

浅谈混凝土框架结构设计 摘要:结构设计是个系统的全面的工作,需要扎实的理论知识功底,灵活创新的思维和严肃认真负责的工作态度。钢筋混凝土框架结构虽然相对简单,但设计中仍有很多需要注意的问题。本文针...

2022一级建造师《建筑实务》1A412020 结构设计 精细考点整理

历年真题分布统计1A412021常用建筑结构体系和应用一、混合结构体系【2012-3】指楼盖和屋盖采用钢筋混凝土或钢木结构,而墙和柱采用砌体结构建造的房屋,大多用在住宅、办公楼、教学楼建筑中。优点:...

破土动工!这个故宫“分院”科技含量有点儿高

故宫“分院”设计图。受访者供图近日,位于北京海淀区西北旺镇的故宫北院区项目已开始破土动工,该项目也被称作故宫“分院”,筹备近十年之久。据悉,故宫本院每年展览文物的数量不到1万件,但是“分院”建成后,预...

装配式结构体系介绍(上)(装配式结构如何设计)

PC构件深化、构件之间连接节点做法等与相应装配式结构体系密切相关。本节列举目前常见的几种装配式结构体系:装配整体式混凝土剪力墙结构体系、装配整体式混凝土框架结构体系、装配整体式混凝土空腔结构体系(S...

这些不是双向抗侧结构体系(这些不是双向抗侧结构体系的特点)

双向抗侧土木吧规范对双向抗恻力结构有何规定?为何不应采用单向有墙的结构?双向抗侧土木吧1.规范对双向抗侧力结构体系的要求抗侧力体系是指抵抗水平地震作用及风荷载的结构体系。对于结构体系的布置,规范针对...

2022一级建造师《建筑实务》1A412020 结构设计 精细化考点整理

1A412021常用建筑结构体系和应用一、混合结构体系【2012-3】指楼盖和屋盖采用钢筋混凝土或钢木结构,而墙和柱采用砌体结构建造的房屋,大多用在住宅、办公楼、教学楼建筑中。优点:抗压强度高,造价...

取消回复欢迎 发表评论: