65
65
*
66
66
* <pre>
67
67
* OAuth2ClientHttpRequestInterceptor requestInterceptor =
68
- * new OAuth2ClientHttpRequestInterceptor(authorizedClientManager, clientRegistrationId );
68
+ * new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
69
69
* RestClient restClient = RestClient.builder()
70
70
* .requestInterceptor(requestInterceptor)
71
71
* .build();
79
79
*
80
80
* <p>
81
81
* 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
83
83
* {@link OAuth2AuthorizationFailureHandler}. A
84
84
* {@link RemoveAuthorizedClientOAuth2AuthorizationFailureHandler} can be used to remove
85
85
* the cached {@link OAuth2AuthorizedClient}, so that future requests will result in a new
86
86
* token being retrieved from an Authorization Server, and sent to the Resource Server.
87
87
*
88
88
* <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)} .
93
93
*
94
94
* @author Steve Riesenberg
95
95
* @since 6.4
@@ -158,21 +158,21 @@ public OAuth2ClientHttpRequestInterceptor(OAuth2AuthorizedClientManager authoriz
158
158
* same token is no longer used in future requests to the Resource Server.
159
159
* @param authorizationFailureHandler the {@link OAuth2AuthorizationFailureHandler}
160
160
* that handles authentication and authorization failures
161
- * @see #setAuthorizedClientRepository (OAuth2AuthorizedClientRepository)
162
- * @see #setAuthorizedClientService (OAuth2AuthorizedClientService)
161
+ * @see #authorizationFailureHandler (OAuth2AuthorizedClientRepository)
162
+ * @see #authorizationFailureHandler (OAuth2AuthorizedClientService)
163
163
*/
164
164
public void setAuthorizationFailureHandler (OAuth2AuthorizationFailureHandler authorizationFailureHandler ) {
165
165
Assert .notNull (authorizationFailureHandler , "authorizationFailureHandler cannot be null" );
166
166
this .authorizationFailureHandler = authorizationFailureHandler ;
167
167
}
168
168
169
169
/**
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} .
173
173
*
174
174
* <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)
176
176
* failures returned from an OAuth 2.0 Resource Server will be forwarded to a
177
177
* {@link RemoveAuthorizedClientOAuth2AuthorizationFailureHandler}, which will
178
178
* potentially remove the {@link OAuth2AuthorizedClient} from the given
@@ -185,24 +185,24 @@ public void setAuthorizationFailureHandler(OAuth2AuthorizationFailureHandler aut
185
185
* to the Resource Server.
186
186
* @param authorizedClientRepository the repository of authorized clients
187
187
*/
188
- public void setAuthorizedClientRepository (OAuth2AuthorizedClientRepository authorizedClientRepository ) {
188
+ public static OAuth2AuthorizationFailureHandler authorizationFailureHandler (
189
+ OAuth2AuthorizedClientRepository authorizedClientRepository ) {
189
190
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
+ });
200
200
}
201
201
202
202
/**
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} .
206
206
*
207
207
* <p>
208
208
* When this setter is used, authentication (HTTP 401) and authorization (HTTP 403)
@@ -218,16 +218,12 @@ private static void removeAuthorizedClient(OAuth2AuthorizedClientRepository auth
218
218
* to the Resource Server.
219
219
* @param authorizedClientService the service used to manage authorized clients
220
220
*/
221
- public void setAuthorizedClientService (OAuth2AuthorizedClientService authorizedClientService ) {
221
+ public static OAuth2AuthorizationFailureHandler authorizationFailureHandler (
222
+ OAuth2AuthorizedClientService authorizedClientService ) {
222
223
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 ()));
231
227
}
232
228
233
229
/**
0 commit comments