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 ...
随机推荐
- Angular 4.x NgClass ngStyle 指令用法
<some-element [ngClass]="'first second'">...</some-element> <some-element [ ...
- 2019 SDN上机第二次作业
2019 SDN上机第二次作业 1.利用mininet创建如下拓扑,要求拓扑支持OpenFlow 1.3协议,主机名.交换机名以及端口对应正确,请给出拓扑Mininet执行结果,展示端口连接情况 1. ...
- ORB-SLAM2初步(Tracking.cpp)
今天主要是分析一下Tracking.cpp这个文件,它是实现跟踪过程的主要文件,这里主要针对单目,并且只是截取了部分代码片段. 一.跟踪过程分析 首先构造函数中使用初始化列表对跟踪状态mState(N ...
- Paper | Deep Residual Learning for Image Recognition
目录 1. 故事 2. 残差学习网络 2.1 残差块 2.2 ResNet 2.3 细节 3. 实验 3.1 短连接网络与plain网络 3.2 Projection解决短连接维度不匹配问题 3.3 ...
- 无聊系列 - C#中一些常用类型与java的类型对应关系
昨天在那个.NET转java群里,看到一位朋友在问C#的int 对应java的哪个对象,就心血来潮,打算写一下C#中一些基础性的东西,在java中怎么找. 1. 基础值类型 如:int,long,do ...
- geth 基本使用
概要 geth 是以太坊的官方 golang 客户端. 通过 geth 的使用可以直观的了解以太坊, 乃至区块链的运作. 下面, 通过 geth 来构造一次搭建私链, 创建账户, 挖矿, 交易的流程. ...
- 关于使用IDEA,使用Maven打包项目
关于使用IDEA,使用Maven打包项目 在近期的一个接口项目中,使用的是SpringBoot + Maven的配置, 由于使用IDEA不久,不太熟悉使用Maven进行项目打包.记录一下. 由于使用的 ...
- JVM的监控工具之jstat
参考博客:https://www.cnblogs.com/lxcmyf/p/9878293.html jstat(JVMStatisticsMonitoringTool)是用于监视虚拟机各种运行状态信 ...
- php 5.5 编译安装
链接:https://pan.baidu.com/s/1Iy5kdugWqmvtsrYG0WYAdA 提取码:knk9 上面的链接 php5.5.8 编译安装的包 ./configure --pre ...
- 我的Xamarin开发配置
我用的的是VS2019 步骤1:打开VS→工具→Android→Android SDK 管理器 安装平台的 Android 9.0-pie下的Android SDK Platform 28 和 Goo ...