【发布时间】:2018-09-03 07:32:34
【问题描述】:
按照here 的建议,我尝试在我的AppUser 的password 和passwordSalt 字段上使用@JsonIgnore 和@JsonProperty(来自import com.fasterxml.jackson.annotation.*;)。
@Entity
@Table(name = "app_user")
public class AppUser extends AbstractTimestampEntity {
// ..
@JsonIgnore
public String getPassword() {
return password;
}
@JsonProperty
public void setPassword(String password) {
this.password = password;
}
@JsonIgnore
public String getPasswordSalt() {
return passwordSalt;
}
@JsonProperty
public void setPasswordSalt(String passwordSalt) {
this.passwordSalt = passwordSalt;
}
}
但是,由于某种原因,这两个字段始终为 null。当我尝试“注册”一个用户时,我发送的 JSON 没有完全反序列化。 password 设置为 null:
我想要实现的是能够接收用户的密码以便存储它,但同时确保存储的(加密的)密码没有被序列化并发送回客户端。
我在这里做错了什么?
【问题讨论】:
-
是否填充了其他字符串字段?你能显示被发送到反序列化的 json 吗?
-
@DanielM 好吧,此时我只使用 Java 对象。我正在使用 Springs
TestRestTemplate来序列化 Java 对象并将其发送到服务器。密码肯定是被设置的。但到目前为止,我无法避免将(加密的)密码发送回客户端。 -
您正在使用什么序列化程序对测试中的对象进行序列化?如果是这个,那么您将不会对密码进行序列化 - 然后它不会被发送到服务器,因此不会被反序列化。
-
@DanielM 我不确定我是否理解。我的目标是允许客户发送密码。这意味着如果客户端发送一个 json
{"password": "mypassword"},则相应的 POJO 应该将属性password设置为mypassword。另一方面,如果我从数据库中获取实体并将其发送回客户端,我希望忽略此字段。它必须保持null。顺便说一句,我在这里使用 Spring Boot。 -
如果您使用的是同一个类(在
getPassword()上有@JsonIgnore),您正在使用字段密码序列化 POJO - 那么它不会被序列化为 json 并发送到服务器。所以服务器不会在 json 中获取密码字段并将其设置为 null
标签: spring-boot jackson