Spring Security 3.1 中功能强大的加密工具 PasswordEncoder

好吧,这种加密机制很复杂,还是看下图比较好了解:

 3.1.0版本中新的PasswordEncoder继承关系 

    

而在Spring-Security 3.1.0 版本之后,Spring-security-crypto模块中的password包提供了更给力的加密密码的支持,这个包中也有PasswordEncoder接口,接口定义如下。 

Java代码   收藏代码
  1. Public interface PasswordEncoder{  
  2.   String encode(String rawPassword);  
  3.   Boolean matches(String rawPassword,String encodedPassword);  
  4. }  
 

 定义了两个方法,encode方法是对方法加密,而match方法是用来验证密码和加密后密码是否一致的,如果一致则返回true。和authentication.encoding包中的PasswordEncoder接口相比,简化了许多。 

   位于org.springframeword.security.crypto.password包中的 

StandardPasswordEncoder类,是PasswordEncoder接口的(唯一)一个实现类,是本文所述加密方法的核心。它采用SHA-256算法,迭代1024次,使用一个密钥(site-wide secret)以及8位随机盐对原密码进行加密。 

   随机盐确保相同的密码使用多次时,产生的哈希都不同; 密钥应该与密码区别开来存放,加密时使用一个密钥即可;对hash算法迭代执行1024次增强了安全性,使暴力破解变得更困难些。 

   和上一个版本的PasswordEncoder比较,好处显而易见:盐值不用用户提供,每次随机生成;多重加密————迭代SHA算法+密钥+随机盐来对密码加密,大大增加密码破解难度。 

OK,了解了原理我们可以来测试一下:

Java代码   收藏代码
  1. import org.springframework.security.crypto.password.PasswordEncoder;  
  2. import org.springframework.security.crypto.password.StandardPasswordEncoder;  
  3.   
  4. /** 
  5.  * @author XUYI 
  6.  * Spring Security 3.1 PasswordEncoder 
  7.  */  
  8. public class EncryptUtil {  
  9.     //从配置文件中获得  
  10.     private static final String SITE_WIDE_SECRET = "my-secret-key";  
  11.     private static final PasswordEncoder encoder = new StandardPasswordEncoder(  
  12.        SITE_WIDE_SECRET);  
  13.    
  14.     public static String encrypt(String rawPassword) {  
  15.          return encoder.encode(rawPassword);  
  16.     }  
  17.    
  18.     public static boolean match(String rawPassword, String password) {  
  19.          return encoder.matches(rawPassword, password);  
  20.     }  
  21.       
  22.     public static void main(String[] args) {  
  23.         System.out.println(EncryptUtil.encrypt("每次结果都不一样伐?"));  
  24.         System.out.println(EncryptUtil.encrypt("每次结果都不一样伐?"));  
  25.         System.out.println(EncryptUtil.encrypt("每次结果都不一样伐?"));  
  26.         System.out.println(EncryptUtil.encrypt("每次结果都不一样伐?"));  
  27.         System.out.println(EncryptUtil.encrypt("每次结果都不一样伐?"));    
  28.         //但是把每次结果拿出来进行match,你会发现可以得到true。  
  29.     }  
  30.  }

猜你喜欢

转载自www.cnblogs.com/youqc/p/9079447.html