Support defaultHtmlEscape in WebFlux

See gh-36400

Signed-off-by: husseinvr97 <husseinmustafabas05@gmail.com>
This commit is contained in:
husseinvr97
2026-03-10 19:14:41 +00:00
committed by rstoyanchev
parent 104a2033c5
commit d4893fb32f
11 changed files with 212 additions and 14 deletions
@@ -169,6 +169,15 @@ public interface ServerWebExchange {
*/
@Nullable ApplicationContext getApplicationContext();
/**
* Return the default HTML escape setting available for the current request,
* or {@code null} if no default was configured at the handler level.
* @return whether default HTML escaping is enabled, or {@code null} if not configured
* @since 7.0.6
* @see org.springframework.web.server.adapter.WebHttpHandlerBuilder#defaultHtmlEscape(boolean)
*/
@Nullable Boolean getDefaultHtmlEscape();
/**
* Returns {@code true} if the one of the {@code checkNotModified} methods
* in this contract were used and they returned true.
@@ -98,6 +98,11 @@ public class ServerWebExchangeDecorator implements ServerWebExchange {
return getDelegate().getApplicationContext();
}
@Override
public @Nullable Boolean getDefaultHtmlEscape() {
return getDelegate().getDefaultHtmlEscape();
}
@Override
public Mono<MultiValueMap<String, String>> getFormData() {
return getDelegate().getFormData();
@@ -101,6 +101,8 @@ public class DefaultServerWebExchange implements ServerWebExchange {
private final @Nullable ApplicationContext applicationContext;
private final @Nullable Boolean defaultHtmlEscape;
private volatile boolean notModified;
private Function<String, String> urlTransformer = url -> url;
@@ -114,12 +116,19 @@ public class DefaultServerWebExchange implements ServerWebExchange {
WebSessionManager sessionManager, ServerCodecConfigurer codecConfigurer,
LocaleContextResolver localeContextResolver) {
this(request, response, sessionManager, codecConfigurer, localeContextResolver, null);
this(request, response, sessionManager, codecConfigurer, localeContextResolver, null, null);
}
public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response,
WebSessionManager sessionManager, ServerCodecConfigurer codecConfigurer,
LocaleContextResolver localeContextResolver, @Nullable ApplicationContext applicationContext) {
this(request, response, sessionManager, codecConfigurer, localeContextResolver, applicationContext, null);
}
protected DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response,
WebSessionManager sessionManager, ServerCodecConfigurer codecConfigurer,
LocaleContextResolver localeContextResolver, @Nullable ApplicationContext applicationContext) {
LocaleContextResolver localeContextResolver, @Nullable ApplicationContext applicationContext, @Nullable Boolean defaultHtmlEscape) {
Assert.notNull(request, "'request' is required");
Assert.notNull(response, "'response' is required");
@@ -137,6 +146,7 @@ public class DefaultServerWebExchange implements ServerWebExchange {
this.formDataMono = initFormData(request, codecConfigurer, getLogPrefix());
this.multipartDataMono = initMultipartData(codecConfigurer, getLogPrefix());
this.applicationContext = applicationContext;
this.defaultHtmlEscape = defaultHtmlEscape;
if (request instanceof AbstractServerHttpRequest abstractServerHttpRequest) {
abstractServerHttpRequest.setAttributesSupplier(() -> this.attributes);
@@ -278,6 +288,11 @@ public class DefaultServerWebExchange implements ServerWebExchange {
return this.applicationContext;
}
@Override
public @Nullable Boolean getDefaultHtmlEscape() {
return this.defaultHtmlEscape;
}
@Override
public boolean isNotModified() {
return this.notModified;
@@ -99,6 +99,8 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa
private @Nullable ApplicationContext applicationContext;
private @Nullable Boolean defaultHtmlEscape;
/** Whether to log potentially sensitive info (form data at DEBUG, headers at TRACE). */
private boolean enableLoggingRequestDetails = false;
@@ -250,6 +252,26 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa
return this.applicationContext;
}
/**
* Configure a default HTML escape setting to apply to every
* {@link org.springframework.web.server.ServerWebExchange} created
* by this adapter.
* @param defaultHtmlEscape whether to enable default HTML escaping
* @since 7.0.6
*/
public void setDefaultHtmlEscape(Boolean defaultHtmlEscape) {
this.defaultHtmlEscape = defaultHtmlEscape;
}
/**
* Return the configured default HTML escape setting,
* or {@code null} if not configured.
* @since 7.0.6
*/
public @Nullable Boolean getDefaultHtmlEscape() {
return this.defaultHtmlEscape;
}
/**
* This method must be invoked after all properties have been set to
* complete initialization.
@@ -300,7 +322,7 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa
protected ServerWebExchange createExchange(ServerHttpRequest request, ServerHttpResponse response) {
return new DefaultServerWebExchange(request, response, this.sessionManager,
getCodecConfigurer(), getLocaleContextResolver(), this.applicationContext);
getCodecConfigurer(), getLocaleContextResolver(), this.applicationContext, this.defaultHtmlEscape);
}
/**
@@ -91,6 +91,8 @@ public final class WebHttpHandlerBuilder {
private final List<WebExceptionHandler> exceptionHandlers = new ArrayList<>();
private @Nullable Boolean defaultHtmlEscape;
private @Nullable Function<HttpHandler, HttpHandler> httpHandlerDecorator;
private @Nullable WebSessionManager sessionManager;
@@ -130,6 +132,7 @@ public final class WebHttpHandlerBuilder {
this.observationRegistry = other.observationRegistry;
this.observationConvention = other.observationConvention;
this.httpHandlerDecorator = other.httpHandlerDecorator;
this.defaultHtmlEscape = other.defaultHtmlEscape;
}
@@ -289,6 +292,26 @@ public final class WebHttpHandlerBuilder {
return (this.sessionManager != null);
}
/**
* Configure a default HTML escape setting to apply to the created
* {@link org.springframework.web.server.ServerWebExchange}.
* @param defaultHtmlEscape whether to enable default HTML escaping
* @return this builder
* @since 7.0.6
*/
public WebHttpHandlerBuilder defaultHtmlEscape(Boolean defaultHtmlEscape) {
this.defaultHtmlEscape = defaultHtmlEscape;
return this;
}
/**
* Return whether a default HTML escape setting has been configured.
* @since 7.0.6
*/
public boolean hasDefaultHtmlEscape() {
return (this.defaultHtmlEscape != null);
}
/**
* Configure the {@link ServerCodecConfigurer} to set on the {@code WebServerExchange}.
* @param codecConfigurer the codec configurer
@@ -424,6 +447,9 @@ public final class WebHttpHandlerBuilder {
if (this.applicationContext != null) {
adapted.setApplicationContext(this.applicationContext);
}
if(this.defaultHtmlEscape != null) {
adapted.setDefaultHtmlEscape(this.defaultHtmlEscape);
}
adapted.afterPropertiesSet();
return (this.httpHandlerDecorator != null ? this.httpHandlerDecorator.apply(adapted) : adapted);