-1

I'm using Spring Boot and Spring Security.

I would like to get current authenticated user data inside my userController (not only the name, username, password and authorities)

I found on differents links I can use Principal in my requestMapping method. But how can I data on this principal please ?

If I run in debug mode I can see all data I need inside the Principal variable.

@GetMapping("/users")
public ResponseEntity<?> getAllUsers(Principal principal) {
    
    return this.userService.getAllUsers();
}

enter image description here

cigien
  • 57,834
  • 11
  • 73
  • 112

1 Answers1

0

you hard cast it to a user details object:

final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
Toerktumlare
  • 12,548
  • 3
  • 35
  • 54