shiro讲解之 Cryptography
本章节将详细学习一下Shiro 的Cryptography 以及其在Application中的用处。
概念
官方文档
- Cryptography is a natural addition to an enterprise security framework. Shiro’s crypto package contains easy-to-use and understand representations of crytographic Ciphers, Hashes (aka digests) and different codec implementations. All of the classes in this package are carefully designed to be very easy to use and easy to understand. Anyone who has used Java’s native cryptography support knows it can be a challenging animal to tame. Shiro’s crypto APIs simplify the complicated Java mechanisms and make cryptography easy to use for normal mortal human beings.
通常理解
- 通常而言对Shiro 的 Cryptography 的理解应在于Shiro 提供了一个用于加密解密工具包,相较于JAVA本生机制里面的加密解密而言,Shiro 的 Cryptography更为简单高效同时也很安全。Shiro提供了base64和16进制字符串编码/解码的API支持,方便一些编码解码操作
Shiro Cryptography(Shiro Cryptography pakacge提供的机密算法)
对称加密
- AES
- BlowFish
可逆加密
- Hex
- base64
不可逆加密
- SHA-1
- MD5
实例
前言
我们都知道作为一个企业级的开发,对于安全的要求可以说是极致的,因而在一些重要或者敏感的数据都会通过加密保存。下面我们将梳理一下 Shiro Cryptography 在 认证过程中的具体使用(当然它还用于其他地方,这里会是一个典型的体现)。
图形案例
该图形的实际案例为:
①
在平台内我们通过一些加密算法保存了 A 用户的Principals 信息(一般只会对密码加密),该初始化加密算逻辑里面包含了MD5加密算法(不可逆)、加密次数、盐值(一般为用户名),产生的安全数据我们表示为 Security Data A②
通过Shiro Cryptography 模拟数据即当 A 用户登录平台式我们将把 A 用户提交的用户名和密码通过特定的加密算法(此处的特定算法指算法逻辑必须要和初始化加密算法逻辑一样即加密算法、加密次数、盐值必须一会)。产生的安全数据我们表示为 Security Data B③
</
将 Security Data A 与 Security Data B 对比。对比的过程有Shiro SimpleAuthenticationInfo 封装方法完成,我们只需调用即可,下面有代码提现。