zoukankan      html  css  js  c++  java
  • ShiroUtil 对密码进行加密

    ShiroUtil 对密码进行加密

    package com.mozq.sb.shiro02.config;
    
    import org.apache.shiro.authc.SimpleAuthenticationInfo;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
    import org.apache.shiro.crypto.hash.SimpleHash;
    
    public class ShiroUtil {
        public static final String alg = "MD5";
        public static final Integer iterator = 3;
        public static final boolean hex = true;
    
        public static String encode(String pass){
            SimpleHash simpleHash = new SimpleHash(alg, pass, null, iterator);
            //simpleHash.setIterations(iterator);//错误,因为哈希算法是在构造器中调用,不能创建对象后再设置次数
            return hex ? simpleHash.toHex(): simpleHash.toBase64();
            /*System.out.println(Arrays.toString(simpleHash.getBytes()));
            System.out.println(simpleHash.toHex());
            System.out.println(simpleHash.toBase64());*/
        }
    
        public static void main(String[] args) {
            String pass = "jiechang";
            // 1.使用工具类加密密码
            String hashPass = ShiroUtil.encode(pass);
            System.out.println(hashPass);
    
            // 2.创建密码匹配器,注入自定义realm中
            HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher(alg);
            credentialsMatcher.setStoredCredentialsHexEncoded(hex);
            credentialsMatcher.setHashIterations(iterator);
    
            // 3.调用主体登录,将调用匹配方法。
            UsernamePasswordToken token = new UsernamePasswordToken("liubei", pass);
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo("liubei", hashPass, "A");
            boolean OK = credentialsMatcher.doCredentialsMatch(token, info);
            System.out.println(OK);
        }
    }
    
  • 相关阅读:
    【iOS开发-从网络上获取图片尺寸】
    【iOS开发之静态库、动态库】
    【iOS开发之OC和JS互调】
    【iOS之runtime、runloop】
    【iOS开发之C语言】sprintf,strncpy,strcmp三个函数的区别
    计算机中的存储单位
    linux命令行
    python的安装
    Java的跨平台特性
    方法的重写(override)两同两小一大原则:
  • 原文地址:https://www.cnblogs.com/mozq/p/11967872.html
Copyright © 2011-2022 走看看