Osobiście zrobiłbym to w modelu User
. Mam pobierające dla moich polach, więc w setPassword
metody:
this.password = HashHelper.createPassword(password);
Hashhelper
jest tylko pojedyncza klasa dla wielu celów `mieszające rzeczy.
I Hashelper używam BCrypt, po prostu dodaj do Build.scala
org.mindrot" % "jbcrypt" % "0.3m
A CryptoAPI wygląda następująco:
/**
* Create an encrypted password from a clear string.
*
* @param clearString
* the clear string
* @return an encrypted password of the clear string
* @throws AppException
* APP Exception, from NoSuchAlgorithmException
*/
public static String createPassword(String clearString) throws AppException {
if (clearString == null) {
throw new AppException("empty.password");
}
return BCrypt.hashpw(clearString, BCrypt.gensalt());
}
odszyfrowania wygląda następująco:
/**
* Method to check if entered user password is the same as the one that is
* stored (encrypted) in the database.
*
* @param candidate
* the clear text
* @param encryptedPassword
* the encrypted password string to check.
* @return true if the candidate matches, false otherwise.
*/
public static boolean checkPassword(String candidate, String encryptedPassword) {
if (candidate == null) {
return false;
}
if (encryptedPassword == null) {
return false;
}
return BCrypt.checkpw(candidate, encryptedPassword);
}
Uwielbiam przechowywać moje kontrolery tak proste, jak to tylko możliwe, ponieważ widzę moich kontrolerów, tak jak kontrolerów ruchu między akcja użytkownika i model biznesowy (w moich modelach!).
Używam [jBCrypt] (http://mvnrepository.com/artifact/org.mindrot/jbcrypt/0.3m), który jest bardzo łatwy do zintegrowania z aplikacją. – maba
dziękuję, ale naprawdę pytam, gdzie można nazwać funkcję haszowania. w modelu lub w kontrolerze? i jak? – zeal
moim głównym problemem jest to, że nie mogę używać '@ PreUpdate' i' @ Prepersist' w ebean. – zeal