Skip to content

Commit c8f8149

Browse files
committed
Add placeholders to post_logout_redirect_uri
Now supports baseScheme, baseHost, basePort, and basePath in addition to extant baseUrl. Issue gh-11229
1 parent 2b47944 commit c8f8149

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/oidc/web/logout/OidcClientInitiatedLogoutSuccessHandler.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,19 @@ private String postLogoutRedirectUri(HttpServletRequest request, ClientRegistrat
103103
.build();
104104

105105
Map<String, String> uriVariables = new HashMap<>();
106+
String scheme = uriComponents.getScheme();
107+
uriVariables.put("baseScheme", (scheme != null) ? scheme : "");
106108
uriVariables.put("baseUrl", uriComponents.toUriString());
109+
110+
String host = uriComponents.getHost();
111+
uriVariables.put("baseHost", (host != null) ? host : "");
112+
113+
String path = uriComponents.getPath();
114+
uriVariables.put("basePath", (path != null) ? path : "");
115+
116+
int port = uriComponents.getPort();
117+
uriVariables.put("basePort", (port == -1) ? "" : ":" + port);
118+
107119
uriVariables.put("registrationId", clientRegistration.getRegistrationId());
108120

109121
return UriComponentsBuilder.fromUriString(this.postLogoutRedirectUri)
@@ -138,8 +150,15 @@ public void setPostLogoutRedirectUri(URI postLogoutRedirectUri) {
138150
}
139151

140152
/**
141-
* Set the post logout redirect uri template to use. Supports the {@code "{baseUrl}"}
142-
* placeholder, for example:
153+
* Set the post logout redirect uri template.
154+
*
155+
* <br />
156+
* The supported uri template variables are: {@code {baseScheme}}, {@code {baseHost}},
157+
* {@code {basePort}} and {@code {basePath}}.
158+
*
159+
* <br />
160+
* <b>NOTE:</b> {@code "{baseUrl}"} is also supported, which is the same as
161+
* {@code "{baseScheme}://{baseHost}{basePort}{basePath}"}
143162
*
144163
* <pre>
145164
* handler.setPostLogoutRedirectUri("{baseUrl}");

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/oidc/web/logout/OidcClientInitiatedLogoutSuccessHandlerTests.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void logoutWhenUsingPostLogoutRedirectUriThenIncludesItInRedirect() throw
123123
}
124124

125125
@Test
126-
public void logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirect()
126+
public void logoutWhenUsingPostLogoutBaseUrlRedirectUriTemplateThenBuildsItForRedirect()
127127
throws IOException, ServletException {
128128
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
129129
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
@@ -137,6 +137,36 @@ public void logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirect(
137137
"https://endpoint?" + "id_token_hint=id-token&" + "post_logout_redirect_uri=https://rp.example.org");
138138
}
139139

140+
@Test
141+
public void logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirect()
142+
throws IOException, ServletException {
143+
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
144+
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
145+
this.handler.setPostLogoutRedirectUri("{baseScheme}://{baseHost}{basePort}{basePath}");
146+
this.request.setScheme("https");
147+
this.request.setServerPort(443);
148+
this.request.setServerName("rp.example.org");
149+
this.request.setUserPrincipal(token);
150+
this.handler.onLogoutSuccess(this.request, this.response, token);
151+
assertThat(this.response.getRedirectedUrl()).isEqualTo(
152+
"https://endpoint?" + "id_token_hint=id-token&" + "post_logout_redirect_uri=https://rp.example.org");
153+
}
154+
155+
@Test
156+
public void logoutWhenUsingPostLogoutRedirectUriTemplateWithOtherPortThenBuildsItForRedirect()
157+
throws IOException, ServletException {
158+
OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(),
159+
AuthorityUtils.NO_AUTHORITIES, this.registration.getRegistrationId());
160+
this.handler.setPostLogoutRedirectUri("{baseScheme}://{baseHost}{basePort}{basePath}");
161+
this.request.setScheme("https");
162+
this.request.setServerPort(400);
163+
this.request.setServerName("rp.example.org");
164+
this.request.setUserPrincipal(token);
165+
this.handler.onLogoutSuccess(this.request, this.response, token);
166+
assertThat(this.response.getRedirectedUrl()).isEqualTo("https://endpoint?" + "id_token_hint=id-token&"
167+
+ "post_logout_redirect_uri=https://rp.example.org:400");
168+
}
169+
140170
@Test
141171
public void logoutWhenUsingPostLogoutRedirectUriTemplateThenBuildsItForRedirectExpanded()
142172
throws IOException, ServletException {

0 commit comments

Comments
 (0)