Skip to content

Commit 577c68e

Browse files
author
Ankur Pathak
committed
Custom ServerHttpHeadersWriter to HeaderSpec
Add the ability to have a custom ServerHttpHeadersWriter to HeaderSpec Fixes gh-7636
1 parent 87ca714 commit 577c68e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3284,6 +3284,20 @@ public HeaderSpec frameOptions(Customizer<FrameOptionsSpec> frameOptionsCustomiz
32843284
return this;
32853285
}
32863286

3287+
/**
3288+
* Configures custom headers writer
3289+
*
3290+
* @param serverHttpHeadersWriter the {@link ServerHttpHeadersWriter} to provide custom headers writer
3291+
* @return the {@link HeaderSpec} to customize
3292+
* @since 5.3.0
3293+
* @author Ankur Pathak
3294+
*/
3295+
public HeaderSpec writer(ServerHttpHeadersWriter serverHttpHeadersWriter) {
3296+
Assert.notNull(serverHttpHeadersWriter, () -> "serverHttpHeadersWriter cannot be null");
3297+
this.writers.add(serverHttpHeadersWriter);
3298+
return this;
3299+
}
3300+
32873301
/**
32883302
* Configures the Strict Transport Security response headers
32893303
* @return the {@link HstsSpec} to configure

config/src/test/java/org/springframework/security/config/web/server/HeaderSpecTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.junit.Before;
2626
import org.junit.Test;
27+
import reactor.core.publisher.Mono;
2728

2829
import org.springframework.http.HttpHeaders;
2930
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
@@ -46,9 +47,12 @@
4647
*
4748
* @author Rob Winch
4849
* @author Vedran Pavic
50+
* @author Ankur Pathak
4951
* @since 5.0
5052
*/
5153
public class HeaderSpecTests {
54+
private final static String CUSTOM_HEADER = "CUSTOM-HEADER";
55+
private final static String CUSTOM_VALUE = "CUSTOM-VALUE";
5256

5357
private ServerHttpSecurity http = ServerHttpSecurity.http();
5458

@@ -387,6 +391,20 @@ public void headersWhenReferrerPolicyCustomEnabledInLambdaThenCustomReferrerPoli
387391
assertHeaders();
388392
}
389393

394+
@Test
395+
public void headersWhenCustomHeadersWriter() {
396+
this.expectedHeaders.add(CUSTOM_HEADER, CUSTOM_VALUE);
397+
this.http.headers(headers -> headers.writer(exchange -> {
398+
return Mono.just(exchange)
399+
.doOnNext(it -> {
400+
it.getResponse().getHeaders().add(CUSTOM_HEADER, CUSTOM_VALUE);
401+
}).then();
402+
403+
}));
404+
405+
assertHeaders();
406+
}
407+
390408
private void expectHeaderNamesNotPresent(String... headerNames) {
391409
for (String headerName : headerNames) {
392410
this.expectedHeaders.remove(headerName);

0 commit comments

Comments
 (0)