Skip to content

Commit 1d02af1

Browse files
committed
Add default redirect URI for OAuth2 client registration
When redirect URI is not provided for an OAuth2 client registration with authorization code grant type, default it to {baseUrl}/login/oauth2/code/{registrationId}. Closes spring-projectsgh-16377 Signed-off-by: yybmion <[email protected]>
1 parent 2a24bb0 commit 1d02af1

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

Diff for: oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/ClientRegistration.java

+9
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,11 @@ public Builder authorizationGrantType(AuthorizationGrantType authorizationGrantT
476476
* Configuring uri template variables is especially useful when the client is
477477
* running behind a Proxy Server. This ensures that the X-Forwarded-* headers are
478478
* used when expanding the redirect-uri.
479+
*
480+
* <br />
481+
* If not specified and authorization grant type is
482+
* {@link AuthorizationGrantType#AUTHORIZATION_CODE}, defaults to
483+
* "{baseUrl}/login/oauth2/code/{registrationId}".
479484
* @param redirectUri the uri (or uri template) for the redirection endpoint
480485
* @return the {@link Builder}
481486
* @since 5.4
@@ -627,6 +632,10 @@ public Builder clientSettings(ClientSettings clientSettings) {
627632
*/
628633
public ClientRegistration build() {
629634
Assert.notNull(this.authorizationGrantType, "authorizationGrantType cannot be null");
635+
if (this.redirectUri == null && this.registrationId != null
636+
&& AuthorizationGrantType.AUTHORIZATION_CODE.equals(this.authorizationGrantType)) {
637+
this.redirectUri = "{baseUrl}/login/oauth2/code/" + this.registrationId;
638+
}
630639
if (AuthorizationGrantType.CLIENT_CREDENTIALS.equals(this.authorizationGrantType)) {
631640
this.validateClientCredentialsGrantType();
632641
}

Diff for: oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/ClientRegistrationTests.java

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -258,24 +258,22 @@ public void buildWhenAuthorizationCodeGrantClientAuthenticationMethodNotProvided
258258
}
259259

260260
@Test
261-
public void buildWhenAuthorizationCodeGrantRedirectUriIsNullThenThrowIllegalArgumentException() {
262-
assertThatIllegalArgumentException().isThrownBy(() ->
261+
public void buildWhenAuthorizationCodeGrantRedirectUriIsNullThenDefaultsToLoginOAuth2Code() {
263262
// @formatter:off
264-
ClientRegistration.withRegistrationId(REGISTRATION_ID)
265-
.clientId(CLIENT_ID)
266-
.clientSecret(CLIENT_SECRET)
267-
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
268-
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
269-
.redirectUri(null)
270-
.scope(SCOPES.toArray(new String[0]))
271-
.authorizationUri(AUTHORIZATION_URI)
272-
.tokenUri(TOKEN_URI)
273-
.userInfoAuthenticationMethod(AuthenticationMethod.FORM)
274-
.jwkSetUri(JWK_SET_URI)
275-
.clientName(CLIENT_NAME)
276-
.build()
263+
ClientRegistration registration = ClientRegistration.withRegistrationId(REGISTRATION_ID)
264+
.clientId(CLIENT_ID)
265+
.clientSecret(CLIENT_SECRET)
266+
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
267+
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
268+
.scope(SCOPES.toArray(new String[0]))
269+
.authorizationUri(AUTHORIZATION_URI)
270+
.tokenUri(TOKEN_URI)
271+
.userInfoAuthenticationMethod(AuthenticationMethod.FORM)
272+
.jwkSetUri(JWK_SET_URI)
273+
.clientName(CLIENT_NAME)
274+
.build();
277275
// @formatter:on
278-
);
276+
assertThat(registration.getRedirectUri()).isEqualTo("{baseUrl}/login/oauth2/code/" + REGISTRATION_ID);
279277
}
280278

281279
// gh-5494

0 commit comments

Comments
 (0)