public class CurrentSecurityContextArgumentResolver
extends java.lang.Object
implements org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver
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
}
}
| Constructor and Description |
|---|
CurrentSecurityContextArgumentResolver() |
| Modifier and Type | Method and 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 the
ReactiveAdapterRegistry to be used. |
void |
setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
Sets the
BeanResolver to be used on the expressions |
boolean |
supportsParameter(org.springframework.core.MethodParameter parameter) |
public CurrentSecurityContextArgumentResolver()
public void setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
BeanResolver to be used on the expressionsbeanResolver - the BeanResolver to usepublic void setAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry adapterRegistry)
ReactiveAdapterRegistry to be used.adapterRegistry - the ReactiveAdapterRegistry to use. Cannot be null.
Default is ReactiveAdapterRegistry.getSharedInstance()public boolean supportsParameter(org.springframework.core.MethodParameter parameter)
supportsParameter in interface org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolverpublic reactor.core.publisher.Mono<java.lang.Object> resolveArgument(org.springframework.core.MethodParameter parameter,
org.springframework.messaging.Message<?> message)
resolveArgument in interface org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver