1shiro实现MD5加密
@Test
public void testMD5() throws Exception{
Md5Hash hash = new Md5Hash("1","admin",3);
System.out.println(hash);//f3559efea469bd6de83d27d4284b4a7a
}
第一参数为需要加密的数字
第二个参数为加密的第一道程序(加盐)
第三个参数一般为加密的层数
不加层数会很容易简单的利用官网软件进行破解
2.在shiro框架中实现密码加密
1.首先在spring-shiro.xml中配置,告诉shiro实现的是哪一种加密算法
<!--指定当前需要使用的凭证匹配器-->
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 指定加密算法 -->
<property name="hashAlgorithmName" value="MD5"/>
</bean>
2.将使用的凭证匹配器注入到当前自定义的Realm中
@Autowired
public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
super.setCredentialsMatcher(credentialsMatcher);
}
3.在添加用户的时候,需要对用户的密码进行加密
Md5Hash md5Hash = new Md5Hash(employee.getPassword(), employee.getName(),2);
employee.setPassword(md5Hash.toString());
4.在认证的实现类中告诉shiro我们的盐是哪个值(一般都以用户名为盐)
SimpleAuthenticationInfo ret=new SimpleAuthenticationInfo(employee,employee.getPassword(),ByteSource.Util.bytes(employee.getName()),this.getName());
5.测试之前,先把数据库里边的密码改为加密后的数据
-- 使用 MD5 函数对密码进行加密,其中 name用户名 作为盐使用
update employee set password = MD5(concat(name,password));