Class ContentSecurityPolicyServerHttpHeadersWriter

java.lang.Object
org.springframework.security.web.server.header.ContentSecurityPolicyServerHttpHeadersWriter
All Implemented Interfaces:
ServerHttpHeadersWriter

public final class ContentSecurityPolicyServerHttpHeadersWriter extends Object implements ServerHttpHeadersWriter
Writes the Content-Security-Policy response header with configured policy directives.

With related directives specified, web clients could block inline <script> or <style> blocks in the HTML to mitigate XSS attacks injecting malicious inline blocks. To allow intended inline blocks, a CSP directive (usually script-src or style-src) may specify a hard-to-guess nonce matching the nonce attributes of inline HTML blocks.

To ease writing nonce-based CSP headers, this class replaces the {nonce} placeholder in the policyDirectives with a real nonce value read from a request attribute. A ContentSecurityPolicyNonceGeneratingWebFilter can be configured to generate a unique secure random ContentSecurityPolicyNonce for each request.

For example, if the configured policyDirectives is script-src 'self' 'nonce-{nonce}', and a ContentSecurityPolicyNonceGeneratingWebFilter has set a ContentSecurityPolicyNonce of "Nc3n83cnSAd3wc3Sasdfn9", then the written HTTP header value would be script-src 'self' 'nonce-Nc3n83cnSAd3wc3Sasdfn9'.

Since:
5.1
See Also:
  • Field Details

  • Constructor Details

    • ContentSecurityPolicyServerHttpHeadersWriter

      public ContentSecurityPolicyServerHttpHeadersWriter()
  • Method Details

    • writeHttpHeaders

      public reactor.core.publisher.Mono<Void> writeHttpHeaders(org.springframework.web.server.ServerWebExchange exchange)
      Description copied from interface: ServerHttpHeadersWriter
      Write the headers to the response.
      Specified by:
      writeHttpHeaders in interface ServerHttpHeadersWriter
      Parameters:
      exchange -
      Returns:
      A Mono which is returned to the Supplier of the ReactiveHttpOutputMessage.beforeCommit(Supplier).
    • setExchangeMatcher

      public void setExchangeMatcher(ServerWebExchangeMatcher exchangeMatcher)
      Sets the ServerWebExchangeMatcher which determines whether CSP should be written. The default is to write CSP unconditionally.
      Parameters:
      exchangeMatcher - the ServerWebExchangeMatcher to use
      Throws:
      IllegalArgumentException - if exchangeMatcher is null
      Since:
      7.2
    • setPolicyDirectives

      public void setPolicyDirectives(String policyDirectives)
      Set the policy directive(s) to be used in the response header. The policyDirectives may contain {nonce} as placeholders to be replaced.
      Parameters:
      policyDirectives - the policy directive(s)
      Throws:
      IllegalArgumentException - if policyDirectives is null or empty
    • setReportOnly

      public void setReportOnly(boolean reportOnly)
      Set whether to include the Content-Security-Policy-Report-Only header in the response. Otherwise, defaults to the Content-Security-Policy header.
      Parameters:
      reportOnly - whether to only report policy violations