Class AuthenticationPrincipalArgumentResolver

  • All Implemented Interfaces:

    public final class AuthenticationPrincipalArgumentResolver
    extends java.lang.Object
    Allows resolving the Authentication.getPrincipal() using the AuthenticationPrincipal annotation. For example, the following Controller:
     public class MyController {
         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 })
     public @interface CurrentUser {

    The custom annotation can then be used instead. For example:

     public class MyController {
         public void im(@CurrentUser CustomUser customUser) {
             // do something with CustomUser
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object resolveArgument​(org.springframework.core.MethodParameter parameter, mavContainer, org.springframework.web.context.request.NativeWebRequest webRequest, binderFactory)  
      void setBeanResolver​(org.springframework.expression.BeanResolver beanResolver)
      Sets the BeanResolver to be used on the expressions
      boolean supportsParameter​(org.springframework.core.MethodParameter parameter)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AuthenticationPrincipalArgumentResolver

        public AuthenticationPrincipalArgumentResolver()
    • Method Detail

      • supportsParameter

        public boolean supportsParameter​(org.springframework.core.MethodParameter parameter)
        Specified by:
        supportsParameter in interface
      • resolveArgument

        public java.lang.Object resolveArgument​(org.springframework.core.MethodParameter parameter,
                                                org.springframework.web.context.request.NativeWebRequest webRequest,
        Specified by:
        resolveArgument in interface
      • setBeanResolver

        public void setBeanResolver​(org.springframework.expression.BeanResolver beanResolver)
        Sets the BeanResolver to be used on the expressions
        beanResolver - the BeanResolver to use