Class AuthenticationPrincipalArgumentResolver
- java.lang.Object
-
- org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver
-
- All Implemented Interfaces:
org.springframework.web.method.support.HandlerMethodArgumentResolver
public final class AuthenticationPrincipalArgumentResolver extends java.lang.Object implements org.springframework.web.method.support.HandlerMethodArgumentResolver
Allows resolving theAuthentication.getPrincipal()
using theAuthenticationPrincipal
annotation. For example, the followingController
:@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 theSecurityContextHolder
. If theAuthentication
orAuthentication.getPrincipal()
is null, it will return null. If the types do not match, null will be returned unlessAuthenticationPrincipal.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) @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:
- 4.0
-
-
Constructor Summary
Constructors Constructor Description AuthenticationPrincipalArgumentResolver()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 theBeanResolver
to be used on the expressionsvoid
setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy)
Sets theSecurityContextHolderStrategy
to use.boolean
supportsParameter(org.springframework.core.MethodParameter parameter)
-
-
-
Method Detail
-
supportsParameter
public boolean supportsParameter(org.springframework.core.MethodParameter parameter)
- Specified by:
supportsParameter
in interfaceorg.springframework.web.method.support.HandlerMethodArgumentResolver
-
resolveArgument
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)
- Specified by:
resolveArgument
in interfaceorg.springframework.web.method.support.HandlerMethodArgumentResolver
-
setBeanResolver
public void setBeanResolver(org.springframework.expression.BeanResolver beanResolver)
Sets theBeanResolver
to be used on the expressions- Parameters:
beanResolver
- theBeanResolver
to use
-
setSecurityContextHolderStrategy
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy)
Sets theSecurityContextHolderStrategy
to use. The default action is to use theSecurityContextHolderStrategy
stored inSecurityContextHolder
.- Since:
- 5.8
-
-