小编Plu*_*toz的帖子

使用Spring Data REST进行密码编码

我应该如何使用Spring Data REST自动编码实体的subbmitted plain password字段?

我正在使用BCrypt编码器,当客户端通过POST,PUT和PATCH发送请求时,我想自动编码请求的密码字段.

@Entity
public class User {
  @NotNull
  private String username;
  @NotNull
  private String passwordHash;
  ...
  getters/setters/etc
  ...
}
Run Code Online (Sandbox Code Playgroud)

首先我尝试使用@HandleBeforeCreate和@HandleBeforeSave事件侦听器解决但是其中的用户参数已经合并,所以我不能在用户的新密码或旧密码哈希之间有任何区别:

@HandleBeforeSave
protected void onBeforeSave(User user) {
    if (user.getPassword() != null) {
        account.setPassword(passwordEncoder.encode(account.getPassword()));
    }
    super.onBeforeSave(account);
}
Run Code Online (Sandbox Code Playgroud)

这是可能的,在setter方法上使用@Projection和SpEL吗?

spring-data-rest

4
推荐指数
1
解决办法
1745
查看次数

标签 统计

spring-data-rest ×1