shiro使用md5salt哈希散列加密
我们先得到MD5加密后的字串
@Testpublic void test01(){//md5Md5Hash md5Hash = new Md5Hash("123456");System.out.println(md5Hash.toHex());//e10adc3949ba59abbe56e057f20f883e}@Testpublic void test02(){//md5+saltMd5Hash hash = new Md5Hash("123456", "ABCDE");System.out.println(hash.toHex());//8cdcdd77a21a5f80e3a88a013bc957f8}@Testpublic void test03(){//md5+salt+hash散列Md5Hash hash = new Md5Hash("123456", "ABCDE",1024);System.out.println(hash.toHex());//abadd954d2234843108de678396229e5}
定义成一个类方便调用
public class Md5Enum {/*** 对"123456"直接md5结果*/public static final String HEX1 = "e10adc3949ba59abbe56e057f20f883e";/*** 对"123456",盐为"ABCDE"的md5结果*/public static final String HEX2 = "8cdcdd77a21a5f80e3a88a013bc957f8";/*** 对"123456",盐为"ABCDE",散列1024次的md5结果*/public static final String HEX3 = "abadd954d2234843108de678396229e5";/*** 盐值*/public static final String SALT="ABCDE";
}
一、只用md5不加盐的hash
1.写一个配置类
public class UserMd5Authenticator {@Testpublic void test() {DefaultSecurityManager securityManager = new DefaultSecurityManager();UserMd5Realm realm = new UserMd5Realm();//使用hash凭证匹配器,默认是SimpleCredentialsMatcherrealm.setCredentialsMatcher(new HashedCredentialsMatcher("md5"));securityManager.setRealm(realm);SecurityUtils.setSecurityManager(securityManager);Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken("荼白","123456");try {subject.login(token);System.out.println("登录成功");}catch (UnknownAccountException e){e.printStackTrace();}catch (IncorrectCredentialsException e){e.printStackTrace();}}
}
2.自定义realm
public class UserMd5Realm extends AuthorizingRealm {@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {return null;}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {String username = (String) token.getPrincipal();if(username.equals("荼白")){return new SimpleAuthenticationInfo(username,"Md5Enum.HEX1",getName());}return null;}
}
运行后得到结果
二、使用md5加盐的hash
我们的这个类是和上面一样的
public class UserMd5Authenticator {@Testpublic void test() {//和上面是一样的//...}
}
我们只需要修改自定义的Realm即可
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;public class UserMd5Realm extends AuthorizingRealm {@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {return null;}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {String username = (String) token.getPrincipal();if(username.equals("荼白")){//参数分别为:数据库用户名,数据库md5+salt后的密码,注册时的随机盐,realm的名字return new SimpleAuthenticationInfo(username,Md5Enum.HEX2, ByteSource.Util.bytes(Md5Enum.SALT),getName());}return null;}
}
运行结果
三、使用md5+salt+哈希散列
注:默认的散列次数是1次的
首先是配置类,只需要多加一行代码即可
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;public class UserMd5Authenticator {@Testpublic void test() {DefaultSecurityManager securityManager = new DefaultSecurityManager();UserMd5Realm realm = new UserMd5Realm();//使用hash凭证匹配器,默认是SimpleCredentialsMatcherHashedCredentialsMatcher matcher = new HashedCredentialsMatcher("md5");//设置散列次数,这行是多加的matcher.setHashIterations(1024);realm.setCredentialsMatcher(matcher);securityManager.setRealm(realm);SecurityUtils.setSecurityManager(securityManager);Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken("荼白","123456");try {subject.login(token);System.out.println("登录成功");}catch (UnknownAccountException e){e.printStackTrace();}catch (IncorrectCredentialsException e){e.printStackTrace();}}
}
然后是自定义的Realm,和第二种一样,不需要改变
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;public class UserMd5Realm extends AuthorizingRealm {@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {return null;}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {String username = (String) token.getPrincipal();if(username.equals("荼白")){//参数分别为:数据库用户名,数据库md5+salt后的密码,注册时的随机盐,realm的名字return new SimpleAuthenticationInfo(username,Md5Enum.HEX3, ByteSource.Util.bytes(Md5Enum.SALT),getName());}return null;}
}
运行结果
shiro使用md5salt哈希散列加密相关推荐
- 在线字符串哈希/散列加密工具
在线字符串哈希/散列工具 在线字符串哈希/散列工 本工具可以获取多种散列方式的哈希值,如MD5,SHA1,SHA224,SHA256,SHA384,SHA512,HmacMD5,HmacSHA1等,基 ...
- java md5 密钥_java加密算法--MD5加密和哈希散列带秘钥加密算法源码
packagecom.ompa.common.utils;importjava.security.MessageDigest;importjava.security.NoSuchAlgorithmEx ...
- shiro认证+授权(使用MD5+salt+散列加密)
通过上文自定义realm分析源码可得https://blog.csdn.net/Kevinnsm/article/details/11183124 用户认证在doGetAuthenticationIn ...
- Shiro+springboot+mybatis(md5+salt+散列)认证与授权-01
这个小项目包含了注册与登录,使用了springboot+mybatis+shiro的技术栈:当用户在浏览器登录时发起请求时,首先这一系列的请求会被拦截器进行拦截(ShiroFilter),然后拦截器根 ...
- ASP.NET自带的散列加密口令【转】
使用ASP.NET自带类FormsAuthentication实现散列加密口令. private void LoginButton_Click(object sender,System.EventAr ...
- Shiro+springboot+mybatis(md5+salt+散列)认证与授权-02
代码延续地址:Shiro+springboot+mybatis(md5+salt+散列)认证与授权-01 1.创建t_role角色表(比如管理员admin,普通用户user等),创建t_pers权限表 ...
- 03,redis多键值对,哈希散列hset
// 客户端Jedis连接到服务端,并选择第2个数据库Jedis jedis = new Jedis("127.0.0.1",6379);jedis.select(1);jedis ...
- 【算法详解】数据结构:7种哈希散列算法,你知道几个?
一.前言 哈希表的历史 哈希散列的想法在不同的地方独立出现.1953 年 1 月,汉斯·彼得·卢恩 ( Hans Peter Luhn ) 编写了一份IBM内部备忘录,其中使用了散列和链接.开放寻址后 ...
- Hash(哈希/散列)和Bloom Filter(布隆过滤器)
文章目录 Hash(函数/表) Bloom Filter 布隆过滤器的误识别问题 总结 参考 Hash(函数/表) Hash (中译为哈希,或者散列)函数在计算机领域,尤其是数据快速查找领域,加密领域 ...
最新文章
- Keepalived 主备配置
- 清华计算机复试线2020,清华大学2020年研考复试线公布,复试时间待定
- laravel自动建mysql索引_让 Laravel 优雅地创建 MySQL 全文索引
- vue向ifarm传值_vue组件间传值
- Python正则表达式:最短匹配
- StanfordDB class自学笔记 (12) Constraints and Triggers
- 计算机安装的网络协议怎么看,怎么检查电脑是否安装tcp ip和netbeui协议
- 系统垃圾清理 bat文件
- STM32单片机基础知识总结(一)
- prometheus 异常退出 报错:opening storage failed
- JS模块化CommonJS、ES6模块化 、AMD、CMD知识总结
- 关于透明图像的滤色处理要注意的几个点
- natapp搭建外网服务器
- 助力绵阳市商业银行,打造高效项目生命周期管理平台
- Pytorch踩坑记之交叉熵(nn.CrossEntropy,nn.NLLLoss,nn.BCELoss的区别和使用)
- php 无限极分类(两种方式)
- goahead移植和使用
- 利用K-means进行图像压缩
- vue.js动态设置VueComponent高度遇到的问题
- 【腾讯鲜为人知的重武器3】首席体验官的刀锋
热门文章
- 使用oc的block方法回调
- multisim秒信号发生器_基于Multisim的简易信号发生器的设计
- NYOJ127 星际之门(一)【定理】
- Matlab函数学习---sum函数(计算矩阵、数组和向量元素总和)
- iOS 监听耳机插入和拔出[检索]
- pro缺点和不足 一加7t_一加7T Pro深度体验半个月以后:优点和缺点都很明显
- node.js在2018年能继续火起来吗?我们来看看node.js的待遇情况
- 3.JAVAEE-电子商城-用户管理模块
- ES 7.6.2集群迁移(从一套ES集群迁移数据到另一套集群)
- git checkout到新的分支之后原来未提交的代码找回