Class SecurityMockMvcRequestPostProcessors


  • public final class SecurityMockMvcRequestPostProcessors
    extends java.lang.Object
    Contains MockMvc RequestPostProcessor implementations for Spring Security.
    Since:
    4.0
    • Method Detail

      • digest

        public static SecurityMockMvcRequestPostProcessors.DigestRequestPostProcessor digest​(java.lang.String username)
        Creates a DigestRequestPostProcessor that enables easily adding digest based authentication to a request.
        Parameters:
        username - the username to use
        Returns:
        the DigestRequestPostProcessor to use
      • x509

        public static org.springframework.test.web.servlet.request.RequestPostProcessor x509​(java.security.cert.X509Certificate... certificates)
        Populates the provided X509Certificate instances on the request.
        Parameters:
        certificates - the X509Certificate instances to pouplate
        Returns:
        the RequestPostProcessor to use.
      • x509

        public static org.springframework.test.web.servlet.request.RequestPostProcessor x509​(java.lang.String resourceName)
                                                                                      throws java.io.IOException,
                                                                                             java.security.cert.CertificateException
        Finds an X509Cetificate using a resoureName and populates it on the request.
        Parameters:
        resourceName - the name of the X509Certificate resource
        Returns:
        the RequestPostProcessor to use.
        Throws:
        java.io.IOException
        java.security.cert.CertificateException
      • testSecurityContext

        public static org.springframework.test.web.servlet.request.RequestPostProcessor testSecurityContext()
        Creates a RequestPostProcessor that can be used to ensure that the resulting request is ran with the user in the TestSecurityContextHolder.
        Returns:
        the RequestPostProcessor to sue
      • authentication

        public static org.springframework.test.web.servlet.request.RequestPostProcessor authentication​(Authentication authentication)
        Establish a SecurityContext that uses the specified Authentication for the Authentication.getPrincipal() and a custom UserDetails. All details are declarative and do not require that the user actually exists.

        The support works by associating the user to the HttpServletRequest. To associate the request to the SecurityContextHolder you need to ensure that the SecurityContextPersistenceFilter is associated with the MockMvc instance. A few ways to do this are:

        Parameters:
        authentication - the Authentication to populate
        Returns:
        the RequestPostProcessor to use
      • anonymous

        public static org.springframework.test.web.servlet.request.RequestPostProcessor anonymous()
        Establish a SecurityContext that uses an AnonymousAuthenticationToken. This is useful when a user wants to run a majority of tests as a specific user and wishes to override a few methods to be anonymous. For example:
         
         public class SecurityTests {
             @Before
             public void setup() {
                 mockMvc = MockMvcBuilders
                     .webAppContextSetup(context)
                     .defaultRequest(get("/").with(user("user")))
                     .build();
             }
        
             @Test
             public void anonymous() {
                 mockMvc.perform(get("anonymous").with(anonymous()));
             }
             // ... lots of tests ran with a default user ...
         }
          
        Returns:
        the RequestPostProcessor to use
      • securityContext

        public static org.springframework.test.web.servlet.request.RequestPostProcessor securityContext​(SecurityContext securityContext)
        Establish the specified SecurityContext to be used.

        This works by associating the user to the HttpServletRequest. To associate the request to the SecurityContextHolder you need to ensure that the SecurityContextPersistenceFilter (i.e. Spring Security's FilterChainProxy will typically do this) is associated with the MockMvc instance.

      • httpBasic

        public static org.springframework.test.web.servlet.request.RequestPostProcessor httpBasic​(java.lang.String username,
                                                                                                  java.lang.String password)
        Convenience mechanism for setting the Authorization header to use HTTP Basic with the given username and password. This method will automatically perform the necessary Base64 encoding.
        Parameters:
        username - the username to include in the Authorization header.
        password - the password to include in the Authorization header.
        Returns:
        the RequestPostProcessor to use