我正在使用身份验证功能,并且在持久保存实体类中的数据时遇到了问题。我可以从数据传输对象访问password_hash和用户名,但无法从数据库的User类中查看电子邮件,firstName,lastName或phoneNumber。下图是我注册新用户时发生的示例。您可以看到有4列没有数据。
当我添加newUser
时,我使用看起来像这样的processRegistrationForm方法...
@PostMapping("/register")
public String processRegistrationForm(@ModelAttribute @Valid RegisterFormDTO registerFormDTO,
Errors errors, HttpServletRequest request,
Model model) {
// a few conditionals that I removed for brevity
User newUser = new User(registerFormDTO.getUsername(), registerFormDTO.getPassword(), registerFormDTO.getFirstName(), registerFormDTO.getLastName(), registerFormDTO.getEmail(), registerFormDTO.getPhoneNumber());
userRepository.save(newUser);
setUserInSession(request.getSession(), newUser);
return "redirect:";
}
registerFromDTO
public class RegisterFormDTO extends LoginFormDTO{
private String verifyPassword;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
//getters and setters.
LoginFormDTO
public class LoginFormDTO {
@NotNull
@NotBlank
@Size(min = 3, max = 20, message = "Invalid username. Must be between 3 and 30 characters.")
private String username;
@NotNull
@NotBlank
@Size(min = 5, max = 20, message = "Invalid password. Must be between 5 and 30 characters.")
private String password;
//getters and setters
用户类。
@Entity
public class User extends AbstractEntity {
@NotBlank
private String username;
@NotBlank
private String pwHash;
//@Column(name = "first_name")
private String firstName;
//@Column(name = "last_name")
private String lastName;
//@Column(name = "email")
private String email;
//@Column(name = "phone_number")
private String phoneNumber;
public User() {}
private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
public User(String username, String password, String firstName, String lastName, String email, String phoneNumber){
this.username =username;
this.pwHash = encoder.encode(password);
this.firstName =firstName;
this.lastName = lastName;
this.email = email;
this.phoneNumber = phoneNumber;
}
public boolean isMatchingPassword(String password) {
return encoder.matches(password, pwHash);
}
//getters and setters
最后但并非最不重要的是我的注册表 View
<form method="post">
<div class="form-group">
<label>Username
<input class="form-control" th:field="${registerFormDTO.username}" />
</label>
<p class="error" th:errors="${registerFormDTO.username}"></p>
</div>
<div class="form-group">
<label>Password
<input class="form-control" th:field="${registerFormDTO.password}" type="password" />
</label>
<p class="error" th:errors="${registerFormDTO.password}"></p>
</div>
<div class="form-group">
<label>Verify Password
<input class="form-control" th:field="${registerFormDTO.verifyPassword}" type="password" />
</label>
</div>
<div class="form-group">
<label>First Name
<input class="form-control" th:field="${registerFormDTO.firstName}" type="firstName" />
</label>
</div>
<div class="form-group">
<label>Last Name
<input class="form-control" th:field="${registerFormDTO.lastName}" type="lastName" />
</label>
</div>
<input type="submit" class="btn btn-primary" value="Register" />
</form>
最佳答案
您可以使用MapStruct从DTO映射到模型。
例如:
@Mapper
public interface EmployeeMapper {
@Mappings({
@Mapping(target="employeeId", source="entity.id"),
@Mapping(target="employeeName", source="entity.name")
})
EmployeeDTO employeeToEmployeeDTO(Employee entity);
@Mappings({
@Mapping(target="id", source="dto.employeeId"),
@Mapping(target="name", source="dto.employeeName")
})
Employee employeeDTOtoEmployee(EmployeeDTO dto);
}
“Quick Guide to MapStruct”具有更多信息。