我有一个带有以下构造函数的Object:
public User(String name, String email, String password) {
this.name = name;
this.email = email;
this.creationDate = new Date();
this.passwordHash = hashPassword(password, getCreationDate());
}
hashPassword
是一种创建passwordHash的方法,它将密码字符串和date
作为salt。显然,我只希望它以散列格式存储。问题是,给定以下Controller和请求,返回的值为null:
@RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public
@ResponseBody User createUser(@RequestBody User user) {
return repository.save(user);}
JSON:
{
"name": "My new user",
"email": "My email",
"password": "my password"
}
回应:
{
"id": 1,
"name": "My new user",
"email": "My email",
"passwordHash": null,
"creationDate": null
}
User.java:
@Entity public class User {
@Column private Long id;
@Column(nullable = false) private String name;
@Column(nullable = false, unique = true) private String email;
@Column(nullable = false) private String passwordHash;
@CreationTimestamp private Date creationDate;
// Constructors
public User() {
}
public User(String name, String email, String password) {
this.name = name;
this.email = email;
this.passwordHash = hashPassword(password, getCreationDate());
}
// Additional methods
public String hashPassword(String password, Date date) {
MessageDigest sha512 = null;
try {
sha512 = getInstance("SHA-512");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String passwordProposal = password + date
.toString();
byte[] passwordDigest = new byte[0];
try {
passwordDigest = sha512.digest(passwordProposal.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return Hex.encodeHexString(passwordDigest);
}
public boolean comparePasswords(String password, String passwordHash) {
if (hashPassword(password, getCreationDate()).equals(passwordHash)) {
return true;
} else
return false;
}
// Getters and setters
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPasswordHash() {
return passwordHash;
}
public void setPasswordHash(String password) {
this.passwordHash = password;
}
@Generated(value = GenerationTime.INSERT) public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
// toString
@Override public String toString() {
final StringBuilder sb = new StringBuilder("User{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", email='").append(email).append('\'');
sb.append(", passwordHash='").append(passwordHash).append('\'');
sb.append(", creationDate=").append(creationDate);
sb.append('}');
return sb.toString();
}
}
答案 0 :(得分:1)
问题是默认情况下请求实体转换器(我猜你使用Jackson for JSON)不使用参数构造函数,因此不会填充计算字段。
@JsonCreator
public User(@JsonProperty("name") String name,
@JsonProperty("email") String email,
@JsonProperty("password") String password) {
this.name = name;
this.email = email;
this.creationDate = new Date();
this.passwordHash = hashPassword(password, getCreationDate());
}
我认为上面的代码应该可行。