Class AuthenticationPrincipalArgumentResolver

java.lang.Object
org.springframework.security.messaging.handler.invocation.reactive.AuthenticationPrincipalArgumentResolver
All Implemented Interfaces:
org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver

public class AuthenticationPrincipalArgumentResolver extends Object implements org.springframework.messaging.handler.invocation.reactive.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 ReactiveSecurityContextHolder. 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:
5.2
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    reactor.core.publisher.Mono<Object>
    resolveArgument(org.springframework.core.MethodParameter parameter, org.springframework.messaging.Message<?> message)
     
    void
    setAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry adapterRegistry)
    Sets the ReactiveAdapterRegistry to be used.
    void
    setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
    Sets the BeanResolver to be used on the expressions
    void
    setTemplateDefaults(org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults templateDefaults)
    Configure AuthenticationPrincipal template resolution
    boolean
    supportsParameter(org.springframework.core.MethodParameter parameter)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AuthenticationPrincipalArgumentResolver

      public AuthenticationPrincipalArgumentResolver()
  • Method Details

    • setBeanResolver

      public void setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
      Sets the BeanResolver to be used on the expressions
      Parameters:
      beanResolver - the BeanResolver to use
    • setAdapterRegistry

      public void setAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry adapterRegistry)
      Sets the ReactiveAdapterRegistry to be used.
      Parameters:
      adapterRegistry - the ReactiveAdapterRegistry to use. Cannot be null. Default is ReactiveAdapterRegistry.getSharedInstance()
    • supportsParameter

      public boolean supportsParameter(org.springframework.core.MethodParameter parameter)
      Specified by:
      supportsParameter in interface org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver
    • resolveArgument

      public reactor.core.publisher.Mono<Object> resolveArgument(org.springframework.core.MethodParameter parameter, org.springframework.messaging.Message<?> message)
      Specified by:
      resolveArgument in interface org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver
    • setTemplateDefaults

      public void setTemplateDefaults(org.springframework.security.core.annotation.AnnotationTemplateExpressionDefaults templateDefaults)
      Configure AuthenticationPrincipal template resolution

      By default, this value is null, which indicates that templates should not be resolved.

      Parameters:
      templateDefaults - - whether to resolve AuthenticationPrincipal templates parameters
      Since:
      6.4