How should I encode automatically the subbmitted plain password field of my entity with Spring Data REST?
I'm using BCrypt encoder and I want to automatically encode the request's password field, when the client send it via POST, PUT and PATCH.
@Entity
public class User {
@NotNull
private String username;
@NotNull
private String passwordHash;
...
getters/setters/etc
...
}
First I tried to solve with @HandleBeforeCreate and @HandleBeforeSave event listeners but the User in it's argument is already merged, so I can't make any difference between the User's new password, or the old passwordHash:
@HandleBeforeSave
protected void onBeforeSave(User user) {
if (user.getPassword() != null) {
account.setPassword(passwordEncoder.encode(account.getPassword()));
}
super.onBeforeSave(account);
}
Is that possible, to use @Projection and SpEL on a setter method?