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

3.1.0版本中新的PasswordEncoder继承关系

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

    Public interface PasswordEncoder{
      String encode(String rawPassword);
      Boolean matches(String rawPassword,String encodedPassword);
    }
   

 定义了两个方法,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,了解了原理我们可以来测试一下:

  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.     }  

   
  1. 
    


   public static void main(String[] args) {
          System.out.println(EncryptUtil.encrypt("test"));
          System.out.println(EncryptUtil.encrypt("test"));
          System.out.println(EncryptUtil.encrypt("test"));
    }
 
        b84e62f1d332154e1db9da86c1ec30ee86c43f0307fd1c4b06873167eb08ca674bbf72cfe2a99b5d 
        05b78338cb2c275e471533b05eff31979f5e4df9695ab72eb55cfe3abd92a7d581f250fc9d37d801 
        aba3493a7cd4e646a1ea33b2a1b074b03899b5e76618bfea70542fa337e286cb2ac27bde4f4be903 



	
	private boolean checkPassword(Bdf2User bdf2User,String password) {
		ShaPasswordEncoder passwordEncoder = new ShaPasswordEncoder() ;
		boolean ret = passwordEncoder.isPasswordValid(bdf2User.getPassword(), password, bdf2User.getSalt());
		if(ret)
			return true;
		return false;
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值