'I don't know how to extract the id of principal
I have this code ant I want to extract the UUID of principal and give it as a parameter in the saveProperty function. But I don't know how. Can someone help me?
public String saveProperty(@ModelAttribute("property") Property property, Principal principal) {
//UUID userUUID= principal.getId();
propertyService.saveProperty(property, userUUID);
//propertyService.saveProperty(property, UUID.randomUUID());
return "redirect:/properties";
}
Solution 1:[1]
You would implement the UserDetailsService interface and the UserDetails interface in another class as in this reference, you can get your model containing id in your controller class like this:
public String saveProperty(@ModelAttribute("property") Property property,
Authentication authentication) {
CustomUser customUser = (CustomUser)authentication.getPrincipal();
UUID userUUID = customUser.getId();
propertyService.saveProperty(property, userUUID);
return "redirect:/properties";
}
OR
public String saveProperty(@ModelAttribute("property") Property property,
@AuthenticationPrincipal CustomUser customUser) {
UUID userUUID = customUser.getId();
propertyService.saveProperty(property, userUUID);
return "redirect:/properties";
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | İsmail Y. |
