public final class AuthenticationPrincipalArgumentResolver
extends java.lang.Object
implements org.springframework.web.method.support.HandlerMethodArgumentResolver
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 } }
Constructor and Description |
---|
AuthenticationPrincipalArgumentResolver() |
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
resolveArgument(org.springframework.core.MethodParameter parameter,
org.springframework.web.method.support.ModelAndViewContainer mavContainer,
org.springframework.web.context.request.NativeWebRequest webRequest,
org.springframework.web.bind.support.WebDataBinderFactory binderFactory) |
void |
setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
Sets the
BeanResolver to be used on the expressions |
boolean |
supportsParameter(org.springframework.core.MethodParameter parameter) |
public AuthenticationPrincipalArgumentResolver()
public boolean supportsParameter(org.springframework.core.MethodParameter parameter)
supportsParameter
in interface org.springframework.web.method.support.HandlerMethodArgumentResolver
public java.lang.Object resolveArgument(org.springframework.core.MethodParameter parameter, org.springframework.web.method.support.ModelAndViewContainer mavContainer, org.springframework.web.context.request.NativeWebRequest webRequest, org.springframework.web.bind.support.WebDataBinderFactory binderFactory) throws java.lang.Exception
resolveArgument
in interface org.springframework.web.method.support.HandlerMethodArgumentResolver
java.lang.Exception
public void setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
BeanResolver
to be used on the expressionsbeanResolver
- the BeanResolver
to use