shiro

简介: Shiro的大致介绍

核心概念:Subject, SecurityManager, and Realms

  • Subject

    ####Subject is a security term that basically means "the currently executing user"
    #####Acquiring the Subject
    
    import org.apache.shiro.subject.Subject;
    import org.apache.shiro.SecurityUtils;
    ……
    Subject currentUser = SecurityUtils.getSubject();
#####如果拿到了subject,shiro90%的事情都可以做了例如

-  login
- logout
- access their session
- execute authorization checks
  • SecurityManager

The SecurityManager manages security operations for all users.

How do we set up a SecurityManager?
如果是web应用,我们通常会在web.xml指定一个Shiro Servlet Filter,它会创建一个SecurityManager 实例。SecurityManager 一般是一个单例,他的默认实现是POJO,配置形式有如下几种方式:

- normal Java code
- Spring XML
- YAML 
- .properties 
- .ini files
ini files是最常用的,因为 INI is easy to read, simple to use, and requires very few dependencies。for example

1.用ini配置shiro

[main]
cm = org.apache.shiro.authc.credential.HashedCredentialsMatcher
cm.hashAlgorithm = SHA-512
cm.hashIterations = 1024
# Base64 encoding (less text):
cm.storedCredentialsHexEncoded = false
iniRealm.credentialsMatcher = $cm
[users]
jdoe = TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJpcyByZWFzb2
asmith = IHNpbmd1bGFyIHBhc3Npb24gZnJvbS文件ciBhbXNoZWG5vdCB
INI File解析
1). main区域是用来配置SecurityManager 对象的,这里设置了两个对象,crm以及iniRealm。并且m对象设置了一些参数。然后将crm赋值给了iniRealm对象。
2). users区域用来指定静态的用户账号。

2.java类加载ini配置文件

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.util.Factory;

//1. Load the INI configuration
Factory<SecurityManager> factory =
new IniSecurityManagerFactory("classpath:shiro.ini");

//2. Create the SecurityManager
SecurityManager securityManager = factory.getInstance();

//3. Make it accessible
SecurityUtils.setSecurityManager(securityManager);
代码解析
1)加载ini配置文件
2)创建SecurityManager实例
3)让SecurityManager实例可以被应用访问
  • Realms

    #### A Realm acts as the ‘bridge’ or ‘connector’ between Shiro and your application’s security data
    ######做用户账号(security-related data)和登录(authentication)以及访问控制(authorization )之间的交互
    #####Shiro 提供多种开箱即用的安全数据源:     
    • LDAP
    • elational databases :JDBC
    • text configuration sources : INI , properties files,
    • and more

Authentication

 1. 获取用户的登录名 (principals),和密码(credentials).
 2. 将上一步获取的信息提交给系统
 3. 如果跟系统期待的匹配,用户是已认证的,否则是未认证的
用户登录
//1. Acquire submitted principals and credentials:
AuthenticationToken token =
new UsernamePasswordToken(username, password);
//2. Get the current Subject:
Subject currentUser = SecurityUtils.getSubject();
//3. Login:
currentUser.login(token);
处理失败的情况
//3. Login:
try {
    currentUser.login(token);
} catch (IncorrectCredentialsException ice) { …
} catch (LockedAccountException lae) { …
}
…
catch (AuthenticationException ae) {…
} 

Authorization

Authorization is essentially access control - controlling what your users can access in your application
Authorization是用来控制用户可以访问应用的哪些resource和webpage
Subject API 让我们以非常简便的方式做role和permission校验.Subject Api Permission Document例如:
if ( subject.hasRole(“administrator”) ) {
    //show the ‘Create User’ button
} else {
    //grey-out the button?
}  
……
if ( subject.isPermitted(“user:create”) ) {
    //show the ‘Create User’ button
} else {
    //grey-out the button?
}
 ……
if ( subject.isPermitted(“user:delete:jsmith”) ) {
    //delete the ‘jsmith’ user
} else {
    //don’t delete ‘jsmith’
}

Session Management

