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密码加密的更多相关文章

  1. 第五章:shiro密码加密

    在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...

  2. JavaEE权限管理系统的搭建(四)--------使用拦截器实现登录认证和apache shiro密码加密

    RBAC 基于角色的权限访问控制(Role-Based Access Control)在RBAC中,权限与角色相关联,用户通过成为适当角色的成员而得到这些角色的权限.这就极大地简化了权限的管理.在一个 ...

  3. Shiro——MD5加密

    一.shiro默认密码的比对 通过 AuthenticatingRealm 的 credentialsMatcher 属性来进行的密码的比对 /**源码org.apache.shiro.realm.A ...

  4. Shiro自定义realm实现密码验证及登录、密码加密注册、修改密码的验证

    一:先从登录开始,直接看代码 @RequestMapping(value="dologin",method = {RequestMethod.GET, RequestMethod. ...

  5. Apach Shiro MD5密码加密过程(明文生成密码过程)详细解析

    前言: 最近再项目当中使用的ApachShiro安全框架,对于权限和服务器资源的保护都有一个很好的管理.前期主要参考的文章有 项目中设计密码的加盐处理以及二次加密问题,跟着断点 一步步揭开Apach ...

  6. shiro登录密码加密

    密码加密 String passwd = new SimpleHash("SHA-1", "username", "password").t ...

  7. 跟开涛老师学shiro -- 编码/加密

    在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...

  8. 学习Spring Boot:(十四)spring-shiro的密码加密

    前言 前面配置了怎么使用 shiro ,这次研究下怎么使用spring shiro的密码加密,并且需要在新增.更新用户的时候,实现生成盐,加密后的密码进行入库操作. 正文 配置凭证匹配器 @Bean ...

  9. Shiro简单加密服务

    编码/加密 在涉及到密码存储问题上,应该加密 / 生成密码摘要存储,而不是存储明文密码.比如之前的 600w csdn 账号泄露对用户可能造成很大损失,因此应加密 / 生成不可逆的摘要方式存储. Sh ...

随机推荐

  1. JDOJ1178:铺地板II

    JDOJ1178:铺地板II https://neooj.com/oldoj/problem.php?id=1178 题目描述 用1 x 1和2 x 2的磁砖不重叠地铺满N x 3的地板,共有多少种方 ...

  2. Centos7 yum安装MySQL5.7.25

    1 下载并安装MySQL官方的 Yum Repository[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-commun ...

  3. 树莓派包含python2.7系统路径

  4. h5移动端页面强制横屏

    说明:这个的原文章来自于https://www.jianshu.com/p/9c3264f4a405  ,我做点点补充  ,谢谢原链接的小姐姐 最近公司是要我做一个h5的小视频,因为是视频接视频,并且 ...

  5. jQuery 源码解析(八) 异步队列模块 Callbacks 回调函数详解

    异步队列用于实现异步任务和回调函数的解耦,为ajax模块.队列模块.ready事件提供基础功能,包含三个部分:Query.Callbacks(flags).jQuery.Deferred(funct) ...

  6. ElasticSearch 6.7.1操作纪录

    以下操作均在 6.7.1版本中正常 c# ES客户端 测试项目地址:https://gitee.com/dhclly/IceDog.ElasticSearchClient/tree/master/sr ...

  7. Dockerfile 中的 CMD 与 ENTRYPOINT(转)

    add by zhj:  CMD和ENTRYPOINT的差异很小,可以认为完全可以相互代替.两者都支持shell模式和exec模式, shell模式:跟你在shell下执行命令的格式一样,简单方便,但 ...

  8. 禁用software reporter tool.exe 解决CPU高占用率的问题

    或者 或者 C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\SwReporter\36.184.200 下编辑 manifes ...

  9. kali渗透综合靶机(十三)--Dina 1.0靶机

    kali渗透综合靶机(十三)--Dina 1.0靶机 一.主机发现 1.netdiscover -i eth0 -r 192.168.10.0/24 二.端口扫描 1. masscan --rate= ...

  10. VS2013(InstallShield2015LimitedEdition)打包程序详解

    VS2012没有自带打包工具,所以要先下载并安装一个打包工具.我采用微软提供的打包工具:  InstallShield2015LimitedEdition.下载地址:https://msdn.micr ...