shiro密码的比对,密码的MD5加密,MD5盐值加密,多个Relme
有具体问题的可以参考之前的关于shiro的博文,关于shiro的博文均是一次工程的内容

使用其提供的接口实现
<!--
.配置Realm
.1直接实现Realm接口的bean
-->
<bean id="jdbcRealm" class="com.MrChengs.shiro.realms.ShiroRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <!-- 加密的方法 -->
<property name="hashAlgorithmName" value="MD5"></property> <!-- 指定加密的次数 -->
<property name="hashIterations" value=""></property>
</bean>
</property> </bean>
看源码:
public SimpleHash(String algorithmName, Object source, Object salt, int hashIterations)
throws CodecException, UnknownAlgorithmException {
if (!StringUtils.hasText(algorithmName)) {
throw new NullPointerException("algorithmName argument cannot be null or empty.");
}
this.algorithmName = algorithmName;
this.iterations = Math.max(DEFAULT_ITERATIONS, hashIterations);
ByteSource saltBytes = null;
if (salt != null) {
saltBytes = convertSaltToBytes(salt);
this.salt = saltBytes;
}
ByteSource sourceBytes = convertSourceToBytes(source);
hash(sourceBytes, saltBytes, hashIterations);
}
测试加密:
public static void main(String[] args) {
String hash="MD5";
Object cred = "";
Object salt = null;
int hashInter = ;
//加密的类
System.out.println(new SimpleHash(hash, cred, salt, hashInter));
}
fc1709d0a95a6be30bc5926fdb7f22f4
MD5盐值加密
//盐值
ByteSource credentialsSalt = ByteSource.Util.bytes(username);
此时放置的不在是明文的密码
ShiroRealm.java
//6.根据用户的情况来构建AuthenticationInfo并且返回
//以下信息是从数据库中获取的
//principal:认证的实体信息,可以是username,也可以是数据表对应的实体对象
Object principal = username;
//credentials:密码
Object credentials = null;
if("user".equals(username)){
//计算后user密码为123456的盐值
credentials = "2044dc18864ca3bc408359a0fb13c2a7";
}else if("admin".equals(username)){
//计算和admin密码为123456的盐值
credentials = "30beaf2a87d54ebe889cfccc076247ad";
} //realmName:当前realm对象为name,调用父类的getName()方法即可
String realmName = getName(); //盐值
ByteSource credentialsSalt = ByteSource.Util.bytes(username); SimpleAuthenticationInfo info = null;//new SimpleAuthenticationInfo(principal, credentials, realmName);
info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
return info;
}
盐值的计算:
public static void main(String[] args) {
String hash="MD5";
Object cred = "";
Object salt = "admin";
int hashInter = ;
//加密的类
System.out.println(new SimpleHash(hash, cred, salt, hashInter));
//new SimpleHash(algorithmName, source, salt, hashIterations)
}
在测试中,只有用户名为user/admin 密码为123456才能成功登陆
多Realm
创建新的类

SecondRealm。java
public class SecondRealm extends AuthenticatingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
System.out.println("SecondRealm-->");
//1.把AuthenticationToken转为UsernamePasswordToken
UsernamePasswordToken upToken = (UsernamePasswordToken) arg0;
//2.从UsernamePasswordToken获取username
String username = upToken.getUsername();
//3.调用数据库的方法,从数据库查询username对应的用户记录
System.out.println("从数据库中获取username:" + username);
//4.若用户不存在可以抛出异常 UnKnownAccountException异常
if("unknow".equals(username)){
throw new UnknownAccountException("username 不存在");
}
//5.根据用户信息的清空决定是否需要抛出其他的异常
if("monster".equals(username)){
throw new LockedAccountException("用户被锁定");
}
//6.根据用户的情况来构建AuthenticationInfo并且返回
//以下信息是从数据库中获取的
//principal:认证的实体信息,可以是username,也可以是数据表对应的实体对象
Object principal = username;
//credentials:密码
Object credentials = null;
if("user".equals(username)){
credentials = "6e3be0247455b9298f47eac8e57a07214ef84115";
}else if("admin".equals(username)){
credentials = "ff9633d047eaaf9861984ed86e5f73f904647716";
}
//realmName:当前realm对象为name,调用父类的getName()方法即可
String realmName = getName();
//盐值
ByteSource credentialsSalt = ByteSource.Util.bytes(username);
SimpleAuthenticationInfo info = null;//new SimpleAuthenticationInfo(principal, credentials, realmName);
info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
return info;
}
public static void main(String[] args) {
String hash="SHA1";
Object cred = "";
Object salt = "user";
int hashInter = ;
//加密的类
System.out.println(new SimpleHash(hash, cred, salt, hashInter));
//new SimpleHash(algorithmName, source, salt, hashIterations)
}
}
加密方式是SHA1
<!--
.配置SecurityManager
-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="cacheManager" ref="cacheManager"/> <!-- 此时这个属性需要注释使用下面的属性配置
<property name="realm" ref="jdbcRealm"/>
-->
<property name="authenticator" ref="autheniicator"></property> </bean>
<!-- 认证器 -->
<bean id="autheniicator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
<property name="realms">
<list>
<ref bean="jdbcRealm"/>
<ref bean="SecondRealm"/>
</list>
</property>
</bean> <!--
.配置Realm
.1直接实现Realm接口的bean
-->
<bean id="jdbcRealm" class="com.MrChengs.shiro.realms.ShiroRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <!-- 加密的方法 -->
<property name="hashAlgorithmName" value="MD5"></property> <!-- 指定加密的次数 -->
<property name="hashIterations" value=""></property>
</bean>
</property>
</bean> <bean id="SecondRealm" class="com.MrChengs.shiro.realms.SecondRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <!-- 加密的方法 -->
<property name="hashAlgorithmName" value="SHA1"></property> <!-- 指定加密的次数 -->
<property name="hashIterations" value=""></property>
</bean>
</property>
</bean>
执行的顺序和list的顺序有关
<bean id="autheniicator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
<property name="realms">
<list>
<ref bean="jdbcRealm"/>
<ref bean="SecondRealm"/>
</list>
</property>
</bean>

