接本人的上篇文章《Shiro认证、角色、权限》,这篇文章我们来学习shiro的加盐加密实现

自定义Realm:

package com.czhappy.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.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
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; /**
* 自定义Realm
*/
public class CustomRealm extends AuthorizingRealm { Map<String, String> userMap = new HashMap<String, String>(16);
{
userMap.put("chen", "eeb9bad681184779aa6570e402d6ef6c");
super.setName("customRealm");
} //角色权限验证
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String userName = (String) principalCollection.getPrimaryPrincipal();
//从数据库或者缓存中获取角色数据
Set<String> roleSet = getRolesByUserName(userName); //从数据库或者缓存中获取权限数据
Set<String> permissionSet = getPermissionsByUserName(userName); SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setRoles(roleSet);
simpleAuthorizationInfo.setStringPermissions(permissionSet);
return simpleAuthorizationInfo;
} /**
* 模拟从数据库或者缓存中获取权限数据
* @param userName
* @return
*/
private Set<String> getPermissionsByUserName(String userName) {
Set<String> sets = new HashSet<String>();
sets.add("user:add");
sets.add("user:delete");
return sets;
} /**
* 模拟从数据库或者缓存中获取角色数据
* @param userName
* @return
*/
private Set<String> getRolesByUserName(String userName) {
Set<String> sets = new HashSet<String>();
sets.add("admin");
sets.add("user");
return sets;
} //登录验证
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//从主体传过来的认证信息中获取用户名
String userName = (String) authenticationToken.getPrincipal();
//通过用户名到数据库中获取凭证
String password = getPasswordByUsername(userName); if(password == null){
return null;
}
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo
(userName, password, "customRealm");
//设置加盐参数
simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("hello"));
return simpleAuthenticationInfo;
} /**
* 模拟数据库访问
* @param userName
* @return
*/
private String getPasswordByUsername(String userName) {
return userMap.get(userName);
}
}

编写测试实现类:

设置以md5的加密方式加密,加盐的参数设置为:hello

package com.czhappy.test;

import com.czhappy.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test; public class CustomRealmTest { @Test
public void testAuthentication() {
CustomRealm customRealm = new CustomRealm();
//创建SecurityManager环境
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(customRealm); HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
matcher.setHashAlgorithmName("md5");//加密方式
matcher.setHashIterations(1);//加密次数 customRealm.setCredentialsMatcher(matcher); //主体提交认证请求
SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("chen", "123456");
subject.login(token); System.out.println("isAuthenticated=" + subject.isAuthenticated());
subject.checkRole("admin");
subject.checkPermissions("user:delete", "user:add"); } public static void main(String[] args) {
Md5Hash md5Hash = new Md5Hash("123456", "hello");
System.out.println(md5Hash.toString());
}
}

Shiro加盐加密的更多相关文章

  1. 一种简单的md5加盐加密的方法(防止彩虹表撞库)

    md5加密(或者说摘要算法)大家都很熟悉了 就不解释了 现在很多数据库设计都喜欢用单向加密的方式保存密码,验证时对提交的密码再次加密之后做密文对比 /// <summary> 使用MD5加 ...

  2. 加盐加密salt

    加盐加密是一种对系统登录口令的加密方式,它实现的方式是将每一个口令同一个叫做”盐“(salt)的n位随机数相关联. 加盐加密是一种对系统登录口令的加密方式,它实现的方式是将每一个口令同一个叫做”盐“( ...

  3. md5加密,md5加盐加密和解密

    package com.java.test; import java.security.MessageDigest; import java.security.SecureRandom; import ...

  4. 【koa2】用户注册、登录校验与加盐加密

    加密与解密 先介绍一下关于服务端用户名跟密码的存储状态,我们知道当前端在注册一个新用户时,会在表单内填入用户名和密码,并通过post请求提交到服务器,服务器再把用户名和密码从ctx.request.b ...

  5. MD5 加密 以及 加盐加密

    这是MD5加密 - (NSString *)MD5Hash { const char *cStr = [self UTF8String]; unsigned char result[16]; CC_M ...

  6. MD5加盐加密

    package com.chauvet.utils; import java.security.NoSuchAlgorithmException; import java.util.Random; / ...

  7. MD5 加盐加密

    一.概述 MD5(Message Digest  Algorithm 5),是一种散列算法,是不可逆的,即通过md5加密之后没办法得到原文,没有解密算法. 在一般的项目中都会有登录注册功能,最简单的, ...

  8. shiro认证+盐加密

    Shiro认证 导入pom依赖 <shiro.version>1.2.5</shiro.version> <!--shiro--> <dependency&g ...

  9. MD5加密加盐

    Java实现MD5的随机加盐加密,这样以来就很难解密了,必须使用原密码才能正常的登录系统了,以下为Java实现的MD5随机加盐加密,以及使用Apache的Hex类实现Hex(16进制字符串和)和字节数 ...

随机推荐

  1. ACWing P372 棋盘覆盖 题解

    Analysis 这是一个经典的二分图问题,我们将图进行奇偶染色,注意边界条件的判断.再跑一遍匈牙利算法就行了,跟上一题很像. #include<iostream> #include< ...

  2. kubernetes 部署metricserver

    本篇适用于kubeadm部署的k8s的集群 安装环境:首先要部署好k8s的集群,版本是1.11.1,我的虚拟机部署的,一个master节点,一个node节点.笔记本性能有限 下载metrics-ser ...

  3. 10分钟教你用eclipse上传代码到GitHub

    关注我们的公众号哦!获取更多精彩消息! 好久没有更新了,这两天小编在整理以前的代码,上传到GitHub做备份. 加上现在GitHub的私有仓库不是免费了嘛,所以今天顺便给大家讲讲怎么用eclipse上 ...

  4. 2018-2019-2 20165234 《网络对抗技术》 Exp6 信息搜集与漏洞扫描

    Exp6 信息搜集与漏洞扫描 实验内容 1. 各种搜索技巧的应用 2. DNS IP注册信息的查询 3. 基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务的查点(以自己主机为目标) 4 ...

  5. 用Fiddler模拟低速网络环境(弱网)

    原文链接:http://caibaojian.com/fiddler.html 有时候宽频网路用习惯了… 在开发的过程就比较少去考虑最佳化的问题… 但当有人反应说「你的网页好慢」 甚至当网路速度慢,会 ...

  6. Coroutine 协程

    https://en.wikipedia.org/wiki/Coroutine Coroutines are computer program components that generalize s ...

  7. JavaScript原型,原型链 ? 有什么特点?

    每个对象都会在其内部初始化一个属性,就是prototype(原型),当我们访问一个对象的属性时, 如果这个对象内部不存在这个属性,那么他就会去prototype里找这个属性,这个prototype又会 ...

  8. Spring Boot项目跳转不到jsp页面是怎么回事

    SpringBoot访问不了JSP但却能进入后台 一直报错: 解决方法: 改成下面的

  9. Webpack中的sourcemap以及如何在生产和开发环境中合理的设置

    一 . 从Sourcemap和Data URL说起 (1)什么是Sourcemap? 我们在打包中,将开发环境中源代码经过压缩,去空格,babel编译转化,最终可以得到适用于生产环境的项目代码,这样处 ...

  10. golang 不足

    滴滴出行技术总监:关于技术选型的那些事儿 原创: 杜欢 InfoQ 2017-02-26   https://mp.weixin.qq.com/s/6EtLzMhdtQijRA7Xrn_pTg     ...