I've got an add user option in my app. I'd like to store the user pass in hash format in the database. Th password is stored in plain text format in the sample codes included with the framework. After some searching i've found out that there's a Crypto.encryptAES() function implemented in play2 that can be used to secure passwords.
My question is what's the best place to use it? And how to use it to create the most maintainable code?
I found a much simpler solution on the web at this adress : http://rny.io/playframework/bcrypt/2013/10/22/better-password-hashing-in-play-2.html
First download the jbcrypt-xxx.jar at this adress.
In the libraryDependencies in build.sbt, add :
This is the function to create a new user (located in the model class User) :
And, still in the User class, the function to authenticate :
And it work !
Personally I would do it in the
User
model. I have getters for my fields, so insetPassword
method:The
Hashhelper
is just an singleton class for multi purposes hashing stuff.And in Hashelper I use BCrypt, just add following to Build.scala
And the crypting looks like:
And decrypting looks like:
I love to keep my controllers as simple as possible as I see my controllers just as traffic controllers between the user action and the business model (inside my models!) stuff.