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

需求:实际开发时realm要进行md5值(明文散列后的值)的对比



此文老猫原创,转载请加本文连接:http://blog.youkuaiyun.com/nthack5730/article/details/50994100

更多有关老猫的文章:http://blog.youkuaiyun.com/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【盐】,进行加密相当于对原始的密码+盐进行散列。
        |---正常使用散列方法时:
            |---在程序中对原始密码+盐进行散列,将散列值存储在数据库州农工,并且还要将盐也存储在数据库中
            |---如果进行密码比对,使用相同方法,将原始密码+盐进行散列,进行比对

此文老猫原创,转载请加本文连接:http://blog.youkuaiyun.com/nthack5730/article/details/50994100

更多有关老猫的文章:http://blog.youkuaiyun.com/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());
    }
}




此文老猫原创,转载请加本文连接:http://blog.youkuaiyun.com/nthack5730/article/details/50994100

更多有关老猫的文章:http://blog.youkuaiyun.com/nthack5730





shiro执行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);


}

}


此文老猫原创,转载请加本文连接:http://blog.youkuaiyun.com/nthack5730/article/details/50994100

更多有关老猫的文章:http://blog.youkuaiyun.com/nthack5730


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值