Skip to content

Commit 2f808a5

Browse files
committed
Refactor OAuth2AuthorizationFailureHandler
1 parent d47db53 commit 2f808a5

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

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

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
*
6666
* <pre>
6767
* OAuth2ClientHttpRequestInterceptor requestInterceptor =
68-
* new OAuth2ClientHttpRequestInterceptor(authorizedClientManager, clientRegistrationId);
68+
* new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
6969
* RestClient restClient = RestClient.builder()
7070
* .requestInterceptor(requestInterceptor)
7171
* .build();
@@ -79,17 +79,17 @@
7979
*
8080
* <p>
8181
* This interceptor has the ability to forward authentication (HTTP 401 Unauthorized) and
82-
* authorization (HTTP 403 Forbidden) failures from an OAuth 2.0 Resource Server to a
82+
* authorization (HTTP 403 Forbidden) failures from an OAuth 2.0 Resource Server to an
8383
* {@link OAuth2AuthorizationFailureHandler}. A
8484
* {@link RemoveAuthorizedClientOAuth2AuthorizationFailureHandler} can be used to remove
8585
* the cached {@link OAuth2AuthorizedClient}, so that future requests will result in a new
8686
* token being retrieved from an Authorization Server, and sent to the Resource Server.
8787
*
8888
* <p>
89-
* If either the {@link #setAuthorizedClientRepository(OAuth2AuthorizedClientRepository)}
90-
* setter or {@link #setAuthorizedClientService(OAuth2AuthorizedClientService)} setter is
91-
* used, a {@link RemoveAuthorizedClientOAuth2AuthorizationFailureHandler} will be
92-
* configured automatically.
89+
* Use either {@link #authorizationFailureHandler(OAuth2AuthorizedClientRepository)} or
90+
* {@link #authorizationFailureHandler(OAuth2AuthorizedClientService)} to create a
91+
* {@link RemoveAuthorizedClientOAuth2AuthorizationFailureHandler} which can be provided
92+
* to {@link #setAuthorizationFailureHandler(OAuth2AuthorizationFailureHandler)}.
9393
*
9494
* @author Steve Riesenberg
9595
* @since 6.4
@@ -158,21 +158,21 @@ public OAuth2ClientHttpRequestInterceptor(OAuth2AuthorizedClientManager authoriz
158158
* same token is no longer used in future requests to the Resource Server.
159159
* @param authorizationFailureHandler the {@link OAuth2AuthorizationFailureHandler}
160160
* that handles authentication and authorization failures
161-
* @see #setAuthorizedClientRepository(OAuth2AuthorizedClientRepository)
162-
* @see #setAuthorizedClientService(OAuth2AuthorizedClientService)
161+
* @see #authorizationFailureHandler(OAuth2AuthorizedClientRepository)
162+
* @see #authorizationFailureHandler(OAuth2AuthorizedClientService)
163163
*/
164164
public void setAuthorizationFailureHandler(OAuth2AuthorizationFailureHandler authorizationFailureHandler) {
165165
Assert.notNull(authorizationFailureHandler, "authorizationFailureHandler cannot be null");
166166
this.authorizationFailureHandler = authorizationFailureHandler;
167167
}
168168

169169
/**
170-
* Sets the {@link OAuth2AuthorizedClientRepository} which is used to set up the
171-
* {@link OAuth2AuthorizationFailureHandler} that handles authentication and
172-
* authorization failures when communicating to the OAuth 2.0 Resource Server.
170+
* Provides an {@link OAuth2AuthorizationFailureHandler} that handles authentication
171+
* and authorization failures when communicating to the OAuth 2.0 Resource Server
172+
* using a {@link OAuth2AuthorizedClientRepository}.
173173
*
174174
* <p>
175-
* When this setter is used, authentication (HTTP 401) and authorization (HTTP 403)
175+
* When this method is used, authentication (HTTP 401) and authorization (HTTP 403)
176176
* failures returned from an OAuth 2.0 Resource Server will be forwarded to a
177177
* {@link RemoveAuthorizedClientOAuth2AuthorizationFailureHandler}, which will
178178
* potentially remove the {@link OAuth2AuthorizedClient} from the given
@@ -185,24 +185,24 @@ public void setAuthorizationFailureHandler(OAuth2AuthorizationFailureHandler aut
185185
* to the Resource Server.
186186
* @param authorizedClientRepository the repository of authorized clients
187187
*/
188-
public void setAuthorizedClientRepository(OAuth2AuthorizedClientRepository authorizedClientRepository) {
188+
public static OAuth2AuthorizationFailureHandler authorizationFailureHandler(
189+
OAuth2AuthorizedClientRepository authorizedClientRepository) {
189190
Assert.notNull(authorizedClientRepository, "authorizedClientRepository cannot be null");
190-
this.authorizationFailureHandler = new RemoveAuthorizedClientOAuth2AuthorizationFailureHandler(
191-
(clientRegistrationId, principal, attributes) -> removeAuthorizedClient(authorizedClientRepository,
192-
clientRegistrationId, principal, attributes));
193-
}
194-
195-
private static void removeAuthorizedClient(OAuth2AuthorizedClientRepository authorizedClientRepository,
196-
String clientRegistrationId, Authentication principal, Map<String, Object> attributes) {
197-
HttpServletRequest request = (HttpServletRequest) attributes.get(HttpServletRequest.class.getName());
198-
HttpServletResponse response = (HttpServletResponse) attributes.get(HttpServletResponse.class.getName());
199-
authorizedClientRepository.removeAuthorizedClient(clientRegistrationId, principal, request, response);
191+
return new RemoveAuthorizedClientOAuth2AuthorizationFailureHandler(
192+
(clientRegistrationId, principal, attributes) -> {
193+
HttpServletRequest request = (HttpServletRequest) attributes
194+
.get(HttpServletRequest.class.getName());
195+
HttpServletResponse response = (HttpServletResponse) attributes
196+
.get(HttpServletResponse.class.getName());
197+
authorizedClientRepository.removeAuthorizedClient(clientRegistrationId, principal, request,
198+
response);
199+
});
200200
}
201201

202202
/**
203-
* Sets the {@link OAuth2AuthorizedClientService} which is used to set up the
204-
* {@link OAuth2AuthorizationFailureHandler} that handles authentication and
205-
* authorization failures when communicating to the OAuth 2.0 Resource Server.
203+
* Provides an {@link OAuth2AuthorizationFailureHandler} that handles authentication
204+
* and authorization failures when communicating to the OAuth 2.0 Resource Server
205+
* using a {@link OAuth2AuthorizedClientService}.
206206
*
207207
* <p>
208208
* When this setter is used, authentication (HTTP 401) and authorization (HTTP 403)
@@ -218,16 +218,12 @@ private static void removeAuthorizedClient(OAuth2AuthorizedClientRepository auth
218218
* to the Resource Server.
219219
* @param authorizedClientService the service used to manage authorized clients
220220
*/
221-
public void setAuthorizedClientService(OAuth2AuthorizedClientService authorizedClientService) {
221+
public static OAuth2AuthorizationFailureHandler authorizationFailureHandler(
222+
OAuth2AuthorizedClientService authorizedClientService) {
222223
Assert.notNull(authorizedClientService, "authorizedClientService cannot be null");
223-
this.authorizationFailureHandler = new RemoveAuthorizedClientOAuth2AuthorizationFailureHandler(
224-
(clientRegistrationId, principal, attributes) -> removeAuthorizedClient(authorizedClientService,
225-
clientRegistrationId, principal));
226-
}
227-
228-
private static void removeAuthorizedClient(OAuth2AuthorizedClientService authorizedClientService,
229-
String clientRegistrationId, Authentication principal) {
230-
authorizedClientService.removeAuthorizedClient(clientRegistrationId, principal.getName());
224+
return new RemoveAuthorizedClientOAuth2AuthorizationFailureHandler(
225+
(clientRegistrationId, principal, attributes) -> authorizedClientService
226+
.removeAuthorizedClient(clientRegistrationId, principal.getName()));
231227
}
232228

233229
/**

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/web/function/client/OAuth2ClientHttpRequestInterceptorTests.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import static org.mockito.Mockito.verify;
8282
import static org.mockito.Mockito.verifyNoInteractions;
8383
import static org.mockito.Mockito.verifyNoMoreInteractions;
84+
import static org.springframework.security.oauth2.client.web.function.client.OAuth2ClientHttpRequestInterceptor.authorizationFailureHandler;
8485
import static org.springframework.security.oauth2.client.web.function.client.RequestAttributeClientRegistrationIdResolver.clientRegistrationId;
8586
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
8687
import static org.springframework.test.web.client.match.MockRestRequestMatchers.headerDoesNotExist;
@@ -183,15 +184,16 @@ public void setAuthorizationFailureHandlerWhenNullThenThrowsIllegalArgumentExcep
183184
}
184185

185186
@Test
186-
public void setAuthorizedClientRepositoryWhenNullThenThrowsIllegalArgumentException() {
187+
public void authorizationFailureHandlerWhenAuthorizedClientRepositoryIsNullThenThrowsIllegalArgumentException() {
187188
assertThatIllegalArgumentException()
188-
.isThrownBy(() -> this.requestInterceptor.setAuthorizedClientRepository(null))
189+
.isThrownBy(() -> authorizationFailureHandler((OAuth2AuthorizedClientRepository) null))
189190
.withMessage("authorizedClientRepository cannot be null");
190191
}
191192

192193
@Test
193-
public void setAuthorizedClientServiceWhenNullThenThrowsIllegalArgumentException() {
194-
assertThatIllegalArgumentException().isThrownBy(() -> this.requestInterceptor.setAuthorizedClientService(null))
194+
public void authorizationFailureHandlerWhenAuthorizedClientServiceIsNullThenThrowsIllegalArgumentException() {
195+
assertThatIllegalArgumentException()
196+
.isThrownBy(() -> authorizationFailureHandler((OAuth2AuthorizedClientService) null))
195197
.withMessage("authorizedClientService cannot be null");
196198
}
197199

@@ -548,8 +550,9 @@ public void interceptWhenAuthorizationExceptionThenCallsAuthorizationFailureHand
548550
}
549551

550552
@Test
551-
public void interceptWhenUnauthorizedAndAuthorizedClientRepositorySetThenAuthorizedClientRemoved() {
552-
this.requestInterceptor.setAuthorizedClientRepository(this.authorizedClientRepository);
553+
public void interceptWhenUnauthorizedAndAuthorizationFailureHandlerSetWithAuthorizedClientRepositoryThenAuthorizedClientRemoved() {
554+
this.requestInterceptor
555+
.setAuthorizationFailureHandler(authorizationFailureHandler(this.authorizedClientRepository));
553556
given(this.authorizedClientManager.authorize(any(OAuth2AuthorizeRequest.class)))
554557
.willReturn(this.authorizedClient);
555558

@@ -574,8 +577,9 @@ public void interceptWhenUnauthorizedAndAuthorizedClientRepositorySetThenAuthori
574577
}
575578

576579
@Test
577-
public void interceptWhenUnauthorizedAndAuthorizedClientServiceSetThenAuthorizedClientRemoved() {
578-
this.requestInterceptor.setAuthorizedClientService(this.authorizedClientService);
580+
public void interceptWhenUnauthorizedAndAuthorizationFailureHandlerSetWithAuthorizedClientServiceThenAuthorizedClientRemoved() {
581+
this.requestInterceptor
582+
.setAuthorizationFailureHandler(authorizationFailureHandler(this.authorizedClientService));
579583
given(this.authorizedClientManager.authorize(any(OAuth2AuthorizeRequest.class)))
580584
.willReturn(this.authorizedClient);
581585

0 commit comments

Comments
 (0)