此文老猫原创,转载请加本文连接:http://blog.youkuaiyun.com/nthack5730/article/details/50994100
更多有关老猫的文章:http://blog.youkuaiyun.com/nthack5730
[main]
#定义凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#指定散列算法
credentialsMatcher.hashAlgorithmName=md5
#指定散列次数
credentialsMatcher.hashIterations=1
#将凭证匹配器设置到realm
customRealm=cn.marer.shiro.realm.CustomRealmMd5
customRealm.credentialsMatcher=$credentialsMatcher
securityManager.realms=$customRealm
此文老猫原创,转载请加本文连接:http://blog.youkuaiyun.com/nthack5730/article/details/50994100
更多有关老猫的文章:http://blog.youkuaiyun.com/nthack5730
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.junit.Test;
public classMD5Test {
@Test
public void testMD5(){
//原始密码:
String source = "11111";
//盐
String salt = "qwertyw";
//散列次数
int hashIterations = 1;
//构造方法中:
//第一个参数:明文,原始密码
//第二个参数:盐,通过使用随机数
//第三个参数:散列的次数,比如散列两次
Md5Hash md5 = new Md5Hash(source, salt, hashIterations);
System.out.println(md5.toString());
//散列一次的值:696863f81beabaa84d21cdbaa3e76239
//第一个参数:散列算法
SimpleHash simpleHash = new SimpleHash("md5", source, salt, hashIterations);
System.out.println(simpleHash.toString());
}
}
此文老猫原创,转载请加本文连接:http://blog.youkuaiyun.com/nthack5730/article/details/50994100
更多有关老猫的文章:http://blog.youkuaiyun.com/nthack5730
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;
/**
* @author CatScan
*
*/
public classAuthenticationTest {
//自定义realm进行散列匹配,执行MD5验证
@Test
public void testCustomRealmMD5(){
//创建securityManager工厂,通过Ini配置文件创建securityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm-md5.ini");
//创建SecurityManager
SecurityManager sm = factory.getInstance();
//将securityManager设置到当前的环境中
SecurityUtils.setSecurityManager(sm);
//从SecurityUtils里面创建一个subject
Subject subject = SecurityUtils.getSubject();
//在认证提交前,需要准备token(令牌)
//这里的将来的用户和密码是由用户输入进去的
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","11111");
try {
//执行认证提交
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
}
//是否认证通过
boolean isAuthenticated = subject.isAuthenticated();
System.out.println("是否认证通过:" + isAuthenticated);
//退出操作
subject.logout();
//是否认证通过
isAuthenticated = subject.isAuthenticated();
System.out.println("是否认证通过:" + isAuthenticated);
}
}
此文老猫原创,转载请加本文连接:http://blog.youkuaiyun.com/nthack5730/article/details/50994100
更多有关老猫的文章:http://blog.youkuaiyun.com/nthack5730

本文介绍如何在Shiro框架中实现密码的MD5散列处理及验证过程,包括配置散列算法、设置散列次数及添加盐值来增强安全性,并提供测试代码。
973

被折叠的 条评论
为什么被折叠?



