forked from spring-projects/spring-security
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FormRedirectStrategy to enable POST OIDC Logout
FormRedirectStrategy redirects using an autosubmitting HTML form using the POST method versus DefaultRedirectStrategy which redirects using the GET method. Can be used to implement POST binding for relying party initiated OIDC logout by setting FormRedirectStrategy as the redirection strategy on OidcClientInitiatedLogoutSuccessHandler. Closes spring-projectsgh-13002
- Loading branch information
Showing
9 changed files
with
302 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
web/src/main/java/org/springframework/security/web/server/ui/FormRedirectStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2002-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.web.server.ui; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.web.RedirectStrategy; | ||
import org.springframework.web.util.HtmlUtils; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
/** | ||
* Redirect using an autosubmitting HTML form using the POST method. All query params | ||
* provided in the URL are changed to inputs in the form so they are submitted as POST | ||
* data instead of query string data. | ||
*/ | ||
/* default */ class FormRedirectStrategy implements RedirectStrategy { | ||
|
||
private static final String REDIRECT_PAGE_TEMPLATE = """ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<meta name="description" content=""> | ||
<meta name="author" content=""> | ||
<title>Redirect</title> | ||
<link href="{{contextPath}}/default-ui.css" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<div class="content"> | ||
<form id="redirectForm" class="redirect-form" method="POST" action="{{action}}"> | ||
{{params}} | ||
<button class="primary" type="submit">Click to Continue</button> | ||
</form> | ||
</div> | ||
<script src="{{contextPath}}/form-redirect.js"> | ||
</body> | ||
</html> | ||
"""; | ||
|
||
private static final String HIDDEN_INPUT_TEMPLATE = """ | ||
<input name="{{name}}" type="hidden" value="{{value}}" /> | ||
"""; | ||
|
||
@Override | ||
public void sendRedirect(final HttpServletRequest request, final HttpServletResponse response, final String url) | ||
throws IOException { | ||
final UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(url); | ||
|
||
final StringBuilder hiddenInputsHtmlBuilder = new StringBuilder(); | ||
// inputs | ||
for (final Entry<String, List<String>> entry : uriComponentsBuilder.build().getQueryParams().entrySet()) { | ||
final String name = entry.getKey(); | ||
for (final String value : entry.getValue()) { | ||
hiddenInputsHtmlBuilder.append(HtmlTemplates.fromTemplate(HIDDEN_INPUT_TEMPLATE) | ||
.withValue("name", name) | ||
.withValue("value", value) | ||
.render()); | ||
} | ||
} | ||
|
||
final String html = HtmlTemplates.fromTemplate(REDIRECT_PAGE_TEMPLATE) | ||
// clear the query string as we don't want that to be part of the form action | ||
// URL | ||
.withValue("action", uriComponentsBuilder.query(null).build().toUriString()) | ||
.withRawHtml("params", hiddenInputsHtmlBuilder.toString()) | ||
.withValue("contextPath", request.getContextPath()) | ||
.render(); | ||
response.setStatus(HttpStatus.OK.value()); | ||
response.setContentType(MediaType.TEXT_HTML_VALUE); | ||
response.getWriter().write(html); | ||
response.getWriter().flush(); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
web/src/main/resources/org/springframework/security/form-redirect.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
document.getElementById("redirectForm").submit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ramework/security/web/server/ui/DefaultResourcesFormRedirectJavascriptWebFilterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.web.server.ui; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; | ||
import org.springframework.mock.web.server.MockServerWebExchange; | ||
import org.springframework.web.server.WebFilterChain; | ||
import org.springframework.web.server.WebHandler; | ||
import org.springframework.web.server.handler.DefaultWebFilterChain; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Craig Andrews | ||
* @since 6.4 | ||
*/ | ||
class DefaultResourcesFormRedirectJavascriptWebFilterTests { | ||
|
||
private final WebHandler notFoundHandler = (exchange) -> { | ||
exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND); | ||
return Mono.empty(); | ||
}; | ||
|
||
private final DefaultResourcesWebFilter filter = DefaultResourcesWebFilter.formRedirectJavascript(); | ||
|
||
@Test | ||
void filterWhenPathMatchesThenRenders() { | ||
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/form-redirect.js")); | ||
WebFilterChain filterChain = new DefaultWebFilterChain(this.notFoundHandler, List.of(this.filter)); | ||
|
||
filterChain.filter(exchange).block(); | ||
|
||
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK); | ||
assertThat(exchange.getResponse().getHeaders().getContentType()) | ||
.isEqualTo(new MediaType("text", "javascript", StandardCharsets.UTF_8)); | ||
assertThat(exchange.getResponse().getBodyAsString().block()).contains("document"); | ||
} | ||
|
||
@Test | ||
void filterWhenPathDoesNotMatchThenCallsThrough() { | ||
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/does-not-match")); | ||
WebFilterChain filterChain = new DefaultWebFilterChain(this.notFoundHandler, List.of(this.filter)); | ||
|
||
filterChain.filter(exchange).block(); | ||
|
||
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@Test | ||
void toStringPrintsPathAndResource() { | ||
assertThat(this.filter.toString()).isEqualTo( | ||
"DefaultResourcesWebFilter{matcher=PathMatcherServerWebExchangeMatcher{pattern='/form-redirect.js', method=GET}, resource='org/springframework/security/form-redirect.js'}"); | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
web/src/test/java/org/springframework/security/web/server/ui/FormRedirectStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2002-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.web.server.ui; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.mock.web.MockServletContext; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
public class FormRedirectStrategyTest { | ||
|
||
private FormRedirectStrategy formRedirectStrategy; | ||
|
||
private MockHttpServletRequest request; | ||
|
||
private MockHttpServletResponse response; | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
formRedirectStrategy = new FormRedirectStrategy(); | ||
final MockServletContext mockServletContext = new MockServletContext(); | ||
mockServletContext.setContextPath("/contextPath"); | ||
// the request URL doesn't matter | ||
request = MockMvcRequestBuilders.get("http://localhost").buildRequest(mockServletContext); | ||
response = new MockHttpServletResponse(); | ||
} | ||
|
||
@Test | ||
public void absoluteUrlNoParametersRedirect() throws IOException { | ||
formRedirectStrategy.sendRedirect(request, response, "http://example.com"); | ||
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); | ||
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); | ||
assertThat(response.getContentAsString()).contains("action=\"http://example.com\""); | ||
} | ||
|
||
@Test | ||
public void rootRelativeUrlNoParametersRedirect() throws IOException { | ||
formRedirectStrategy.sendRedirect(request, response, "/test"); | ||
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); | ||
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); | ||
assertThat(response.getContentAsString()).contains("action=\"/test\""); | ||
} | ||
|
||
@Test | ||
public void relativeUrlNoParametersRedirect() throws IOException { | ||
formRedirectStrategy.sendRedirect(request, response, "test"); | ||
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); | ||
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); | ||
assertThat(response.getContentAsString()).contains("action=\"test\""); | ||
} | ||
|
||
@Test | ||
public void absoluteUrlWithFragmentRedirect() throws IOException { | ||
formRedirectStrategy.sendRedirect(request, response, "http://example.com/path#fragment"); | ||
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); | ||
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); | ||
assertThat(response.getContentAsString()).contains("action=\"http://example.com/path#fragment\""); | ||
} | ||
|
||
@Test | ||
public void absoluteUrlWithQueryParamsRedirect() throws IOException { | ||
formRedirectStrategy.sendRedirect(request, response, "http://example.com/path?param1=one¶m2=two#fragment"); | ||
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); | ||
assertThat(response.getContentType()).isEqualTo(MediaType.TEXT_HTML_VALUE); | ||
assertThat(response.getContentAsString()).contains("action=\"http://example.com/path#fragment\""); | ||
assertThat(response.getContentAsString()).contains("<input name=\"param1\" type=\"hidden\" value=\"one\" />"); | ||
assertThat(response.getContentAsString()).contains("<input name=\"param2\" type=\"hidden\" value=\"two\" />"); | ||
} | ||
|
||
} |