spring Security 文档中有这么一句话: "盐值的原理非常简单,就是先把密码和盐值指定的内容合并在一起,再使用md5对合并后的内容进行演算,这样一来,就算密码是一个很常见的字符串,再加上用户名,最后算出来的md5值就没那么容易猜出来了。因为攻击者不知道盐值的值,也很难反算出密码原文。"

问题如何理解这句话: "先把密码和盐值指定的内容合并在一起,再使用md5对合并后的内容进行演算".  例如, 在 applicationContext-security.xml 文件中的配置如下:

<authentication-provider user-service-ref="userDetailsService">     
     <password-encoder hash="md5">  
         <!-- 将每个用户的username作为盐值 -->  
         <salt-source user-property="username"/>  
     </password-encoder>  
</authentication-provider>  

假设用户名是 Tom, 密码为 123456, 那么在数据库中存放的值应该是什么?

通过查看 Spring Security 的 org.springframework.security.providers.encoding.BasePasswordEncoder 类可知 Spring Security 通过如下方式来匹配在数据库中已经被盐值加密的密码:

protected String mergePasswordAndSalt(String password, Object salt, boolean strict) {  
        if (password == null) {  
            password = "";  
        }  
  
        if (strict && (salt != null)) {  
            if ((salt.toString().lastIndexOf("{") != -1) || (salt.toString().lastIndexOf("}") != -1)) {  
                throw new IllegalArgumentException("Cannot use { or } in salt.toString()");  
            }  
        }  
  
        if ((salt == null) || "".equals(salt)) {  
            return password;  
        } else {  
            return password + "{" + salt.toString() + "}";  
        }  
}  

即通过 password + "{" + salt.toString() + "}" 中方式把 "密码和盐值指定的内容合并在一起". 所以对于用户名是 Tom, 密码为 123456 的用户在数据库中存放的密码应该为对 "123456{Tom}" md5 验算后的值: 610c492873b994f96f93e342a56bcd68

Spring Security中的MD5盐值加密的更多相关文章

  1. shiro密码的比对,密码的MD5加密,MD5盐值加密,多个Relme

    有具体问题的可以参考之前的关于shiro的博文,关于shiro的博文均是一次工程的内容 密码的比对   通过AuthenticatingRealm的CredentialsMatcher方法 密码的加密 ...

  2. c# MD5盐值加密

    using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...

  3. MD5盐值加密

    加密思路 思路解析:(数据解析过程基于16进制来处理的,加密后为16进制字符串) 加密阶段: 对一个字符串进行MD5加密,我们需要使用到MessageDigest(消息摘要对象),需要一个盐值(sal ...

  4. shiro 密码的MD5盐值加密

  5. c# MD5及盐值加密

    using System;using System.Collections.Generic;using System.Linq;using System.Security.Cryptography;u ...

  6. SpringBoot整合shiro-MD5盐值加密

    为什么要进行密码加密? 在我们的日常生活中,许多人有着在不同网站上使用相同密码的坏习惯(包括我也是qaq),假如应用程序或服务器出现漏洞,数据被窃取,用户的明文密码直接被暴露给黑客.显然后果将不堪设想 ...

  7. 一个密码经过多次MD5加密能否提高安全性?Java MD5盐值加解密

    什么是MD5? MD5(Message Digest Algorithm 5,信息摘要算法5),是计算机广泛使用的摘要算法(又称哈希算法)之一.MD5是将一段信息,通过其不可逆的字符串变换算法,产生了 ...

  8. 浅谈使用spring security中的BCryptPasswordEncoder方法对密码进行加密与密码匹配

    浅谈使用springsecurity中的BCryptPasswordEncoder方法对密码进行加密(encode)与密码匹配(matches) spring security中的BCryptPass ...

  9. Spring Security 中的 Bcrypt

    最近在写用户管理相关的微服务,其中比较重要的问题是如何保存用户的密码,加盐哈希是一种常见的做法.知乎上有个问题大家可以先读一下: 加盐密码保存的最通用方法是? 对于每个用户的密码,都应该使用独一无二的 ...

随机推荐

  1. Warning: The following processes: -cmd.exe -java.exe are locking the following directory:

  2. [HTML5] Text Alternatives

    Most of times, we need 'alt' to the images, so it can tell the screen reader what is this image abou ...

  3. USACO holstein 超时代码

    /* ID:kevin_s1 PROG:holstein LANG:C++ */第八组数据跪了.半天都不出结果 #include <iostream> #include <cstdi ...

  4. hdu 5410 CRB and His Birthday 01背包和全然背包

    #include<stdio.h> #include<string.h> #include<vector> #include<queue> #inclu ...

  5. Visual studio 编译时copy文件、文件夹

    项目属性--生成事件 添加命令 xcopy /y /d "$(SolutionDir)Dll\Linphone\*.dll" "$(TargetDir)\Linphone ...

  6. HDU 4912 lca贪心

    Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. bzoj2806: [Ctsc2012]Cheat(SAM+DP)

    2806: [Ctsc2012]Cheat 题目:传送门 题解: 感觉这题考的更多的就是DP啊... 看完题目的第一反应就是广义SAM...(然而并不会) 再YY一会儿想起来可以直接将作文库连成一个母 ...

  8. 【POJ 2482】 Stars in Your Windows

    [题目链接] http://poj.org/problem?id=2482 [算法] 线段树 + 扫描线 [代码] #include <algorithm> #include <bi ...

  9. BZOJ 2957 分块

    思路: 记录每栋楼楼顶与原点连线的斜率 那么一栋楼可见当且仅当前面所有楼的斜率都小于这栋楼 将n栋楼分为√(0.5*n*logn)块 每一块内维护一个单调上升子序列(注意不是LCS) 比如说4 1 2 ...

  10. 多元一次方程解法 C++

    #include<iostream> #include<math.h> #include<fstream> #include<stdlib.h> usi ...