加密的过程如下:
研究这部分内容, 我们需要将数据库中Password值
都改为MD5处理后的值, 过程如下:
mysql> SELECT * FROM tb_users;
+---------+----------+----------+---------------+
| user_id | username | password | password_salt |
+---------+----------+----------+---------------+
| 1 | zhangsan | 123456 | NULL |
| 2 | lisi | 123456 | NULL |
| 3 | wangwu | 123456 | NULL |
| 4 | zhaoliu | 123456 | NULL |
| 5 | chenqi | 123456 | NULL |
+---------+----------+----------+---------------+
5 rows in set (0.00 sec)mysql> UPDATE `tb_users` SET password = md5(password);
Query OK, 5 rows affected (0.54 sec)
Rows matched: 5 Changed: 5 Warnings: 0mysql> SELECT * FROM `tb_users`;
+---------+----------+----------------------------------+---------------+
| user_id | username | password | password_salt |
+---------+----------+----------------------------------+---------------+
| 1 | zhangsan | e10adc3949ba59abbe56e057f20f883e | NULL |
| 2 | lisi | e10adc3949ba59abbe56e057f20f883e | NULL |
| 3 | wangwu | e10adc3949ba59abbe56e057f20f883e | NULL |
| 4 | zhaoliu | e10adc3949ba59abbe56e057f20f883e | NULL |
| 5 | chenqi | e10adc3949ba59abbe56e057f20f883e | NULL |
+---------+----------+----------------------------------+---------------+
5 rows in set (0.00 sec)
那么接下来我们看一下如何使Shiro支持MD5的验证, 我们在配置类中增加如下代码:
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher() {HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();hashedCredentialsMatcher.setHashAlgorithmName("md5"); // 设置数据库存储的密码格式hashedCredentialsMatcher.setHashIterations(1); // 经过几次加密, 如果是2的话则是该含义: md5(md5(值))return hashedCredentialsMatcher;
}@Bean
public MyRealm myRealm(HashedCredentialsMatcher hashedCredentialsMatcher) {MyRealm myRealm = new MyRealm();myRealm.setCredentialsMatcher(hashedCredentialsMatcher); // 将加密算法设置给 Realmreturn myRealm;
}
其这样设计的含义如下:
我们只需要将加密规则封装到Realm中, SecurityManager中的Authenticator就会根据matcher的加密规则来进行校验.