您的位置:首页 > 其它

Shiro入门7:修改自定义realm支持散列MD5密码对比

2016-03-27 21:42 369 查看
需求:实际开发时realm要进行md5值(明文散列后的值)的对比此文老猫原创,转载请加本文连接:/article/11906591.html更多有关老猫的文章:http://blog.csdn.net/nthack5730需要在配置文件中进行散列对比
shiro-realmMD5.ini
Edit
[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
散列算法:通常需要对密码进行散列,常用的有md5、sha,md5散列测试程序
|---对于MD5密码,如果知道散列后的值,可以通过穷举的算法,得到md5密码对应的明文
|
|---建议对MD5进行散列时加salt【盐】,进行加密相当于对原始的密码+盐进行散列。
|---正常使用散列方法时:
|---在程序中对原始密码+盐进行散列,将散列值存储在数据库州农工,并且还要将盐也存储在数据库中
|---如果进行密码比对,使用相同方法,将原始密码+盐进行散列,进行比对此文老猫原创,转载请加本文连接:/article/11906591.html更多有关老猫的文章:http://blog.csdn.net/nthack5730测试用的MD5转换算法:
Edit
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());

}

}
此文老猫原创,转载请加本文连接:/article/11906591.html更多有关老猫的文章:http://blog.csdn.net/nthack5730shiro执行MD5验证流程:
Edit
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);

}

}
此文老猫原创,转载请加本文连接:/article/11906591.html更多有关老猫的文章:http://blog.csdn.net/nthack5730
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: