Skip to content

Custom ServerHttpHeadersWriter to HeaderSpec #8028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3284,6 +3284,20 @@ public HeaderSpec frameOptions(Customizer<FrameOptionsSpec> frameOptionsCustomiz
return this;
}

/**
* Configures custom headers writer
*
* @param serverHttpHeadersWriter the {@link ServerHttpHeadersWriter} to provide custom headers writer
* @return the {@link HeaderSpec} to customize
* @since 5.3.0
* @author Ankur Pathak
*/
public HeaderSpec writer(ServerHttpHeadersWriter serverHttpHeadersWriter) {
Assert.notNull(serverHttpHeadersWriter, () -> "serverHttpHeadersWriter cannot be null");
this.writers.add(serverHttpHeadersWriter);
return this;
}

/**
* Configures the Strict Transport Security response headers
* @return the {@link HstsSpec} to configure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Mono;

import org.springframework.http.HttpHeaders;
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
Expand All @@ -46,9 +47,12 @@
*
* @author Rob Winch
* @author Vedran Pavic
* @author Ankur Pathak
* @since 5.0
*/
public class HeaderSpecTests {
private final static String CUSTOM_HEADER = "CUSTOM-HEADER";
private final static String CUSTOM_VALUE = "CUSTOM-VALUE";

private ServerHttpSecurity http = ServerHttpSecurity.http();

Expand Down Expand Up @@ -387,6 +391,20 @@ public void headersWhenReferrerPolicyCustomEnabledInLambdaThenCustomReferrerPoli
assertHeaders();
}

@Test
public void headersWhenCustomHeadersWriter() {
this.expectedHeaders.add(CUSTOM_HEADER, CUSTOM_VALUE);
this.http.headers(headers -> headers.writer(exchange -> {
return Mono.just(exchange)
.doOnNext(it -> {
it.getResponse().getHeaders().add(CUSTOM_HEADER, CUSTOM_VALUE);
}).then();

}));

assertHeaders();
}

private void expectHeaderNamesNotPresent(String... headerNames) {
for (String headerName : headerNames) {
this.expectedHeaders.remove(headerName);
Expand Down