1. 程式人生 > >shiro學習筆記(4)--加密

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;
    }