7、Shiro加密和加盐
这里我们以md5加密方法举例,首先我们写一个main方法测试我们的密码经过md5加密之后的得到什么样的字符串:
/**
* 书写方法测试Md5Hash将密码“houru”加密之后的密文
* 但是仅仅加密还是不够的,别人知道你的加密算法之后还是可以轻易破解密码的,因此我们还要“加盐”
* 加盐:(调味)就是我们在加密密码的基础上在增加一些其他元素
* @param a
*/
public static void main(String a[]){
Md5Hash md5Hash1=new Md5Hash("houru");//只加密不加盐
Md5Hash md5Hash2=new Md5Hash("houru","jiayan");//加密又加盐
System.err.println(md5Hash1.toString());
System.err.println(md5Hash2.toString());
//没有加盐的加密结果:8a126ba89f60b97abf6185cd666ed8b4
// 加盐的加密结果: b7f30984e630bd6bd18f0b4a3196a257
}
下面的代码在上一篇博客基础上修改:
MyrealmTest.java:
package com.shiro.shiroframe; import com.shiro.myrealm.CustomRealm;
import org.apache.shiro.SecurityUtils;
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.jupiter.api.Test; public class MyrealmTest {
//引入我们自定义的realm
CustomRealm customRealm = new CustomRealm(); @Test
public void MyrealmTest() { //引入加密工具类HashedCredentialsMatcher:
HashedCredentialsMatcher hashedCredentialsMatcher=new HashedCredentialsMatcher();
//设置我们要采用的加密方法的名称:
hashedCredentialsMatcher.setHashAlgorithmName("md5");
//设置加密的次数:
hashedCredentialsMatcher.setHashIterations(1);
//给我们的自定义的realm设置hashedCredentialsMatcher对象
customRealm.setCredentialsMatcher(hashedCredentialsMatcher); DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(customRealm); SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("miyue", "houru");
subject.login(usernamePasswordToken);
System.err.println(subject.isAuthenticated());
subject.checkRoles("admin");
subject.checkPermission("user:add");
}
}
CustomRealm.java:
package com.shiro.myrealm; 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.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Hash;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set; public class CustomRealm extends AuthorizingRealm {
//认证方法
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
/**
* 重写认证方法
*/
//1、从主体传过来的认证信息中获取用户名
String username = (String) authenticationToken.getPrincipal();
//2、通过用户名到数据库获取凭证
String password = getPassWordByUsername(username); if (password == null) {
return null;
}
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("miyue", password, "test");
//注意,如果我们采用了加盐的方式加密,那么我们要给simpleAuthenticationInfo设置盐:
simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("jiayan"));
return simpleAuthenticationInfo;
} //授权方法
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
/**
* 重新授权方法
*/
String username = (String) principalCollection.getPrimaryPrincipal();
//从角色和缓存中获取角色数据
Set<String> roles = getRolesByUsername(username);
//从角色和缓存中获取权限数据
Set<String> permission = getPermissionsByUsername(username);
SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setRoles(roles);
simpleAuthorizationInfo.setStringPermissions(permission);
return simpleAuthorizationInfo;
} //下面使用map,set模拟数据库数据返回
Map<String, String> map = new HashMap<String, String>(); {
// map.put("miyue", "houru");
//模拟数据库返回的密文
map.put("miyue", "b7f30984e630bd6bd18f0b4a3196a257");
} private String getPassWordByUsername(String username) {
return map.get(username) == null ? null : map.get(username);
}
private Set<String> getRolesByUsername(String username) {
Set<String> set = new HashSet<>();
set.add("admin");
set.add("user");
return set;
}
private Set<String> getPermissionsByUsername(String username) {
Set<String> set = new HashSet<>();
set.add("user:delete");
set.add("user:add");
return set;
}
}
7、Shiro加密和加盐的更多相关文章
- Python学习笔记(七)加密加盐
MD5加密和加盐 Python的MD5加密 Python的hashlib模块的MD5加密,是比较简单一种加密,md5函数必须传入编译后的结果,否则会报错: Traceback (most recent ...
- Shiro加密
在开发的时候,很多数据我们都希望是以加密过后的形式存储起来,而不是最原始的数据. 在shiro中也提供了编码,解码,加密,加密算法实现等等一系列的内容. 编码/解码 在org.apache.shiro ...
- shiro学习(三,shiro加密)
shiro加密 使用MD5加密 认证 //自定义的Realm 域 public class CustomRealmSecret extends AuthorizingRealm { @Overrid ...
- MD5加密+加盐
了解: MD5加密,是属于不可逆的.我们知道正常使用MD5加密技术,同一字符,加密后的16进制数是不变的,自从出现彩虹表,对于公司内部员工来说,可以反查数据,获取不可能的权限,所以出现了salt算法. ...
- Python爬虫连载9-JS加密之“盐”、ajax请求
一.JS加密之“盐” 1.salt属性“盐":多用于密码学,比如我们的银行卡是六位密码,但是实际上在银行的系统里,我们输入密码后,会给原始的密码添加若干字符,形成更加难以破解的密码.这个过 ...
- Shiro 加密helloWorld
承接第一章 初解加密 只贴更改的源码,其他看上一篇. ShiroRealm.java package com.lkk.shiro.realms; import org.apache.shiro.aut ...
- shiro加密简单实现
1.添加shiro依赖 定义shiro的版本号 <shiro.ver>1.2.3</shiro.ver> 加入shiro的依赖 <dependency> <g ...
- 【Shiro】五、Apache Shiro加密
Shiro提供了更好封装,更好使用的加密算法API,可以作为平时使用的一个工具类的预选方案. Shiro的密码学 基本特性 接口驱动,基于POJO 对JCE(Java Cryptography Ext ...
- MD5加密以及验证加密-加盐
加密与解密算法: /// <summary> /// 签名字符串 32位 /// </summary> /// <param name="input" ...
随机推荐
- 10分钟,让你彻底明白Promise原理
什么是Promise?本代码用定外卖来举例子,让你明白. // 定外卖就是一个Promise,Promist的意思就是承诺// 我们定完外卖,饭不会立即到我们手中// 这时候我们和商家就要达成一个承诺 ...
- CSS的四种定位的参照物
一.static定位 HTML 元素的默认值,即没有定位,遵循正常的文档流对象. 静态定位的元素不会受到 top, bottom, left, right影响. <!DOCTYPE html&g ...
- random 方法 生成随机数
Math.random() 生成 大于等于0.0 且小于 1.0 的double 型随机数 ( 0.0 <= Math.random() < 1.0 ) 可以使用它便携简单了表达式,生成任 ...
- 目标检测数据库 PASCAL 格式的 Ground Truth 的解析函数
最近在做一个目标检测算法,训练时用到了 bootstrap 策略,于是我将PASCAL的 Ground Truth 格式的读取函数从 Matlab 改写为 C++.PASCAL 的标注格式为: # P ...
- laravel的model
1.创建模型 $ php artisan make:model Models/Issue 2.模型的白名单机制,用于赋值 class Issue extends Model { //指定表名 pr ...
- 引用vector里的元素被删除后,引用会怎么样?
引用的定义不多说,直接看做变量的别名就可以了.有一天写着写着代码,突然想到,如果对vector里某个元素设置引用后,将这个元素从vector里删除会怎么样?我思考了下,认为那个元素会被删除,但是引用还 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(4)|借用Borrowing]
[易学易懂系列|rustlang语言|零基础|快速入门|(4)] Borrowing 继续讲讲另一个重要的概念:借用(borrowing), 什么是借用? 我们先来看前一文章([易学易懂系列|rust ...
- feign请求写法
@FeignClient(value = "test", url = "${proxy.srvs.test:}") public interface ISubS ...
- create-react-app踩坑记
前言 哇,不的不说这个react 这个脚手架create-react-app脚确实有很多问题,哈哈,下面来看看吧有哪些坑: 引用sass或者less 记得16年还是几年是不支持sass,和less的, ...
- vue作业1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...