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 gh-16377 Signed-off-by: yybmion <yunyubin54@gmail.com>
This commit is contained in:
parent
2a24bb0b26
commit
1d02af11a8
|
@ -476,6 +476,11 @@ public final class ClientRegistration implements Serializable {
|
||||||
* Configuring uri template variables is especially useful when the client is
|
* Configuring uri template variables is especially useful when the client is
|
||||||
* running behind a Proxy Server. This ensures that the X-Forwarded-* headers are
|
* running behind a Proxy Server. This ensures that the X-Forwarded-* headers are
|
||||||
* used when expanding the redirect-uri.
|
* used when expanding the redirect-uri.
|
||||||
|
*
|
||||||
|
* <br />
|
||||||
|
* If not specified and authorization grant type is
|
||||||
|
* {@link AuthorizationGrantType#AUTHORIZATION_CODE}, defaults to
|
||||||
|
* "{baseUrl}/login/oauth2/code/{registrationId}".
|
||||||
* @param redirectUri the uri (or uri template) for the redirection endpoint
|
* @param redirectUri the uri (or uri template) for the redirection endpoint
|
||||||
* @return the {@link Builder}
|
* @return the {@link Builder}
|
||||||
* @since 5.4
|
* @since 5.4
|
||||||
|
@ -627,6 +632,10 @@ public final class ClientRegistration implements Serializable {
|
||||||
*/
|
*/
|
||||||
public ClientRegistration build() {
|
public ClientRegistration build() {
|
||||||
Assert.notNull(this.authorizationGrantType, "authorizationGrantType cannot be null");
|
Assert.notNull(this.authorizationGrantType, "authorizationGrantType cannot be null");
|
||||||
|
if (this.redirectUri == null && this.registrationId != null
|
||||||
|
&& AuthorizationGrantType.AUTHORIZATION_CODE.equals(this.authorizationGrantType)) {
|
||||||
|
this.redirectUri = "{baseUrl}/login/oauth2/code/" + this.registrationId;
|
||||||
|
}
|
||||||
if (AuthorizationGrantType.CLIENT_CREDENTIALS.equals(this.authorizationGrantType)) {
|
if (AuthorizationGrantType.CLIENT_CREDENTIALS.equals(this.authorizationGrantType)) {
|
||||||
this.validateClientCredentialsGrantType();
|
this.validateClientCredentialsGrantType();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -258,24 +258,22 @@ public class ClientRegistrationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void buildWhenAuthorizationCodeGrantRedirectUriIsNullThenThrowIllegalArgumentException() {
|
public void buildWhenAuthorizationCodeGrantRedirectUriIsNullThenDefaultsToLoginOAuth2Code() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
ClientRegistration.withRegistrationId(REGISTRATION_ID)
|
ClientRegistration registration = ClientRegistration.withRegistrationId(REGISTRATION_ID)
|
||||||
.clientId(CLIENT_ID)
|
.clientId(CLIENT_ID)
|
||||||
.clientSecret(CLIENT_SECRET)
|
.clientSecret(CLIENT_SECRET)
|
||||||
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
|
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
|
||||||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
||||||
.redirectUri(null)
|
.scope(SCOPES.toArray(new String[0]))
|
||||||
.scope(SCOPES.toArray(new String[0]))
|
.authorizationUri(AUTHORIZATION_URI)
|
||||||
.authorizationUri(AUTHORIZATION_URI)
|
.tokenUri(TOKEN_URI)
|
||||||
.tokenUri(TOKEN_URI)
|
.userInfoAuthenticationMethod(AuthenticationMethod.FORM)
|
||||||
.userInfoAuthenticationMethod(AuthenticationMethod.FORM)
|
.jwkSetUri(JWK_SET_URI)
|
||||||
.jwkSetUri(JWK_SET_URI)
|
.clientName(CLIENT_NAME)
|
||||||
.clientName(CLIENT_NAME)
|
.build();
|
||||||
.build()
|
|
||||||
// @formatter:on
|
// @formatter:on
|
||||||
);
|
assertThat(registration.getRedirectUri()).isEqualTo("{baseUrl}/login/oauth2/code/" + REGISTRATION_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
// gh-5494
|
// gh-5494
|
||||||
|
|
Loading…
Reference in New Issue