Answer the question
In order to leave comments, you need to log in
How to implement a user data change form in spring boot + spring security?
Good afternoon.
I deal with spring, I make a simple CRUD application. I did authorization and registration, during registration I check the password for the number of characters and complexity.
I decided to add a Personal Account, where the user can change his mail, name and (if necessary, password) himself.
there are no problems with changing the password, they can’t figure out how to change the mail and name without a password, because I have to fill in the password fields and confirm the password, and if I just change the name and mail, the validator swears that the password cannot be empty.
User class:
public class User{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "username")
@NotBlank(message = "не может быть пустым")
@Size(min = 2, message = "длина имени должна быть от 2 до 50 символов")
@Size(max = 50,message = "длина имени должна быть от 2 до 50 символов")
private String username;
@Column(name = "password")
@NotBlank(message = "не может быть пустым")
@PaswordCheckStrong(message = "длина пароля от 8 до 128 латинских символов, должен содержать спец символы #[email protected]$%^&*- и одну большую и маленькую букву")
private String password;
@Transient
private String passwordConfirm;
@Column(name = "email",unique = true)
@Email(message = "формат email не верный")
@NotBlank(message = "email не может быть пустым")
private String email;
//def constructor + getter + setter
}
model.addAttribute("currentUser", (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
String username = user.getUsername();
String email = user.getEmail();
model.addAttribute("username", username);
model.addAttribute("email", email);
Answer the question
In order to leave comments, you need to log in
Blunted, removed the validation in the signature of the controller method, and changed the fields I needed through a temporary user:
public String saveUser(@ModelAttribute("currentUser") User user,Model model, Principal principal) {
User tempUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
tempUser.setEmail(user.getEmail());
tempUser.setUsername(user.getUsername());
userService.save(tempUser);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question