Class TestSecurityContextHolder


  • public final class TestSecurityContextHolder
    extends Object
    The TestSecurityContextHolder is very similar to SecurityContextHolder, but is necessary for testing. For example, we cannot populate the desired SecurityContext in SecurityContextHolder for web based testing. In a web request, the SecurityContextPersistenceFilter will override the SecurityContextHolder with the value returned by the SecurityContextRepository. At the end of the FilterChain the SecurityContextPersistenceFilter will clear out the SecurityContextHolder. This means if we make multiple web requests, we will not know which SecurityContext to use on subsequent requests. Typical usage is as follows:
    Since:
    4.0
    Author:
    Rob Winch
    • Method Detail

      • clearContext

        public static void clearContext()
        Clears the SecurityContext from TestSecurityContextHolder and SecurityContextHolder.
      • setContext

        public static void setContext​(org.springframework.security.core.context.SecurityContext context)
        Sets the SecurityContext on TestSecurityContextHolder and SecurityContextHolder.
        Parameters:
        context - the SecurityContext to use