Skip to content

Commit dcd2137

Browse files
committed
LoginPageGeneratingWebFilter honors context path
Closes gh-8807
1 parent 1bb49bb commit dcd2137

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private byte[] createPage(ServerWebExchange exchange, String csrfTokenHtmlInput)
105105
+ " </head>\n"
106106
+ " <body>\n"
107107
+ " <div class=\"container\">\n"
108-
+ formLogin(queryParams, csrfTokenHtmlInput)
108+
+ formLogin(queryParams, contextPath, csrfTokenHtmlInput)
109109
+ oauth2LoginLinks(queryParams, contextPath, this.oauth2AuthenticationUrlToClientName)
110110
+ " </div>\n"
111111
+ " </body>\n"
@@ -114,13 +114,13 @@ private byte[] createPage(ServerWebExchange exchange, String csrfTokenHtmlInput)
114114
return page.getBytes(Charset.defaultCharset());
115115
}
116116

117-
private String formLogin(MultiValueMap<String, String> queryParams, String csrfTokenHtmlInput) {
117+
private String formLogin(MultiValueMap<String, String> queryParams, String contextPath, String csrfTokenHtmlInput) {
118118
if (!this.formLoginEnabled) {
119119
return "";
120120
}
121121
boolean isError = queryParams.containsKey("error");
122122
boolean isLogoutSuccess = queryParams.containsKey("logout");
123-
return " <form class=\"form-signin\" method=\"post\" action=\"/login\">\n"
123+
return " <form class=\"form-signin\" method=\"post\" action=\"" + contextPath + "/login\">\n"
124124
+ " <h2 class=\"form-signin-heading\">Please sign in</h2>\n"
125125
+ createError(isError)
126126
+ createLogoutSuccess(isLogoutSuccess)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2020 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.Test;
20+
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
21+
import org.springframework.mock.web.server.MockServerWebExchange;
22+
import reactor.core.publisher.Mono;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
27+
public class LoginPageGeneratingWebFilterTests {
28+
29+
@Test
30+
public void filterWhenLoginWithContextPathThenActionContainsContextPath() throws Exception {
31+
LoginPageGeneratingWebFilter filter = new LoginPageGeneratingWebFilter();
32+
filter.setFormLoginEnabled(true);
33+
34+
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test/login").contextPath("/test"));
35+
36+
filter.filter(exchange, e -> Mono.empty()).block();
37+
38+
assertThat(exchange.getResponse().getBodyAsString().block()).contains("action=\"/test/login\"");
39+
}
40+
41+
@Test
42+
public void filterWhenLoginWithNoContextPathThenActionDoesNotContainsContextPath() throws Exception {
43+
LoginPageGeneratingWebFilter filter = new LoginPageGeneratingWebFilter();
44+
filter.setFormLoginEnabled(true);
45+
46+
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/login"));
47+
48+
filter.filter(exchange, e -> Mono.empty()).block();
49+
50+
assertThat(exchange.getResponse().getBodyAsString().block()).contains("action=\"/login\"");
51+
}
52+
}

0 commit comments

Comments
 (0)