Skip to content

getRemoteUser() returns name of object implementing AuthenticatedPrincipal #9102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -106,6 +107,9 @@ public String getRemoteUser() {
if (auth.getPrincipal() instanceof UserDetails) {
return ((UserDetails) auth.getPrincipal()).getUsername();
}
if (auth.getPrincipal() instanceof AuthenticatedPrincipal) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking that this is too narrow of a change to resolve #3357.

I wonder if this should instead do

if (auth instanceof AbstractAuthenticationToken) {
    return auth.getName();
}

since AbstractAuthenticationToken already has this same logic for AuthenticatedPrincipal.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.AuthenticatedPrincipal;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
* Tests {@link SecurityContextHolderAwareRequestWrapper}.
Expand Down Expand Up @@ -130,4 +135,18 @@ public void testRolePrefixNotAppliedIfRoleStartsWith() {
assertThat(wrapper.isUserInRole("ROLE_FOOBAR")).isTrue();
}

@Test
public void testGetRemoteUserStringWithAuthenticatedPrinciple() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you please change Principle to Principal?

String username = "authPrincipleUsername";
AuthenticatedPrincipal principal = mock(AuthenticatedPrincipal.class);
given(principal.getName()).willReturn(username);
Authentication auth = new TestingAuthenticationToken(principal, "user");
SecurityContextHolder.getContext().setAuthentication(auth);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/");
SecurityContextHolderAwareRequestWrapper wrapper = new SecurityContextHolderAwareRequestWrapper(request, "");
assertThat(wrapper.getRemoteUser()).isEqualTo(username);
verify(principal, times(1)).getName();
}

}