43
43
import org .springframework .security .core .authority .AuthorityUtils ;
44
44
import org .springframework .security .core .context .SecurityContext ;
45
45
import org .springframework .security .core .context .SecurityContextHolder ;
46
- import org .springframework .security .core .context .SecurityContextHolderStrategy ;
47
46
import org .springframework .security .core .context .SecurityContextImpl ;
48
47
import org .springframework .security .oauth2 .client .ClientAuthorizationException ;
49
48
import org .springframework .security .oauth2 .client .OAuth2AuthorizationFailureHandler ;
@@ -110,15 +109,15 @@ public class OAuth2ClientHttpRequestInterceptorTests {
110
109
@ Mock
111
110
private OAuth2AuthorizedClientRepository authorizedClientRepository ;
112
111
113
- @ Mock
114
- private SecurityContextHolderStrategy securityContextHolderStrategy ;
115
-
116
112
@ Mock
117
113
private OAuth2AuthorizedClientService authorizedClientService ;
118
114
119
115
@ Mock
120
116
private OAuth2ClientHttpRequestInterceptor .ClientRegistrationIdResolver clientRegistrationIdResolver ;
121
117
118
+ @ Mock
119
+ private OAuth2ClientHttpRequestInterceptor .PrincipalResolver principalResolver ;
120
+
122
121
@ Captor
123
122
private ArgumentCaptor <OAuth2AuthorizeRequest > authorizeRequestCaptor ;
124
123
@@ -167,13 +166,6 @@ public void constructorWhenAuthorizedClientManagerIsNullThenThrowsIllegalArgumen
167
166
.withMessage ("authorizedClientManager cannot be null" );
168
167
}
169
168
170
- @ Test
171
- public void constructorWhenClientRegistrationIdResolverIsNullThenThrowsIllegalArgumentException () {
172
- assertThatIllegalArgumentException ()
173
- .isThrownBy (() -> new OAuth2ClientHttpRequestInterceptor (this .authorizedClientManager , null ))
174
- .withMessage ("clientRegistrationIdResolver cannot be null" );
175
- }
176
-
177
169
@ Test
178
170
public void setAuthorizationFailureHandlerWhenNullThenThrowsIllegalArgumentException () {
179
171
assertThatIllegalArgumentException ()
@@ -198,10 +190,16 @@ public void authorizationFailureHandlerWhenAuthorizedClientServiceIsNullThenThro
198
190
}
199
191
200
192
@ Test
201
- public void setSecurityContextHolderStrategyWhenNullThenThrowsIllegalArgumentException () {
193
+ public void setClientRegistrationIdResolverWhenNullThenThrowsIllegalArgumentException () {
202
194
assertThatIllegalArgumentException ()
203
- .isThrownBy (() -> this .requestInterceptor .setSecurityContextHolderStrategy (null ))
204
- .withMessage ("securityContextHolderStrategy cannot be null" );
195
+ .isThrownBy (() -> this .requestInterceptor .setClientRegistrationIdResolver (null ))
196
+ .withMessage ("clientRegistrationIdResolver cannot be null" );
197
+ }
198
+
199
+ @ Test
200
+ public void setPrincipalResolverWhenNullThenThrowsIllegalArgumentException () {
201
+ assertThatIllegalArgumentException ().isThrownBy (() -> this .requestInterceptor .setPrincipalResolver (null ))
202
+ .withMessage ("principalResolver cannot be null" );
205
203
}
206
204
207
205
@ Test
@@ -605,8 +603,7 @@ public void interceptWhenUnauthorizedAndAuthorizationFailureHandlerSetWithAuthor
605
603
606
604
@ Test
607
605
public void interceptWhenCustomClientRegistrationIdResolverSetThenUsed () {
608
- this .requestInterceptor = new OAuth2ClientHttpRequestInterceptor (this .authorizedClientManager ,
609
- this .clientRegistrationIdResolver );
606
+ this .requestInterceptor .setClientRegistrationIdResolver (this .clientRegistrationIdResolver );
610
607
this .requestInterceptor .setAuthorizationFailureHandler (this .authorizationFailureHandler );
611
608
given (this .authorizedClientManager .authorize (any (OAuth2AuthorizeRequest .class )))
612
609
.willReturn (this .authorizedClient );
@@ -625,31 +622,29 @@ public void interceptWhenCustomClientRegistrationIdResolverSetThenUsed() {
625
622
this .server .verify ();
626
623
verify (this .authorizedClientManager ).authorize (this .authorizeRequestCaptor .capture ());
627
624
verify (this .clientRegistrationIdResolver ).resolve (any (HttpRequest .class ));
628
- verifyNoMoreInteractions (this .clientRegistrationIdResolver , this .authorizedClientManager );
625
+ verifyNoMoreInteractions (this .authorizedClientManager , this .clientRegistrationIdResolver );
629
626
verifyNoInteractions (this .authorizationFailureHandler );
630
627
OAuth2AuthorizeRequest authorizeRequest = this .authorizeRequestCaptor .getValue ();
631
628
assertThat (authorizeRequest .getClientRegistrationId ()).isEqualTo (clientRegistrationId );
632
629
assertThat (authorizeRequest .getPrincipal ()).isEqualTo (this .principal );
633
630
}
634
631
635
632
@ Test
636
- public void interceptWhenCustomSecurityContextHolderStrategySetThenUsed () {
637
- this .requestInterceptor .setSecurityContextHolderStrategy (this .securityContextHolderStrategy );
633
+ public void interceptWhenCustomPrincipalResolverSetThenUsed () {
634
+ this .requestInterceptor .setPrincipalResolver (this .principalResolver );
638
635
given (this .authorizedClientManager .authorize (any (OAuth2AuthorizeRequest .class )))
639
636
.willReturn (this .authorizedClient );
640
637
641
638
bindToRestClient (withRequestInterceptor ());
642
639
this .server .expect (requestTo (REQUEST_URI ))
643
640
.andExpect (hasAuthorizationHeader (this .authorizedClient .getAccessToken ()))
644
641
.andRespond (withApplicationJson ());
645
- SecurityContext securityContext = new SecurityContextImpl ();
646
- securityContext .setAuthentication (this .principal );
647
- given (this .securityContextHolderStrategy .getContext ()).willReturn (securityContext );
642
+ given (this .principalResolver .resolve (any (HttpRequest .class ))).willReturn (this .principal );
648
643
performRequest (withClientRegistrationId ());
649
644
this .server .verify ();
650
645
verify (this .authorizedClientManager ).authorize (this .authorizeRequestCaptor .capture ());
651
- verify (this .securityContextHolderStrategy ). getContext ( );
652
- verifyNoMoreInteractions (this .authorizedClientManager , this .securityContextHolderStrategy );
646
+ verify (this .principalResolver ). resolve ( any ( HttpRequest . class ) );
647
+ verifyNoMoreInteractions (this .authorizedClientManager , this .principalResolver );
653
648
OAuth2AuthorizeRequest authorizeRequest = this .authorizeRequestCaptor .getValue ();
654
649
assertThat (authorizeRequest .getClientRegistrationId ()).isEqualTo (this .clientRegistration .getRegistrationId ());
655
650
assertThat (authorizeRequest .getPrincipal ()).isEqualTo (this .principal );
0 commit comments