@AuthenticationPrincipal is a Spring Security annotation that injects the principal of the current Authentication straight into a Spring MVC controller method parameter. It replaces the manual call to SecurityContextHolder.getContext().getAuthentication().getPrincipal() and gives you a typed object, such as your own UserDetails class, a Jwt, or an OidcUser.
The annotation lives in org.springframework.security.core.annotation. Spring Security registers an argument resolver for it automatically when web security is enabled, so in a Spring Boot 3 application you only need to add the annotation to a handler method parameter.
During authentication, Spring Security builds an Authentication object and stores it in the SecurityContext. Its getPrincipal() method returns the "who": for form login or HTTP Basic this is usually a UserDetails, for a JWT resource server it is a Jwt, and for OAuth 2.0 login it is an OAuth2User or OidcUser.
When a controller method has a parameter annotated with @AuthenticationPrincipal, the AuthenticationPrincipalArgumentResolver does three things:
Authentication from the security context.If the types do not match, the parameter is null by default, not an exception. Set errorOnInvalidType = true to fail fast instead.
The most common use is a user type that carries more than a username. Extend Spring Security's User so you do not have to implement every UserDetails method:
Return that type from your UserDetailsService, then inject it in a controller:
A request for /api/me by the logged-in user returns JSON like this:
The method contains no lookup code, and the parameter type documents what the endpoint needs.
The expression attribute is a SpEL expression evaluated against the principal. It saves you from injecting a whole object when you need one value:
Here email and id resolve through the getters getEmail() and getId(). Use #this when you need the principal itself inside a larger expression.
The type you ask for must match what your authentication mechanism produces.
| Mechanism | Principal type | Example parameter |
|---|---|---|
| Form login, HTTP Basic | UserDetails (or your subclass) | @AuthenticationPrincipal AppUser user |
| JWT resource server | Jwt | @AuthenticationPrincipal Jwt jwt |
| Opaque token resource server | OAuth2AuthenticatedPrincipal | @AuthenticationPrincipal OAuth2AuthenticatedPrincipal p |
| OAuth 2.0 / OIDC login | OAuth2User / OidcUser | @AuthenticationPrincipal OidcUser user |
For a JWT you can read claims directly:
@AuthenticationPrincipal works as a meta-annotation, so you can define a shorter, project-specific one and keep the SpEL in a single place:
Controllers then read me(@CurrentUser AppUser user). If you later change the principal type, you adjust one annotation instead of every method.
AnonymousAuthenticationToken whose principal is the string anonymousUser. A parameter of type AppUser receives null, so guard against that on endpoints that allow anonymous access.SecurityContextHolder.@CurrentSecurityContext is the sibling annotation when you need the Authentication or the whole SecurityContext instead of the principal.@PreAuthorize("hasRole('ADMIN')") over manual role checks in the method body.Either the user is anonymous, or the principal is not an instance of the declared type. Print authentication.getPrincipal().getClass() to see what Spring Security produced, and match your parameter to it.
No. A plain Principal parameter gives you the Authentication as the servlet Principal, which only exposes a name. @AuthenticationPrincipal gives you the typed principal object itself.
Yes. The same annotation works in Spring WebFlux controllers, where a reactive argument resolver reads the context from the reactive security context.
Yes, through SpEL. Spring Security can expose the principal in query expressions when you register the security evaluation context extension, which lets you filter by the current user.
@AuthenticationPrincipal injects the current principal into MVC handler methods with the correct type. Use your own UserDetails subclass, a Jwt, or an OidcUser depending on the authentication mechanism, extract single values with expression, wrap it in @CurrentUser to keep controllers clean, and handle the anonymous case explicitly.