Class ContentSecurityPolicyNonceGeneratingWebFilter

java.lang.Object
org.springframework.security.web.server.header.ContentSecurityPolicyNonceGeneratingWebFilter
All Implemented Interfaces:
org.springframework.web.server.WebFilter

public final class ContentSecurityPolicyNonceGeneratingWebFilter extends Object implements org.springframework.web.server.WebFilter
A filter which generates a nonce for Content Security Policy and sets it as an exchange attribute.

ContentSecurityPolicyServerHttpHeadersWriter can use the attribute to write a nonce-based Content Security Policy header, and a view technology can render the nonce in generated HTML to allow intended inline <script> or <style> blocks.

The nonce is generated lazily, the first time ContentSecurityPolicyNonce.getNonce() is called, and every caller during the same exchange sees the same value.

Since:
7.2
  • Constructor Details

    • ContentSecurityPolicyNonceGeneratingWebFilter

      public ContentSecurityPolicyNonceGeneratingWebFilter()
      Creates a new instance.

      For each exchange, the created filter will generate a secure random nonce value with 128-bit entropy and encode it as a Base64 string without padding.

  • Method Details

    • filter

      public reactor.core.publisher.Mono<Void> filter(org.springframework.web.server.ServerWebExchange exchange, org.springframework.web.server.WebFilterChain chain)
      Specified by:
      filter in interface org.springframework.web.server.WebFilter
    • setAttributeName

      public void setAttributeName(String attributeName)
      Sets the name of the exchange attribute to generate.
      Parameters:
      attributeName - the name of the exchange attribute to generate
      Throws:
      IllegalArgumentException - if attributeName is null or empty string