从数据库中获取的密码不应该是“123456”这种明文,应该是加密过的字符串,这样可以提高安全性。可以用MD5算法实现加密,需要替换当前Realm的credentitalsMatcher属性。直接使用HashedCredentitalsMatcher对象,并设置加密算计即可. 步骤: 1. 在applicationContext.xml中找到,id=jdbcRealm,并改为如下: 这样前台输入的密码就会被加密,但是从数据库查出来的密码还是”123456”,所以要把数据库查出来的密码加密.(注意:工作中这一步不需要,因为用户注册成功后保存到数据库的已经是加密过的字符串了)
<bean id="jdbcRealm" class="com.dw.shiro.realms.ShiroRealm"> <property name="credentialsMatcher"> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="MD5"></property> <property name="hashIterations" value="1024"></property> </bean> </property> </bean>2. 计算123456机密后的字符串为 fc1709d0a95a6be30bc5926fdb7f22f4
public static void main(String[] args) { String hashAlgorithmName="MD5"; Object credentials="123456"; Object salt=null; int hashIterations=1024; Object result=new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations); System.out.println(result); }3. ShiroRealm中计算过加密后的密码后,改上边定义的密码
再次输入abc 123456可实现登录
