1
0
mirror of synced 2026-08-05 09:47:05 +00:00

Serve static content (css, js) for reactive default UIs from DefaultResourcesWebFilter

This commit is contained in:
Daniel Garnier-Moiroux
2024-08-30 16:09:36 +02:00
committed by Rob Winch
parent 11616a1d78
commit 45d53973ab
4 changed files with 222 additions and 0 deletions
@@ -194,6 +194,7 @@ import org.springframework.security.web.server.savedrequest.ServerRequestCache;
import org.springframework.security.web.server.savedrequest.ServerRequestCacheWebFilter;
import org.springframework.security.web.server.savedrequest.WebSessionServerRequestCache;
import org.springframework.security.web.server.transport.HttpsRedirectWebFilter;
import org.springframework.security.web.server.ui.DefaultResourcesWebFilter;
import org.springframework.security.web.server.ui.LoginPageGeneratingWebFilter;
import org.springframework.security.web.server.ui.LogoutPageGeneratingWebFilter;
import org.springframework.security.web.server.util.matcher.AndServerWebExchangeMatcher;
@@ -2974,6 +2975,7 @@ public class ServerHttpSecurity {
}
if (loginPage != null) {
http.addFilterAt(loginPage, SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);
http.addFilterBefore(DefaultResourcesWebFilter.css(), SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);
if (http.logout != null) {
http.addFilterAt(new LogoutPageGeneratingWebFilter(),
SecurityWebFiltersOrder.LOGOUT_PAGE_GENERATING);
@@ -23,6 +23,7 @@ import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.http.HttpHeaders;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -738,6 +739,40 @@ public class ServerHttpSecurityTests {
.isSameAs(authorizationRedirectStrategy);
}
@Test
void resourcesWhenLoginPageConfiguredThenServesCss() {
this.http.formLogin(withDefaults());
this.http.authenticationManager(this.authenticationManager);
WebTestClient client = WebTestClientBuilder
.bindToControllerAndWebFilters(NotFoundController.class, this.http.build())
.build();
client.get()
.uri("/default-ui.css")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.value(Matchers.containsString("body {"));
}
@Test
void resourcesWhenLoginPageNotConfiguredThenDoesNotServeCss() {
this.http.httpBasic(withDefaults());
this.http.authenticationManager(this.authenticationManager);
WebTestClient client = WebTestClientBuilder
.bindToControllerAndWebFilters(NotFoundController.class, this.http.build())
.build();
client.get()
.uri("/default-ui.css")
.exchange()
.expectStatus()
.isNotFound()
.expectBody(String.class)
.isEqualTo(null);
}
private boolean isX509Filter(WebFilter filter) {
try {
Object converter = ReflectionTestUtils.getField(filter, "authenticationConverter");
@@ -775,6 +810,13 @@ public class ServerHttpSecurityTests {
}
@RestController
private static class NotFoundController {
// Empty controller, makes WebTestClient return HTTP 404
}
private static class TestWebFilter implements WebFilter {
@Override