17
17
18
18
import org .springframework .http .HttpHeaders ;
19
19
import org .springframework .http .HttpStatus ;
20
+ import org .springframework .security .authentication .AbstractAuthenticationToken ;
20
21
import org .springframework .security .authentication .AnonymousAuthenticationToken ;
21
22
import org .springframework .security .core .Authentication ;
22
- import org .springframework .security .core .GrantedAuthority ;
23
23
import org .springframework .security .core .authority .AuthorityUtils ;
24
24
import org .springframework .security .core .context .SecurityContextHolder ;
25
25
import org .springframework .security .oauth2 .client .ClientAuthorizationException ;
61
61
import javax .servlet .http .HttpServletRequest ;
62
62
import javax .servlet .http .HttpServletResponse ;
63
63
import java .time .Duration ;
64
- import java .util .Collection ;
65
64
import java .util .Collections ;
66
65
import java .util .HashMap ;
67
66
import java .util .Map ;
@@ -559,7 +558,7 @@ private Mono<OAuth2AuthorizedClient> reauthorizeClient(OAuth2AuthorizedClient au
559
558
Map <String , Object > attrs = request .attributes ();
560
559
Authentication authentication = getAuthentication (attrs );
561
560
if (authentication == null ) {
562
- authentication = new PrincipalNameAuthentication (authorizedClient .getPrincipalName ());
561
+ authentication = createAuthentication (authorizedClient .getPrincipalName ());
563
562
}
564
563
HttpServletRequest servletRequest = getRequest (attrs );
565
564
HttpServletResponse servletResponse = getResponse (attrs );
@@ -609,52 +608,20 @@ static HttpServletResponse getResponse(Map<String, Object> attrs) {
609
608
return (HttpServletResponse ) attrs .get (HTTP_SERVLET_RESPONSE_ATTR_NAME );
610
609
}
611
610
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" ) ;
614
613
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
+ }
654
619
655
- private UnsupportedOperationException unsupported () {
656
- return new UnsupportedOperationException ("Not Supported" );
657
- }
620
+ @ Override
621
+ public Object getPrincipal () {
622
+ return principalName ;
623
+ }
624
+ };
658
625
}
659
626
660
627
/**
@@ -711,7 +678,7 @@ private Mono<Void> handleResponse(ClientRequest request, ClientResponse response
711
678
ClientAuthorizationException authorizationException = new ClientAuthorizationException (
712
679
oauth2Error , authorizedClient .getClientRegistration ().getRegistrationId ());
713
680
714
- Authentication principal = new PrincipalNameAuthentication (authorizedClient .getPrincipalName ());
681
+ Authentication principal = createAuthentication (authorizedClient .getPrincipalName ());
715
682
HttpServletRequest servletRequest = getRequest (attrs );
716
683
HttpServletResponse servletResponse = getResponse (attrs );
717
684
@@ -779,7 +746,7 @@ private Mono<Void> handleWebClientResponseException(ClientRequest request, WebCl
779
746
ClientAuthorizationException authorizationException = new ClientAuthorizationException (
780
747
oauth2Error , authorizedClient .getClientRegistration ().getRegistrationId (), exception );
781
748
782
- Authentication principal = new PrincipalNameAuthentication (authorizedClient .getPrincipalName ());
749
+ Authentication principal = createAuthentication (authorizedClient .getPrincipalName ());
783
750
HttpServletRequest servletRequest = getRequest (attrs );
784
751
HttpServletResponse servletResponse = getResponse (attrs );
785
752
@@ -804,7 +771,7 @@ private Mono<Void> handleAuthorizationException(ClientRequest request, OAuth2Aut
804
771
return Mono .empty ();
805
772
}
806
773
807
- Authentication principal = new PrincipalNameAuthentication (authorizedClient .getPrincipalName ());
774
+ Authentication principal = createAuthentication (authorizedClient .getPrincipalName ());
808
775
HttpServletRequest servletRequest = getRequest (attrs );
809
776
HttpServletResponse servletResponse = getResponse (attrs );
810
777
0 commit comments