Michael Osipov (Migrated from SEC-3198) said:

The public method getRemoteUser tries to return auth.getPrincipal().toString() but there is no guarantee that the principal which is an arbitrary implementation will acttually return the remote user. The assumption that it does is wrong. Since Authentication implements Principal, it would be the safest bet to return auth.getName() which is by interface guaranteed to be a proper name.

Comment From: tomikmar

I have the similar issue. My application uses two types of login - OAuth2 and LDAP. This is the reason why I need to handle AuthenticatedPrincipal (DefaultOAuth2User) and UserDetails.

Current implementation of SecurityContextHolderAwareRequestWrapper.getRemoteUser() only supports UserDetails. It would be great if this method could also work with AuthenticatedPrincipal returning username instead of the whole object (toString), e.g. (alternative solution to the one above):

@Override
public String getRemoteUser() {
        Authentication auth = getAuthentication();

        if ((auth == null) || (auth.getPrincipal() == null)) {
                return null;
        }

        if (auth.getPrincipal() instanceof UserDetails) {
                return ((UserDetails) auth.getPrincipal()).getUsername();
        }
+       if (auth.getPrincipal() instanceof AuthenticatedPrincipal) {
+               return ((AuthenticatedPrincipal) auth.getPrincipal()).getName();
+       }

        return auth.getPrincipal().toString();
}

Similar approach is used in AbstractAuthenticationToken.getName().