shiro学习笔记(4)--加密

一:加密
1、ini配置
说明:
(1)CredentialsMatcher在AuthenticatingRealm中注入
(2)配置HashedCredentialsMatcher加密是因为此类中参数配置方便

#自定义realm 数据加密
[main]

credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName=md5
credentialsMatcher.hashIterations=2

myrealm=com.kexq.common.shiro.realm.MyRealm
myrealm.credentialsMatcher=$credentialsMatcher

securityManager.realm=$myrealm

2、main

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //用户输入信息
        String username = (String) token.getPrincipal();
        System.out.println(username);

        //从库里查询对应用户证明信息
        String pwd = "123654";
        //加密
        String salt = "cccp2009";
        /**
         * 加密算法--参数说明
         * 1、加密方法
         * 2、加密数据
         * 3、加密盐
         * 4、散列次数
         */
        SimpleHash simpleHash = new SimpleHash("md5",pwd,salt,2);
        System.out.println("加密后的验证信息="+simpleHash);

        /**
         * 参数说明
         * 1、用户输入身份信息
         * 2、数据库中提取的用户认证信息
         * 3、加密方法
         * 4、realm name
         */
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username,simpleHash, ByteSource.Util.bytes(salt),getName());
        return simpleAuthenticationInfo;
    }

猜你喜欢

转载自blog.csdn.net/cccp_2009/article/details/82627147