2013-03-02 44 views
1

我有一個PHP應用程序正在轉換爲Grails。 PHP應用程序使用salt機制來編碼所有用戶的密碼。如何在Grails項目中使用PHP生成的鹽

將鹽和鹽漬密碼移動到Grails中的(自定義)用戶數據庫中時,我無法在我的Grails應用程序中登錄。

當然,我使用的是Spring Security Core plugin,我已經添加了鹽爲this tutorial指定的用戶域類,我發現在這裏:Grails with Spring Security Plugin and Salted Passwords

通過教程運行後,我能夠添加一個用戶,併成功地與該用戶登錄:

[BootStrap.groovy]: 
new User(username:"user", email:"[email protected]", password:"password", enabled:true).save(flush: true) 

(你可能也注意到另外的電子郵件,其中我添加使用this tutorial

但我不能使用從PHP項目轉移過來的任何用戶登錄。如果它的任何幫助,這裏是如何的密碼被編碼:

$password = "password"; 
$salt = bin2hex(openssl_random_pseudo_bytes(32)); 
$passwordSalted = hash("sha256", $salt . $password); 

回答

1

貌似伯特他的建議在這裏把它釘:http://grails.1312388.n4.nabble.com/Spring-Security-Core-plugin-and-multiple-salt-sources-tc3638236.html#a3646256

基本上,我已經從我上面提到的教程提供我自己的鹽,我只需要使用自定義密碼編碼器將它與密碼結合起來。

public class CustomPasswordEncoder extends MessageDigestPasswordEncoder { 

    public CustomPasswordEncoder() { 
     super("SHA-256"); 
    } 

    @Override 
    protected String mergePasswordAndSalt(String password, Object salt, boolean strict) { 
     if (password == null) { 
      password = ""; 
     } 

     if (salt == null || "".equals(salt)) { 
      return password; 
     } 

     return salt + password; 
    } 
} 
相關問題