shiro的加密 深入浅出

来源:互联网 发布:图像分类算法 简单 编辑:程序博客网 时间:2024/05/21 20:26

密码的比对:


通过 AuthenticatingRealm 的credentialsMatcher 属性来进行的密码的比对!


 替换当前 Realm 的 credentialsMatcher 属性. 直接使用 HashedCredentialsMatcher 对象, 并设置加密算法即可. 


配置application-shiro.xml

   <!--     3. 配置 Realm     3.1 直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean     <bean id="jdbcRealm" class="com.zhongyou.shiro.realms.ShiroRealm"></bean>    -->         <bean id="jdbcRealm" class="com.zhongyou.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>


1. 如何把一个字符串加密为 MD5 

public static void main(String[] args) {String hashAlgorithmName = "MD5";Object credentials = "123456";//Object salt = ByteSource.Util.bytes("user");
Object salt = null;int hashIterations = 1024;Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);System.out.println(result);}










0 0
原创粉丝点击