Spring Boot Data Rest JPA - Entity custom create (User) -


i trying learn spring. created project spring boot using following tools:

  • spring data jpa
  • spring data rest
  • spring hateoas
  • spring security

i trying create user entity. want user have encrypted password (+ salt).

when post /api/users create new user.

{ "firstname":"john", "lastname":"doe", "email":"johndoe@example.com", "password":"12345678" } 

but have 2 problems:

  • the password saved in clear-text
  • the salt null
+----+---------------------+-----------+----------+----------+------+ | id |        email        | firstname | lastname | password | salt | +----+---------------------+-----------+----------+----------+------+ |  1 | johndoe@example.com | john      | doe      | 12345678 | null | +----+---------------------+-----------+----------+----------+------+ 

the problem think default constructor used , not other 1 have created. new spring , jpa must missing something. here code.

user.java

@entity @table(name = "users") public class user{      @id     @generatedvalue     private long id;      @column(nullable = false)     public string firstname;      @column(nullable = false)     public string lastname;      @column(nullable = false, unique = true)     public string email;      @jsonignore     @column(nullable = false)     public string password;      @jsonignore     @column     private string salt;      public user() {}      public user(string email, string firstname, string lastname, string password) {         this.email = email;         this.firstname = firstname;         this.lastname = lastname;         this.salt = uuid.randomuuid().tostring();         this.password = new bcryptpasswordencoder().encode(password + this.salt);     }       @jsonignore     public string getsalt() {         return salt;     }      @jsonproperty     public void setsalt(string salt) {         this.salt = salt;     }      public string getemail() {         return email;     }      public void setemail(string email) {         this.email = email;     }      public string getfirstname() {         return firstname;     }      public void setfirstname(string firstname) {         this.firstname = firstname;     }      public long getid() {         return id;     }      public void setid(long id) {         this.id = id;     }      public string getlastname() {         return lastname;     }      public void setlastname(string lastname) {         this.lastname = lastname;     }      @jsonignore     public string getpassword() {         return password;     }      @jsonproperty     public void setpassword(string password) {         this.password = password;     }  } 

userrepository.java

public interface userrepository extends jparepository<user, long> {      public user findbyemail(string email);      public user findbyemailandpassword(string email, string password); } 

application.java

@springbootapplication public class application {       public static void main(string[] args) {         springapplication.run(application .class, args);     }  } 

also if finds did wrong, point me where/how should put user login code (decryption).

thanks.

if want spring use constructor, need to

  • remove no-argument constructor
  • annotate every parameter in other constructor @jsonproperty this
public user(@jsonproperty("email") string email,              @jsonproperty("firstname") string firstname,              @jsonproperty("lastname") string lastname,              @jsonproperty("password") string password) {     this.email = email;     this.firstname = firstname;     this.lastname = lastname;     this.password = new bcryptpasswordencoder().encode(password); } 

you don't need provide salt value bcryptpasswordencoder because salts passwords itself.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -