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 Object
implements org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver
Allows resolving the
Authentication.getPrincipal() using the
CurrentSecurityContext annotation. For example, the following
Controller:
@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 the SecurityContext is empty, it will
return null. If the types do not match, null will be returned unless
CurrentSecurityContext.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)
@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 -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<Object>resolveArgument(org.springframework.core.MethodParameter parameter, org.springframework.messaging.Message<?> message) voidsetAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry adapterRegistry) Sets theReactiveAdapterRegistryto be used.voidsetBeanResolver(org.springframework.expression.BeanResolver beanResolver) Sets theBeanResolverto be used on the expressionsbooleansupportsParameter(org.springframework.core.MethodParameter parameter)
-
Constructor Details
-
CurrentSecurityContextArgumentResolver
public CurrentSecurityContextArgumentResolver()
-
-
Method Details
-
setBeanResolver
public void setBeanResolver(org.springframework.expression.BeanResolver beanResolver) Sets theBeanResolverto be used on the expressions- Parameters:
beanResolver- theBeanResolverto use
-
setAdapterRegistry
public void setAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry adapterRegistry) Sets theReactiveAdapterRegistryto be used.- Parameters:
adapterRegistry- theReactiveAdapterRegistryto use. Cannot be null. Default isReactiveAdapterRegistry.getSharedInstance()
-
supportsParameter
public boolean supportsParameter(org.springframework.core.MethodParameter parameter) - Specified by:
supportsParameterin interfaceorg.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:
resolveArgumentin interfaceorg.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver
-