欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

密码加密工具类(非简单的MD5,UUID加密);

程序员文章站 2024-03-19 13:48:46
...

实际用到的密码加盐的加密方式

这里我单独抽取成了一个工具类,有的时候直接调很方便

PasswordSaltUtil加密
// An highlighted block
public class PasswordSaltUtil {
     public static final String[] md5(String password, String salt) {
       String[] result =new String[2];
        //加密方式
        String hashAlgorithmName = "MD5";
        //盐:为了即使相同的密码不同的盐加密后的结果也不同
        ByteSource byteSalt = ByteSource.Util.bytes(salt);
        //密码
        Object source = password;
        //加密次数
        int hashIterations = 2;
        SimpleHash resultTemp = new SimpleHash(hashAlgorithmName, source, byteSalt, hashIterations);
        result[0] = byteSalt.toString();
        result[1] = resultTemp.toString();
        return result;
    }

    public static void main(String[] args) {
        System.out.println(md5(null,null)[1]);
    }
}

这里的加盐使用的是自定义的一些字符串,加MD5生成随机盐值,加密第一次是生成的盐值,第二次生成加密两次后的密码。

解密
  @Bean
    public RetryLimitHashedCredentialsMatcher hashedCredentialsMatcher(){
        RetryLimitHashedCredentialsMatcher hashedCredentialsMatcher = new RetryLimitHashedCredentialsMatcher();
        hashedCredentialsMatcher.setRedisManager(redisManager());
        // 使用md5 算法进行加密
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        // 设置散列次数: 意为加密几次
        hashedCredentialsMatcher.setHashIterations(2);
        //是否存储为16进制
        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return hashedCredentialsMatcher;
    }

这里因为这是集成Shiro,加密方式配置在了框架里,通过shiro重写doGetAuthenticationInfo方法来完成解密,起到了杀人不见血效果,下图看源码

  @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        LOGGER.info("身份认证开始");
        EasyTypeToken usernamePasswordToken = (EasyTypeToken) token;
        String account = usernamePasswordToken.getUsername();
        String password = new String(usernamePasswordToken.getPassword());
        boolean flag = true;
        if(StringUtils.isEmpty(password)){
            LOGGER.info("免密登录");
            flag = false;
        }else{
            LOGGER.info("密码登录");
            flag = true;
        }
        //手机号码查询
        SportControUser user = userInfoService.findByUserAccount(account);
        //可以在这里直接对用户名校验,或者调用 CredentialsMatcher 校验
        if (user == null) {
            throw new UnknownAccountException("该用户不存在!");
        }
        if (UserInfoConstant.STATUS_FROZEN.equals(user.getStatus())) {
            throw new LockedAccountException("账号已被冻结!");
        }
        if (UserInfoConstant.STATUS_TITLE.equals(user.getStatus())) {
            throw new DisabledAccountException("账号已被禁用!");
        }
        SimpleAuthenticationInfo info;
        if(flag){
            info = new SimpleAuthenticationInfo(user.getUserAccount(),user.getPassword(),new MyByteSource(user.getUserAccount()), getName());
        }else{
            info = new SimpleAuthenticationInfo(user.getUserAccount(),"", getName());
        }
        return info;
    }

直接对用户输入的密码进行加密的配置方式,对其进行再次判断。