Class CurrentSecurityContextArgumentResolver
- java.lang.Object
-
- org.springframework.security.messaging.handler.invocation.reactive.CurrentSecurityContextArgumentResolver
-
- All Implemented Interfaces:
org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver
public class CurrentSecurityContextArgumentResolver extends java.lang.Object implements org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver
Allows resolving theAuthentication.getPrincipal()
using theCurrentSecurityContext
annotation. For example, the followingController
:@Controller public class MyController { @MessageMapping("/im") public void im(@CurrentSecurityContext SecurityContext context) { // do something with context } }
Will resolve the SecurityContext argument using the
ReactiveSecurityContextHolder
. If theSecurityContext
is empty, it will return null. If the types do not match, null will be returned unlessCurrentSecurityContext.errorOnInvalidType()
is true in which case aClassCastException
will be thrown.Alternatively, users can create a custom meta annotation as shown below:
@Target({ ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) @CurrentSecurityContext(expression = "authentication?.principal") 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 CurrentSecurityContextArgumentResolver()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description reactor.core.publisher.Mono<java.lang.Object>
resolveArgument(org.springframework.core.MethodParameter parameter, org.springframework.messaging.Message<?> message)
void
setAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry adapterRegistry)
Sets theReactiveAdapterRegistry
to be used.void
setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
Sets theBeanResolver
to be used on the expressionsboolean
supportsParameter(org.springframework.core.MethodParameter parameter)
-
-
-
Method Detail
-
setBeanResolver
public void setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
Sets theBeanResolver
to be used on the expressions- Parameters:
beanResolver
- theBeanResolver
to use
-
setAdapterRegistry
public void setAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry adapterRegistry)
Sets theReactiveAdapterRegistry
to be used.- Parameters:
adapterRegistry
- theReactiveAdapterRegistry
to use. Cannot be null. Default isReactiveAdapterRegistry.getSharedInstance()
-
supportsParameter
public boolean supportsParameter(org.springframework.core.MethodParameter parameter)
- Specified by:
supportsParameter
in interfaceorg.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver
-
resolveArgument
public reactor.core.publisher.Mono<java.lang.Object> resolveArgument(org.springframework.core.MethodParameter parameter, org.springframework.messaging.Message<?> message)
- Specified by:
resolveArgument
in interfaceorg.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver
-
-