get logged-in user in Spring Security

To get the logged-in user in Spring Security, you can follow these steps:

  1. Inject the Authentication object into your controller or service class.
  2. Check if the Authentication object is not null and is authenticated.
  3. If the Authentication object is authenticated, you can get the logged-in user's details using the getPrincipal() method.
  4. Cast the Principal object to the appropriate user details class (e.g., UserDetails) to access the user's information.
  5. Retrieve the user's details, such as the username, email, or any other relevant information.

Please note that the exact implementation details may vary depending on your specific use case and configuration. It's always a good practice to consult the Spring Security documentation or seek further assistance if needed.