gpt4 book ai didi

java - 使用 Spring 在 Hibernate 中存储计算数据 (passwordHash)

转载 作者:行者123 更新时间:2023-12-01 10:06:27 25 4
gpt4 key购买 nike

我有一个具有以下构造函数的对象:

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 的方法。显然,我只希望它以散列格式存储。问题是,给定以下 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
}

用户.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();
}
}

最佳答案

问题是,默认情况下对实体转换器的请求(我想您使用 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());
}

我认为上面的代码应该可以工作。

关于java - 使用 Spring 在 Hibernate 中存储计算数据 (passwordHash),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36425758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com