shiro使用Md5加密
shiro使用Md5加密
视频参考:https://www.bilibili.com/video/BV1uz4y197Zm?p=8
shiro实现md5加密
package com.jing.md5;import org.apache.shiro.crypto.hash.Md5Hash;public class TestMd5 {public static void main(String[] args) {// 创建md5,使用Md5Hash构造方法进行加密Md5Hash md5Hash = new Md5Hash("123");System.out.println("md5 = " + md5Hash);// md5 + saltMd5Hash md5Hash1 = new Md5Hash("123", "zard");System.out.println("md5 + salt = " + md5Hash1);// md5 + salt + hash散列次数Md5Hash md5Hash2 = new Md5Hash("123", "zard", 1024);System.out.println("md5 + salt + hash散列次数 = " + md5Hash2);}
}
result:
md5 = 202cb962ac59075b964b07152d234b70
md5 + salt = 204f9a8aa53d4a5f3de28e969d2ea713
md5 + salt + hash散列次数 = 7a3865843b5ac72bb4d9e28cd8877681
1.Md5加密
自定义认证授权Realm
package com.jing.md5;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 CustomMd5Realm extends AuthorizingRealm {// 授权@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {return null;}// 认证@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {// 获取用户名String principal = (String) token.getPrincipal();// 直接判断if ("xiaojing".equals(principal)) {// 参数1:用户名 参数2:密码 参数3:realmNamereturn new SimpleAuthenticationInfo(principal, "202cb962ac59075b964b07152d234b70", this.getName());}return null;}
}
package com.jing.md5;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
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;public class TestCustomMd5Realm {public static void main(String[] args) {// 获取默认的安全管理器DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();CustomMd5Realm realm = new CustomMd5Realm();HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();hashedCredentialsMatcher.setHashAlgorithmName("md5");realm.setCredentialsMatcher(hashedCredentialsMatcher);// 将自定义的Realm注入到安全管理器defaultSecurityManager.setRealm(realm);// 使用安全工具类 将安全管理器注入SecurityUtils.setSecurityManager(defaultSecurityManager);// 获取主体Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken("xiaojing", "123");try {subject.login(token);System.out.println("登录成功");} catch (UnknownAccountException e) {e.printStackTrace();System.out.println("账号错误");} catch (IncorrectCredentialsException e) {e.printStackTrace();System.out.println("密码错误");} catch (Exception e) {e.printStackTrace();}}
}
2.Md5加密 + salt
- 只用添加ByteSource.Util.bytes([随机盐])即可
package com.jing.md5;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 CustomMd5Realm extends AuthorizingRealm {@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {return null;}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {String principal = (String) token.getPrincipal();if ("xiaojing".equals(principal)) // 加salt的话,只用添加参数 ByteSource.Util.bytes([随机盐])return new SimpleAuthenticationInfo(principal, "204f9a8aa53d4a5f3de28e969d2ea713", ByteSource.Util.bytes("zard"), this.getName());}return null;}
}
3.Md5加密 + salt + hash散列
package com.jing.md5;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 CustomMd5Realm extends AuthorizingRealm {@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {return null;}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {String principal = (String) token.getPrincipal();if ("xiaojing".equals(principal)) {return new SimpleAuthenticationInfo(principal, "7a3865843b5ac72bb4d9e28cd8877681", ByteSource.Util.bytes("zard"), this.getName());}return null;}
}
hashedCredentialsMatcher.setHashIterations(1024); # 设置散列次数
package com.jing.md5;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
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;public class TestCustomMd5Realm {public static void main(String[] args) {// 获取默认的安全管理器DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();CustomMd5Realm realm = new CustomMd5Realm();HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();// 设置使用md5加密(默认不加密equals比较密码)hashedCredentialsMatcher.setHashAlgorithmName("md5");// hash散列次数hashedCredentialsMatcher.setHashIterations(1024);realm.setCredentialsMatcher(hashedCredentialsMatcher);// 将自定义的Realm注入到安全管理器defaultSecurityManager.setRealm(realm);// 使用安全工具类 将安全管理器注入SecurityUtils.setSecurityManager(defaultSecurityManager);// 获取主体Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken("xiaojing", "123");try {subject.login(token);System.out.println("登录成功");} catch (UnknownAccountException e) {e.printStackTrace();System.out.println("账号错误");} catch (IncorrectCredentialsException e) {e.printStackTrace();System.out.println("密码错误");} catch (Exception e) {e.printStackTrace();}}
}
shiro使用Md5加密相关推荐
- SpringBoot工程使用shiro 进行MD5加密
Spring Boot工程利用shiro 进行MD5加密 1.加入相关依赖 <dependency><groupId>org.apache.shiro</groupId& ...
- 降龙十八掌之 springboot整合shiro(含MD5加密)
java学习爱好者 2019-05-27 16:21:00 开发环境: 1.mysql - 5.7.21 2.navicat(mysql客户端管理工具) 3.idea 2017 4.jdk9 5.to ...
- java shiro盐值加密_shiro盐值加密并验证
在数据表中存的密码不应该是123456,而应该是123456加密之后的字符串,而且还要求这个加密算法是不可逆的,即由加密后的字符串不能反推回来原来的密码,如果能反推回来那这个加密是没有意义的.著名的加 ...
- Shiro的Base64和MD5加密的使用
场景 Shiro自带Base64和MD5加密. Base64位置: MD5位置: 实现 新建测试类 package com.badao.util;import org.apache.shiro.cod ...
- shiro 使用md5密码加密 锁定账户
此篇博客根据之前写的shiro快速配置延续的,建议不了解的可以先看看之前的博客. springMVC中快速配置shiro 1.为了使用密码加密,我们新建一个对用户信息操作的工具类 package co ...
- Shiro权限框架(4):MD5加密
MD5加密 shiro有很多加密算法,如md5和sha,而且还支持加盐,使得密码的解析变得更有难度,更好的保障了数据的安全性.首先我们来看看md5算法的各种实现方式: package com.buba ...
- java shiro盐值加密_java中spring-shiro实现密码的MD5盐值加密
看了网上很多教程,都提到有配置spring shiro的密码加密方式,甚至给出了自定义的Class来实现.却很少有通过配置来解决的. 密码的盐值加密方式应该是非常通用的,也可以算是基础吧.按理说spr ...
- Shiro框架:Shiro简介、登陆认证入门程序、认证执行流程、使用自定义Realm进行登陆认证、Shiro的MD5散列算法
一.Shiro介绍: 1.什么是shiro: (1)shiro是apache的一个开源框架,是一个权限管理的框架,实现用户认证.用户授权. (2)spring中有spring security,是一个 ...
- Shiro认证--盐加密(SSM)
目录 一.认证 导入pom依赖 配置web.xml 数据表编写好后开始逆向生成 查数据库的地方就是realm 实现自定义realm接口 创建自定义域 编写认证方法 二.盐加密 盐加密,数据库密码发展史 ...
最新文章
- java画满天星_java_java实现的满天星效果实例,本文实例讲述了java实现满天星 - phpStudy...
- 从0搭建一个Springboot+vue前后端分离项目(三)使用idea进行页面搭建+Element框架
- 【ArcGIS微课1000例】0008:ArcGIS中如何设置相对路径?(解决图层前红色的感叹号)
- “睡服”面试官系列第十八篇之generator函数的语法(建议收藏学习)
- 前端学习(1402):多人管理22验证joi
- 栈的链式存储结构(C语言实现)
- 算法学习(三)堆排序
- 解决在编程方式下无法访问Spark Master问题
- dtu连接mysql_Azure SQL 数据库中的DTU和eDTU是什么
- 专注物联网人工智能服务 云知声芯起航
- 『市场基础变量计算』
- 保存numpy数组到excel
- 一个简单有趣的爬虫-----爬取百度翻译功能
- 由“戴尔用博客与中国用户沟通”想起
- iMindMap邀您一起“约惠”开学季
- 百度AI市场热品试用 | 视派尔近红外活体识别双目摄像头模组
- 一亩三分地 新手上路 网站规则 - 满分5大米(适用于所有用户) 答案 新手入门
- 写给科研人:身体健康和好的心态缺一不可
- 电商数据分析--常见的数据采集工具及方法
- ios的Safari浏览器下视频播放问题
热门文章
- 解决树莓派鼠标延迟/迟滞问题-转CSDN博主“Deiki”-sunziren
- 教你如何修改ROS机器人工作空间文件夹名字
- 数学建模 matlab 数据建模基础
- C++ COM组件编写初探(上)
- PTA——递归法求最大公约数
- 深度学习-使用tensorflow实现猫狗识别
- 关于Introduction、Discussion的杂记
- 【Web项目】点餐系统
- RH850从0搭建Autosar开发环境【2】- Davinci Configurator配置工程导入DBC与CDD文件
- matlab dwt实现原理,基于DWT的数字水印算法的MatLab实现.pdf