Skip to content

Commit a5351f3

Browse files
vanwobejzheaux
authored andcommitted
LogoutPageGeneratingWebFilter Uses Context Path
Closes gh-11716
1 parent 070dce1 commit a5351f3

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

web/src/main/java/org/springframework/security/web/server/ui/LogoutPageGeneratingWebFilter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -58,14 +58,15 @@ private Mono<Void> render(ServerWebExchange exchange) {
5858

5959
private Mono<DataBuffer> createBuffer(ServerWebExchange exchange) {
6060
Mono<CsrfToken> token = exchange.getAttributeOrDefault(CsrfToken.class.getName(), Mono.empty());
61+
String contextPath = exchange.getRequest().getPath().contextPath().value();
6162
return token.map(LogoutPageGeneratingWebFilter::csrfToken).defaultIfEmpty("").map((csrfTokenHtmlInput) -> {
62-
byte[] bytes = createPage(csrfTokenHtmlInput);
63+
byte[] bytes = createPage(csrfTokenHtmlInput, contextPath);
6364
DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
6465
return bufferFactory.wrap(bytes);
6566
});
6667
}
6768

68-
private static byte[] createPage(String csrfTokenHtmlInput) {
69+
private static byte[] createPage(String csrfTokenHtmlInput, String contextPath) {
6970
StringBuilder page = new StringBuilder();
7071
page.append("<!DOCTYPE html>\n");
7172
page.append("<html lang=\"en\">\n");
@@ -82,7 +83,7 @@ private static byte[] createPage(String csrfTokenHtmlInput) {
8283
page.append(" </head>\n");
8384
page.append(" <body>\n");
8485
page.append(" <div class=\"container\">\n");
85-
page.append(" <form class=\"form-signin\" method=\"post\" action=\"/logout\">\n");
86+
page.append(" <form class=\"form-signin\" method=\"post\" action=\"" + contextPath + "/logout\">\n");
8687
page.append(" <h2 class=\"form-signin-heading\">Are you sure you want to log out?</h2>\n");
8788
page.append(csrfTokenHtmlInput);
8889
page.append(" <button class=\"btn btn-lg btn-primary btn-block\" type=\"submit\">Log Out</button>\n");
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.server.ui;
18+
19+
import org.junit.jupiter.api.Test;
20+
import reactor.core.publisher.Mono;
21+
22+
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
23+
import org.springframework.mock.web.server.MockServerWebExchange;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
public class LogoutPageGeneratingWebFilterTests {
28+
29+
@Test
30+
public void filterWhenLogoutWithContextPathThenActionContainsContextPath() throws Exception {
31+
LogoutPageGeneratingWebFilter filter = new LogoutPageGeneratingWebFilter();
32+
MockServerWebExchange exchange = MockServerWebExchange
33+
.from(MockServerHttpRequest.get("/test/logout").contextPath("/test"));
34+
filter.filter(exchange, (e) -> Mono.empty()).block();
35+
assertThat(exchange.getResponse().getBodyAsString().block()).contains("action=\"/test/logout\"");
36+
}
37+
38+
@Test
39+
public void filterWhenLogoutWithNoContextPathThenActionDoesNotContainsContextPath() throws Exception {
40+
LogoutPageGeneratingWebFilter filter = new LogoutPageGeneratingWebFilter();
41+
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/logout"));
42+
filter.filter(exchange, (e) -> Mono.empty()).block();
43+
assertThat(exchange.getResponse().getBodyAsString().block()).contains("action=\"/logout\"");
44+
}
45+
46+
}

0 commit comments

Comments
 (0)