Shiro enables a Session programming paradigm for any application - from small daemon standalone applications to the largest clustered web applications.
Shiro’s architecture allows for pluggable Session data stores,And it is container independent.
实例
Session session = subject.getSession();
Session session = subject.getSession(boolean create);
session.getAttribute(“key”, someValue);
Date start = session.getStartTimestamp();
Date timestamp = session.getLastAccessTime();
session.setTimeout(millis);

Cryptography

  • Web Support

Shiro ships with a robust web support module to help secure web applications.ReferShiro Web

ShiroFilter in web.xml
![image](https://yqfile.alicdn.com/b9b637c39dbb0214007dbb5614829dc768c45ed7.png)

####URL-Specific Filter Chains
#####Shiro supports security-specific filter rules through its innovative URL filter chaining capability
######Shiro支持一种创造性的URL安全过滤器,例如
    [urls]
    /assets/** = anon
    /user/signup = anon
    /user/** = user
    /rpc/rest/** = perms[rpc:invoke], authc
    /** = authc
左边是URL是web应用的相对路径,右边是过滤器链

JSP Tag Library

image

Web Session Management

1.Default Http Sessions
Shiro defaults its session infrastructure to use the existing Servlet Container sessions that we’re all used to.
2.Shiro’s Native Sessions in the Web Tier
目录
相关文章
|
机器学习/深度学习 Linux vr&ar
ARIMA差分自回归移动平均模型
ARIMA是**差分自回归移动平均模型**的引文缩写,其中AR表示的是自回归模型,MA表示的是移动平均模型,I表示的是差分。一般写成ARIMA(p,d,q),p是自回归阶数,q是移动平均阶数,d表示差分的次数。
2132 0
ARIMA差分自回归移动平均模型
|
7月前
|
传感器 存储 安全
RFID牧场管理应用
RFID技术在牧场管理中应用广泛,通过为每头牲畜佩戴RFID标签,实现从出生到出栏的全生命周期管理。它可自动记录牲畜的健康、饮食、繁殖等信息,优化圈舍分配,精准监测行为和生理状态,及时预警疾病风险。结合传感器与管理系统,RFID助力科学配种、免疫规划及资源调配,提升养殖效率与产品质量。同时,建立全程追溯体系,满足消费者对食品安全的需求,增强市场竞争力,推动畜牧业现代化与可持续发展。
|
存储 缓存 监控
服务器的介绍
服务器的介绍
480 0
|
7月前
数据传输的基本概念
本内容介绍了带宽、数据传输速率和吞吐量的概念及三者关系。带宽是通信链路的最大传输能力,决定理论上限;数据传输速率表示实际传输速度,受多种因素影响可能低于带宽;吞吐量则是实际测量的传输速率,反映网络真实性能。用公路类比:带宽是宽度(容量)、数据传输速率是速度、吞吐量是实际通行量。
1436 7
|
小程序 搜索推荐 数据挖掘
【开题报告】基于微信小程序的电子产品商城的设计与实现
【开题报告】基于微信小程序的电子产品商城的设计与实现
1537 0
|
SQL 关系型数据库 MySQL
MySQL的自增id会用完吗?用完怎么办?
MySQL的自增id会用完吗?用完怎么办?
608 0
|
11月前
|
存储 人工智能 Cloud Native
“爆款”批量生成,如何实现一键创作 AI 有声绘本?
有声读物作为备受欢迎的内容形式之一,已在教育、影视、文化及娱乐等多个领域广泛应用。本方案通过云原生应用开发平台 CAP、函数计算 FC 和百炼模型服务,实现了有声绘本读物的自动化创作,解决了传统制作中步骤繁琐、周期长和高技术门槛的问题,显著提高了创作效率。
451 19
|
存储 缓存 数据安全/隐私保护
FGPA的简介及应用
FGPA的简介及应用
1889 2
|
NoSQL 数据库 Python
PyQt学习(二)-----图形的建立(柱状图,折线图,堆叠柱状图,饼图)
PyQt学习(二)-----图形的建立(柱状图,折线图,堆叠柱状图,饼图)
452 1
|
搜索推荐
基于若依的ruoyi-nbcio流程管理系统增加待办通知个性化设置
基于若依的ruoyi-nbcio流程管理系统增加待办通知个性化设置
440 2