Class AuthenticationPrincipalArgumentResolver
java.lang.Object
org.springframework.security.messaging.context.AuthenticationPrincipalArgumentResolver
- All Implemented Interfaces:
- org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver
public final class AuthenticationPrincipalArgumentResolver
extends Object
implements org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver
Allows resolving the 
Authentication.getPrincipal() using the
 AuthenticationPrincipal annotation. For example, the following
 Controller:
 
 @Controller
 public class MyController {
     @MessageMapping("/im")
     public void im(@AuthenticationPrincipal CustomUser customUser) {
         // do something with CustomUser
     }
 }
 
 
 Will resolve the CustomUser argument using Authentication.getPrincipal() from
 the SecurityContextHolder. If the Authentication or
 Authentication.getPrincipal() is null, it will return null. If the types do not
 match, null will be returned unless
 AuthenticationPrincipal.errorOnInvalidType() is true in which case a
 ClassCastException will be thrown.
 
Alternatively, users can create a custom meta annotation as shown below:
 @Target({ ElementType.PARAMETER })
 @Retention(RetentionPolicy.RUNTIME)
 @AuthenticationPrincipal
 public @interface CurrentUser {
 }
 
 The custom annotation can then be used instead. For example:
 @Controller
 public class MyController {
     @MessageMapping("/im")
     public void im(@CurrentUser CustomUser customUser) {
         // do something with CustomUser
     }
 }
 - Since:
- 4.0
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionresolveArgument(org.springframework.core.MethodParameter parameter, org.springframework.messaging.Message<?> message) voidsetSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) Sets theSecurityContextHolderStrategyto use.voidsetTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) Configure AuthenticationPrincipal template resolutionbooleansupportsParameter(org.springframework.core.MethodParameter parameter) 
- 
Constructor Details- 
AuthenticationPrincipalArgumentResolverpublic AuthenticationPrincipalArgumentResolver()
 
- 
- 
Method Details- 
supportsParameterpublic boolean supportsParameter(org.springframework.core.MethodParameter parameter) - Specified by:
- supportsParameterin interface- org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver
 
- 
resolveArgumentpublic Object resolveArgument(org.springframework.core.MethodParameter parameter, org.springframework.messaging.Message<?> message) - Specified by:
- resolveArgumentin interface- org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver
 
- 
setSecurityContextHolderStrategypublic void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) Sets theSecurityContextHolderStrategyto use. The default action is to use theSecurityContextHolderStrategystored inSecurityContextHolder.- Since:
- 5.8
 
- 
setTemplateDefaultsConfigure AuthenticationPrincipal template resolutionBy default, this value is null, which indicates that templates should not be resolved.- Parameters:
- templateDefaults- - whether to resolve AuthenticationPrincipal templates parameters
- Since:
- 6.4
 
 
-