Shiro密码加密
Shiro密码加密
相关类
org.apache.shiro.authc.credential.CredentialsMatcher
org.apache.shiro.authc.credential.SimpleCredentialsMatcher
org.apache.shiro.authc.credential.HashedCredentialsMatcher
org.apache.shiro.crypto.hash.Md5Hash
org.apache.shiro.crypto.hash.SimpleHash
测试类
密码加密Md5Hash和SimpleHash
加密算法
盐
迭代次数
Hex/Base64
package com.mozq.shiro.shiro01.test;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
public class CryptoTest_01 {
public static void main(String[] args) {
String password = "changhe";
String salt = "changheluoriyuan";
int hashIterations = 5;
//哈希
Md5Hash md5Hash = new Md5Hash(password);
System.out.println(md5Hash.toString());//40da95a4e76c2678a81aa4e114349063
//哈希+加盐
Md5Hash md5SaltHash = new Md5Hash(password, salt);
System.out.println(md5SaltHash.toString());//334f0241faaab8ffe81e4a6b9c493b21
//哈希+加盐+次数
Md5Hash md5SaltIterateHash = new Md5Hash(password, salt, hashIterations);
System.out.println(md5SaltIterateHash.toString());//96ff601332575cb7c3be7304aaad57b1
SimpleHash simpleHash = new SimpleHash("MD5", password, salt, hashIterations);
System.out.println(simpleHash.toString());//96ff601332575cb7c3be7304aaad57b1
/*
Md5Hash继承SimpleHash。能使用Md5Hash的地方就能够使用SimpleHash。只用看2个类的构造器就可以明白它们的关系。
Hex/Base64
再者就是SimpleHash的 toString() toHex() toBase64() 3个方法的关系。
它们与 HashedCredentialsMatcher 的 storedCredentialsHexEncoded 字段是对应的。
*/
}
}
密码解密HashedCredentialsMatcher和SimpleAuthenticationInfo
HashedCredentialsMatcher 存储 加密算法
迭代次数
Hex/Base64
SimpleAuthenticationInfo 存储 盐
加密后字符串
UsernamePasswordToken 存储登录时用户名和密码。
hashedCredentialsMatcher.doCredentialsMatch(token, info)
方法完成认证。
加密后字符串 = 原密码 + 加密算法 + 盐 + 迭代次数 + Hex/Base64
package com.mozq.shiro.shiro01.test;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.util.ByteSource;
public class CryptoTest_02 {
public static void main(String[] args) {
String username = "liubei";
String password = "changhe";
String hashPassword = "96ff601332575cb7c3be7304aaad57b1";
String salt = "changheluoriyuan";
int hashIterations = 5;
//创建认证信息和令牌
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
// SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashPassword, "MozqRealm");
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashPassword, ByteSource.Util.bytes(salt), "MozqRealm");
//创建匹配器
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
hashedCredentialsMatcher.setHashAlgorithmName("MD5");//不设置算法名称将报错
//是Hex编码的还是Base64编码的。对应SimpleHash的toHex()和toBase64()
hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
hashedCredentialsMatcher.setHashIterations(hashIterations);//默认迭代次数为1
boolean success = hashedCredentialsMatcher.doCredentialsMatch(token, info);
System.out.println(success);
/*
Exception in thread "main" java.lang.IllegalStateException: Required 'hashAlgorithmName' property has not been set. This is required to execute the hashing algorithm.
原因:new HashedCredentialsMatcher() 没有设置匹配器的算法名。
Exception in thread "main" java.lang.IllegalArgumentException: Odd number of characters.
原因:new SimpleAuthenticationInfo(username, hashPassword, "MozqRealm") 传入的是原始密码,匹配器中匹配时出错。
false
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashPassword, "MozqRealm");
原因:密码是加盐的,但是制造的认证信息没有提供盐,则导致后面的认证失败。
true
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashPassword, ByteSource.Util.bytes(salt), "MozqRealm");
*/
}
}
Shiro密码加密的更多相关文章
- 第五章:shiro密码加密
在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...
- JavaEE权限管理系统的搭建(四)--------使用拦截器实现登录认证和apache shiro密码加密
RBAC 基于角色的权限访问控制(Role-Based Access Control)在RBAC中,权限与角色相关联,用户通过成为适当角色的成员而得到这些角色的权限.这就极大地简化了权限的管理.在一个 ...
- Shiro——MD5加密
一.shiro默认密码的比对 通过 AuthenticatingRealm 的 credentialsMatcher 属性来进行的密码的比对 /**源码org.apache.shiro.realm.A ...
- Shiro自定义realm实现密码验证及登录、密码加密注册、修改密码的验证
一:先从登录开始,直接看代码 @RequestMapping(value="dologin",method = {RequestMethod.GET, RequestMethod. ...
- Apach Shiro MD5密码加密过程(明文生成密码过程)详细解析
前言: 最近再项目当中使用的ApachShiro安全框架,对于权限和服务器资源的保护都有一个很好的管理.前期主要参考的文章有 项目中设计密码的加盐处理以及二次加密问题,跟着断点 一步步揭开Apach ...
- shiro登录密码加密
密码加密 String passwd = new SimpleHash("SHA-1", "username", "password").t ...
- 跟开涛老师学shiro -- 编码/加密
在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...
- 学习Spring Boot:(十四)spring-shiro的密码加密
前言 前面配置了怎么使用 shiro ,这次研究下怎么使用spring shiro的密码加密,并且需要在新增.更新用户的时候,实现生成盐,加密后的密码进行入库操作. 正文 配置凭证匹配器 @Bean ...
- Shiro简单加密服务
编码/加密 在涉及到密码存储问题上,应该加密 / 生成密码摘要存储,而不是存储明文密码.比如之前的 600w csdn 账号泄露对用户可能造成很大损失,因此应加密 / 生成不可逆的摘要方式存储. Sh ...
随机推荐
- JDOJ3004 超级楼梯
JDOJ3004 超级楼梯 https://neooj.com/oldoj/problem.php?id=3004 题目描述 有一个超级楼梯共N级,刚开始时你在第一级,若每次只能跨上一级或两级,要走上 ...
- 20191031 Codeforces Round #539 (Div. 1) - Virtual Participation
这场怎么全是数据结构题...
- 【ECNU619】白网吧(差分)
点此看题面 大致题意: 给你\(n\)个区间,求最多有多少个区间重叠,以及平均每个点被多少个区间覆盖. 第一个询问 这个应该可以直接离散化+差分,即我们先把每个区间右端点加\(1\),然后对于一个离散 ...
- Java实现字符串反转【Leetcode】
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- 数据仓库004 - 复习Linux shell命令 - vi 进程 端口号 系统命令 高危命令 wget yum rpm压缩
一.复习 1.vi的使用方法: 1.1 .正常编辑一个文件,保存退出 1.2. 粘贴的坑:必须在i键 编辑模式 2.找error cat xxx.log | grep -C 10 ERROR > ...
- 【前端知识体系-JS相关】ES6专题系列总结
1.如何搭建ES6的webpack开发环境? 安装Node环境 node -v // 10.14.1 安装NPM环境 npm -v // 6.4.1 安装babel npm install @babe ...
- 图解Hyperf框架:Hyperf 的初始化
- Window权限维持(九):端口监视器
后台打印程序服务负责管理Windows操作系统中的打印作业.与服务的交互通过打印后台处理程序API执行,该API包含一个函数(AddMonitor),可用于安装本地端口监视器并连接配置.数据和监视器文 ...
- c++和java的一些debug方法
就上面那个绿色的小瓢虫,点了就进了debug模式. 好尴尬啊,就说一句话. 而且,要加断点,不然就一下debug完了.
- 前端开发JS——数组
25.数组 1)声明数组: ①构造函数创建数组 var arr = new Array(); console.log(arr): //[] var arr = new Array(2 ...