zoukankan      html  css  js  c++  java
  • Shiro密码加密

    Shiro密码加密

    相关类

    org.apache.shiro.authc.credential.CredentialsMatcher

    org.apache.shiro.authc.credential.SimpleCredentialsMatcher

    org.apache.shiro.authc.credential.HashedCredentialsMatcher

    org.apache.shiro.crypto.hash.Md5Hash

    org.apache.shiro.crypto.hash.SimpleHash

    测试类

    密码加密Md5Hash和SimpleHash

    加密算法 迭代次数 Hex/Base64

    package com.mozq.shiro.shiro01.test;
    
    import org.apache.shiro.crypto.hash.Md5Hash;
    import org.apache.shiro.crypto.hash.SimpleHash;
    
    public class CryptoTest_01 {
        public static void main(String[] args) {
            String password = "changhe";
            String salt = "changheluoriyuan";
            int hashIterations = 5;
            //哈希
            Md5Hash md5Hash = new Md5Hash(password);
            System.out.println(md5Hash.toString());//40da95a4e76c2678a81aa4e114349063
    
            //哈希+加盐
            Md5Hash md5SaltHash = new Md5Hash(password, salt);
            System.out.println(md5SaltHash.toString());//334f0241faaab8ffe81e4a6b9c493b21
    
            //哈希+加盐+次数
            Md5Hash md5SaltIterateHash = new Md5Hash(password, salt, hashIterations);
            System.out.println(md5SaltIterateHash.toString());//96ff601332575cb7c3be7304aaad57b1
    
            SimpleHash simpleHash = new SimpleHash("MD5", password, salt, hashIterations);
            System.out.println(simpleHash.toString());//96ff601332575cb7c3be7304aaad57b1
            /*
            Md5Hash继承SimpleHash。能使用Md5Hash的地方就能够使用SimpleHash。只用看2个类的构造器就可以明白它们的关系。
            
            Hex/Base64
            再者就是SimpleHash的 toString() toHex() toBase64() 3个方法的关系。
            它们与 HashedCredentialsMatcher 的 storedCredentialsHexEncoded 字段是对应的。
             */
        }
    }
    

    密码解密HashedCredentialsMatcher和SimpleAuthenticationInfo

    HashedCredentialsMatcher 存储 加密算法 迭代次数 Hex/Base64

    SimpleAuthenticationInfo 存储 加密后字符串

    UsernamePasswordToken 存储登录时用户名和密码。

    hashedCredentialsMatcher.doCredentialsMatch(token, info) 方法完成认证。

    加密后字符串 = 原密码 + 加密算法 + 盐 + 迭代次数 + Hex/Base64

    package com.mozq.shiro.shiro01.test;
    
    import org.apache.shiro.authc.SimpleAuthenticationInfo;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
    import org.apache.shiro.util.ByteSource;
    
    public class CryptoTest_02 {
        public static void main(String[] args) {
            String username = "liubei";
            String password = "changhe";
            String hashPassword = "96ff601332575cb7c3be7304aaad57b1";
            String salt = "changheluoriyuan";
            int hashIterations = 5;
    
            //创建认证信息和令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    //        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashPassword, "MozqRealm");
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashPassword, ByteSource.Util.bytes(salt), "MozqRealm");
    
            //创建匹配器
            HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
            hashedCredentialsMatcher.setHashAlgorithmName("MD5");//不设置算法名称将报错
            //是Hex编码的还是Base64编码的。对应SimpleHash的toHex()和toBase64()
            hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
            hashedCredentialsMatcher.setHashIterations(hashIterations);//默认迭代次数为1
    
            boolean success = hashedCredentialsMatcher.doCredentialsMatch(token, info);
            System.out.println(success);
            /*
            Exception in thread "main" java.lang.IllegalStateException: Required 'hashAlgorithmName' property has not been set.  This is required to execute the hashing algorithm.
            原因:new HashedCredentialsMatcher() 没有设置匹配器的算法名。
    
            Exception in thread "main" java.lang.IllegalArgumentException: Odd number of characters.
            原因:new SimpleAuthenticationInfo(username, hashPassword, "MozqRealm") 传入的是原始密码,匹配器中匹配时出错。
    
            false
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashPassword, "MozqRealm");
            原因:密码是加盐的,但是制造的认证信息没有提供盐,则导致后面的认证失败。
    
            true
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashPassword, ByteSource.Util.bytes(salt), "MozqRealm");
             */
        }
    }
    
  • 相关阅读:
    学习:HelloWorld项目目录
    学习:java设计模式—Adapter模式
    学习:java设计模式—Decorator模式
    MyEclipse8.5/8.6不能安装ADT
    学习:Android框架
    笔记:代码整洁之道
    JVM常用启动参数
    春雷第一声初入博客
    在Winform中更改控件导致designer中代码自动移除解决方法
    C#生成灰度图片:拖动图片到picturebox显示,拖动picturebox图片到资源管理器 (Drag & drop )
  • 原文地址:https://www.cnblogs.com/mozq/p/11761896.html
Copyright © 2011-2022 走看看