Spring security3的MD5加密和StandardPasswordEncoder的配置

656 阅读4分钟
原文链接: blog.csdn.net

1、MD5加盐值进行加密处理

application-security.xml文件配置:

  1. <authentication-manager>  
  2.         <authentication-provider>  
  3.             <password-encoder hash= "md5" >  
  4.                 <salt-source user-property= "username" />  
  5.             </password-encoder>  
  6.         </authentication-provider>  
  7.     </authentication-manager>  
<authentication-manager>
        <authentication-provider>
        	<password-encoder hash="md5" >
        		<salt-source user-property="username" />
        	</password-encoder>
        </authentication-provider>
    </authentication-manager>
直接配置 hash = 'md5' 等效于单独配置
<bean id="encoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" />
这样,登录时输入的用户密码将会使用md5(加盐值)加密后与数据库里的密文进行匹配。
对应的MD5加密和匹配Java代码:
  1. import org.springframework.security.authentication.encoding.Md5PasswordEncoder;  
  2. private static final Md5PasswordEncoder md5encoder =  new Md5PasswordEncoder();  
  3. public static String md5encode(String rawPass, String salt) {  
  4.         return md5encoder.encodePassword(rawPass, salt);  
  5.     }  
  6.       
  7.     public static boolean md5match(String encPass, String rawPass, String salt) {   
  8.         return md5encoder.isPasswordValid(encPass, rawPass, salt);  
  9.     }  
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
private static final Md5PasswordEncoder md5encoder = new Md5PasswordEncoder();
public static String md5encode(String rawPass, String salt) {
        return md5encoder.encodePassword(rawPass, salt);
    }
    
    public static boolean md5match(String encPass, String rawPass, String salt) { 
    	return md5encoder.isPasswordValid(encPass, rawPass, salt);
    }

2、Spring security3新的StandardPasswordEncoder 标准加密方式

application-security.xml文件配置:

  1. <bean id="encoder" class= "org.springframework.security.crypto.password.StandardPasswordEncoder" >  
  2.         <constructor-arg name="secret"  value="my-secret-key" /> //注意这里的秘钥值  
  3.     </bean>  
  4. <authentication-manager>  
  5.         <authentication-provider user-service-ref= "userExtendService">  
  6.             <password-encoder ref= "encoder" />  
  7.         </authentication-provider>  
  8.     </authentication-manager>  
<bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder" >
		<constructor-arg name="secret" value="my-secret-key" /> //注意这里的秘钥值
	</bean>
<authentication-manager>
        <authentication-provider user-service-ref="userExtendService">
        	<password-encoder ref="encoder" />
        </authentication-provider>
    </authentication-manager>

对应的加密和匹配Java代码:
  1. private static final PasswordEncoder encoder =  new StandardPasswordEncoder("my-secret-key");//秘钥值  
  2.       
  3.     public static String encrypt(String rawPassword) {  
  4.          return encoder.encode(rawPassword);  
  5.     }  
  6.    
  7.     public static boolean match(String rawPassword, String password) {  
  8.          return encoder.matches(rawPassword, password);  
  9.     }  
private static final PasswordEncoder encoder = new StandardPasswordEncoder("my-secret-key");//秘钥值
    
    public static String encrypt(String rawPassword) {
         return encoder.encode(rawPassword);
    }
 
    public static boolean match(String rawPassword, String password) {
         return encoder.matches(rawPassword, password);
    }

盐值不需要用户提供,每次随机生成;多重加密——迭代SHA算法+密钥+随机盐来对密码加密,大大增加密码破解难度,加密后得到的密码是80位。
注意这里的秘钥配置,不配置秘钥也是可以的。

附:StandardPasswordEncoder.java源码中的构造函数:

  1. /** 
  2.      * Constructs a standard password encoder with no additional secret value. 
  3.      */  
  4. public StandardPasswordEncoder() {  
  5.         this("");  
  6.     }  
  7.   
  8.     /** 
  9.      * Constructs a standard password encoder with a secret value which is also included in the 
  10.      * password hash. 
  11.      * 
  12.      * @param secret the secret key used in the encoding process (should not be shared) 
  13.      */  
  14.     public StandardPasswordEncoder(CharSequence secret) {  
  15.         this("SHA-256", secret);  
  16.     }  
  17.   
  18.     // internal helpers  
  19.     private StandardPasswordEncoder(String algorithm, CharSequence secret) {  
  20.         this.digester = new Digester(algorithm, DEFAULT_ITERATIONS);  
  21.         this.secret = Utf8.encode(secret);  
  22.         this.saltGenerator = KeyGenerators.secureRandom();  
  23.     }  
/**
     * Constructs a standard password encoder with no additional secret value.
     */
public StandardPasswordEncoder() {
        this("");
    }

    /**
     * Constructs a standard password encoder with a secret value which is also included in the
     * password hash.
     *
     * @param secret the secret key used in the encoding process (should not be shared)
     */
    public StandardPasswordEncoder(CharSequence secret) {
        this("SHA-256", secret);
    }

    // internal helpers
    private StandardPasswordEncoder(String algorithm, CharSequence secret) {
        this.digester = new Digester(algorithm, DEFAULT_ITERATIONS);
        this.secret = Utf8.encode(secret);
        this.saltGenerator = KeyGenerators.secureRandom();
    }