shiro密码的比对,密码的MD5加密,MD5盐值加密,多个Relme的更多相关文章
- Java和JS MD5加密-附盐值加密demo
JAVA和JS的MD5加密 经过测试:字母和数据好使,中文不好使. 源码如下: ** * 类MD5Util.java的实现描述: * */public class MD5Util { // 获得MD5 ...
- Spring Security中的MD5盐值加密
在 spring Security 文档中有这么一句话: "盐值的原理非常简单,就是先把密码和盐值指定的内容合并在一起,再使用md5对合并后的内容进行演算,这样一来,就算密码是一个很常见的字 ...
- c# MD5及盐值加密
using System;using System.Collections.Generic;using System.Linq;using System.Security.Cryptography;u ...
- c# MD5盐值加密
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...
- shiro盐值加密并验证
在数据表中存的密码不应该是123456,而应该是123456加密之后的字符串,而且还要求这个加密算法是不可逆的,即由加密后的字符串不能反推回来原来的密码,如果能反推回来那这个加密是没有意义的.著名的加 ...
- MD5盐值加密
加密思路 思路解析:(数据解析过程基于16进制来处理的,加密后为16进制字符串) 加密阶段: 对一个字符串进行MD5加密,我们需要使用到MessageDigest(消息摘要对象),需要一个盐值(sal ...
- shiro 密码的MD5盐值加密
- hashlib 文件校验,MD5动态加盐返回加密后字符
hashlib 文件校验 # for循环校验 import hashlib def check_md5(file): ret = hashlib.md5() with open(file, mode= ...
- 给MD5加上salt随机盐值加密算法实现密码安全的php实现
给MD5加上salt随机盐值加密算法实现密码安全的php实现 如果直接对密码进行散列,那么黑客可以对通过获得这个密码散列值,然后通过查散列值字典(例如MD5密码破解网站),得到某用户的密码.加上sal ...
随机推荐
- MongoDb 学习笔记(一) --- MongoDb 数据库介绍、安装、使用
1.数据库和文件的主要区别 . 数据库有数据库表.行和列的概念,让我们存储操作数据更方便 . 数据库提供了非常方便的接口,可以让 nodejs.php java .net 很方便的实现增加修改删除功能 ...
- [android] post请求接口demo测试代码
MainActivity.java package com.tsh.test; import java.io.InputStream; import java.io.OutputStream; imp ...
- ps命令详解加例子
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- spring boot 定时任务
定时任务实现方式 三种: 1) Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务. 最早的时候就是这样写定时任务的. 2) 开源的第三方框 ...
- asm demo
出处:https://blog.csdn.net/zhangjg_blog/article/details/22976929 package com.gxf.asm; import org.objec ...
- Spring课程 Spring入门篇 6-1 Spring AOP API的PointCut、advice的概念及应用
本节主要是模拟spring aop 的过程. 实现spring aop的过程 这一节老师虽然说是以后在工作中不常用这些api,实际上了解还是有好处的, 我们可以从中模拟一下spring aop的过程. ...
- jquery中的$().each和$.each的区别
jquery中的$().each和$.each的区别 注意:jquery中的$().each和$.each的区别,前者只能遍历数组,后者可以遍历数组和对象 备注:sinobook项目中地名本体相关地按 ...
- zookeeper安装及使用
0. 下载并安装 1. 开机启动 cd /etc/rc.d/init.d touch zookeeper vi zookeeper #!/bin/bash #chkconfig #descripti ...
- ios 下防止整个网页滑动(阻尼回弹 . 瞒天过海,骗IOS,把阻尼回弹限制在滚动区div内
下面是一个手机APP页面,分成上中下三部分,最上面和最下面是固定的,中间可以滚动.这是常见的APP布局方式. <style> .box{ overflow: auto; -webkit-o ...
- 理解JS表达式
表达式:是由运算元和运算符(可选)构成,并产生运算结果的语法结构. 基本表达式 以下在ES5中被称为基本表达式(Primary Expression) this.null.arguments等内置的关 ...