@AuthenticationPrincipal
Reference: Spring Docs - AuthenticationPrincipal
An annotation that injects a user object implementing the
UserDetailsinterface to retrieve the currently logged-in userWhat is
UserDetails?An interface that holds user information (username, password, etc.)
Implement this interface to create a class used for login
Stored by being encapsulated in
AuthenticationIn other words, UserDetails implementation information is taken by the Authentication object stored in the Spring Security Context, and user information is contained in the Authentication object
β Using
@AuthenticationPrincipal, you can extract user information from theAuthenticationobject and have it injected
Not only
UserDetailsbut other classes containing user information can also be injected, because it extracts user information from Spring Security'sPrincipalobjectIf the class containing user information implements
Principal, that class can be used for injectionUses Authentication.getPrincipal() as the method argument!
What is
java.lang.Object getPrincipal()?Retrieves the user information of the principal being authenticated
Returns: the Principal being authenticated or the authenticated principal after authentication.
Last updated