Skip to content

Commit c6da7b2

Browse files
committed
Polish gh-7840
1 parent 65b5d46 commit c6da7b2

File tree

1 file changed

+17
-50
lines changed

1 file changed

+17
-50
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServletOAuth2AuthorizedClientExchangeFilterFunction.java

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
import org.springframework.http.HttpHeaders;
1919
import org.springframework.http.HttpStatus;
20+
import org.springframework.security.authentication.AbstractAuthenticationToken;
2021
import org.springframework.security.authentication.AnonymousAuthenticationToken;
2122
import org.springframework.security.core.Authentication;
22-
import org.springframework.security.core.GrantedAuthority;
2323
import org.springframework.security.core.authority.AuthorityUtils;
2424
import org.springframework.security.core.context.SecurityContextHolder;
2525
import org.springframework.security.oauth2.client.ClientAuthorizationException;
@@ -61,7 +61,6 @@
6161
import javax.servlet.http.HttpServletRequest;
6262
import javax.servlet.http.HttpServletResponse;
6363
import java.time.Duration;
64-
import java.util.Collection;
6564
import java.util.Collections;
6665
import java.util.HashMap;
6766
import java.util.Map;
@@ -559,7 +558,7 @@ private Mono<OAuth2AuthorizedClient> reauthorizeClient(OAuth2AuthorizedClient au
559558
Map<String, Object> attrs = request.attributes();
560559
Authentication authentication = getAuthentication(attrs);
561560
if (authentication == null) {
562-
authentication = new PrincipalNameAuthentication(authorizedClient.getPrincipalName());
561+
authentication = createAuthentication(authorizedClient.getPrincipalName());
563562
}
564563
HttpServletRequest servletRequest = getRequest(attrs);
565564
HttpServletResponse servletResponse = getResponse(attrs);
@@ -609,52 +608,20 @@ static HttpServletResponse getResponse(Map<String, Object> attrs) {
609608
return (HttpServletResponse) attrs.get(HTTP_SERVLET_RESPONSE_ATTR_NAME);
610609
}
611610

612-
private static class PrincipalNameAuthentication implements Authentication {
613-
private final String principalName;
611+
private static Authentication createAuthentication(final String principalName) {
612+
Assert.hasText(principalName, "principalName cannot be empty");
614613

615-
private PrincipalNameAuthentication(String principalName) {
616-
Assert.hasText(principalName, "principalName cannot be empty");
617-
this.principalName = principalName;
618-
}
619-
620-
@Override
621-
public Collection<? extends GrantedAuthority> getAuthorities() {
622-
throw unsupported();
623-
}
624-
625-
@Override
626-
public Object getCredentials() {
627-
throw unsupported();
628-
}
629-
630-
@Override
631-
public Object getDetails() {
632-
throw unsupported();
633-
}
634-
635-
@Override
636-
public Object getPrincipal() {
637-
return getName();
638-
}
639-
640-
@Override
641-
public boolean isAuthenticated() {
642-
throw unsupported();
643-
}
644-
645-
@Override
646-
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
647-
throw unsupported();
648-
}
649-
650-
@Override
651-
public String getName() {
652-
return this.principalName;
653-
}
614+
return new AbstractAuthenticationToken(null) {
615+
@Override
616+
public Object getCredentials() {
617+
return "";
618+
}
654619

655-
private UnsupportedOperationException unsupported() {
656-
return new UnsupportedOperationException("Not Supported");
657-
}
620+
@Override
621+
public Object getPrincipal() {
622+
return principalName;
623+
}
624+
};
658625
}
659626

660627
/**
@@ -711,7 +678,7 @@ private Mono<Void> handleResponse(ClientRequest request, ClientResponse response
711678
ClientAuthorizationException authorizationException = new ClientAuthorizationException(
712679
oauth2Error, authorizedClient.getClientRegistration().getRegistrationId());
713680

714-
Authentication principal = new PrincipalNameAuthentication(authorizedClient.getPrincipalName());
681+
Authentication principal = createAuthentication(authorizedClient.getPrincipalName());
715682
HttpServletRequest servletRequest = getRequest(attrs);
716683
HttpServletResponse servletResponse = getResponse(attrs);
717684

@@ -779,7 +746,7 @@ private Mono<Void> handleWebClientResponseException(ClientRequest request, WebCl
779746
ClientAuthorizationException authorizationException = new ClientAuthorizationException(
780747
oauth2Error, authorizedClient.getClientRegistration().getRegistrationId(), exception);
781748

782-
Authentication principal = new PrincipalNameAuthentication(authorizedClient.getPrincipalName());
749+
Authentication principal = createAuthentication(authorizedClient.getPrincipalName());
783750
HttpServletRequest servletRequest = getRequest(attrs);
784751
HttpServletResponse servletResponse = getResponse(attrs);
785752

@@ -804,7 +771,7 @@ private Mono<Void> handleAuthorizationException(ClientRequest request, OAuth2Aut
804771
return Mono.empty();
805772
}
806773

807-
Authentication principal = new PrincipalNameAuthentication(authorizedClient.getPrincipalName());
774+
Authentication principal = createAuthentication(authorizedClient.getPrincipalName());
808775
HttpServletRequest servletRequest = getRequest(attrs);
809776
HttpServletResponse servletResponse = getResponse(attrs);
810777

0 commit comments

Comments
 (0)