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);
        }
    }
    
  • 相关阅读:
    LGWR Trace Warning: Log Write Time ? Maybe not an issue
    Transaction & Undo
    XML Function at a glance
    Java step by step (1) : simple Spring IoC container
    First Impression on BBED: recover deleted rows
    【SQL*PLUS】Copy Command
    SYS_CONTEXT('USERENV', 'HOST') Return NULL & Oracle Fixed Tables
    ORA12519
    Some ORAs (32001, 00106)
    First Impression on BBED: explore block structure using map command
  • 原文地址:https://www.cnblogs.com/mozq/p/11967872.html
Copyright © 2011-2022 走看看