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 ...
随机推荐
- (三十四)golang--接口
golang的多态特性主要体现在接口上: 主要优势:高内聚低耦合: package main import ( "fmt" ) type usb interface { start ...
- MYSQL5.6免安装版在windows下的使用
一.去MYSQL官网下载MYSQL免安装版,由于我的系统是64位的,所以就下载了64位的Mysql版本 http://cdn.mysql.com//Downloads/MySQL-5.6/mysql- ...
- Gin框架 - 数据绑定和验证
概述 上篇文章分享了 Gin 框架使用 Logrus 进行日志记录,这篇文章分享 Gin 框架的数据绑定与验证. 有读者咨询我一个问题,如何让框架的运行日志不输出控制台? 解决方案: engine : ...
- 关于使用IDEA,使用Maven打包项目
关于使用IDEA,使用Maven打包项目 在近期的一个接口项目中,使用的是SpringBoot + Maven的配置, 由于使用IDEA不久,不太熟悉使用Maven进行项目打包.记录一下. 由于使用的 ...
- 你真的了解nginx重定向URI?-rewrite和alias指令
未经允许不得转载!最近发现有博主转载我的文章,并没有跟我打招呼,也没有注明出处!!!! 熟悉Nginx的同学都知道Nginx可以用来做负载均衡和反向代理,非常好用.做前后端分离也是非常的方便. 今天我 ...
- Spring源码系列 — 注解原理
前言 前文中主要介绍了Spring中处理BeanDefinition的扩展点,其中着重介绍BeanDefinitionParser方式的扩展.本篇文章承接该内容,详解Spring中如何利用BeanDe ...
- A Pattern Language for Parallel Programming
The pattern language is organized into four design spaces. Generally one starts at the top in the F ...
- Elasticsearch 中映射参数doc_values 和 fielddata分析比较
doc_values 默认情况下,大部分字段是索引的,这样让这些字段可被搜索.倒排索引(inverted index)允许查询请求在词项列表中查找搜索项(search term),并立即获得包含该词项 ...
- 【maven】【IDEA】idea中使用maven编译项目,报错java: 错误: 找不到符号 【2】
=================================================================================== idea中使用maven编译项目 ...
- express捕获全局异常的三种方法
场景 express的路由里抛出异常后,全局中间件没办法捕获,需要在所有的路由函数里写try catch,这坑爹的逻辑让人每次都要多写n行代码 官方错误捕获中件间代码如下 app.use(functi ...