Class ContentSecurityPolicyNonceGeneratingFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
org.springframework.security.web.header.ContentSecurityPolicyNonceGeneratingFilter
All Implemented Interfaces:
jakarta.servlet.Filter, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.EnvironmentAware, org.springframework.core.env.EnvironmentCapable, org.springframework.web.context.ServletContextAware

public final class ContentSecurityPolicyNonceGeneratingFilter extends org.springframework.web.filter.OncePerRequestFilter
A filter which generates a nonce for Content Security Policy and sets it as a request attribute.

ContentSecurityPolicyHeaderWriter 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 request sees the same value.

Since:
7.2
  • Field Summary

    Fields inherited from class org.springframework.web.filter.OncePerRequestFilter

    ALREADY_FILTERED_SUFFIX

    Fields inherited from class org.springframework.web.filter.GenericFilterBean

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain)
     
    void
    setAttributeName(String attributeName)
    Set the name of the request attribute to generate.

    Methods inherited from class org.springframework.web.filter.OncePerRequestFilter

    doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatch

    Methods inherited from class org.springframework.web.filter.GenericFilterBean

    addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ContentSecurityPolicyNonceGeneratingFilter

      public ContentSecurityPolicyNonceGeneratingFilter()
      Creates a new instance.

      For each request, 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

    • doFilterInternal

      protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) throws jakarta.servlet.ServletException, IOException
      Specified by:
      doFilterInternal in class org.springframework.web.filter.OncePerRequestFilter
      Throws:
      jakarta.servlet.ServletException
      IOException
    • setAttributeName

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