Interface MockMvcAuthenticationSpecification

All Known Implementing Classes:
MockMvcRequestSpecificationImpl

public interface MockMvcAuthenticationSpecification
Specify an authentication scheme to use when sending a request. It's recommended to add the spring-security-test module to the classpath and use the with(RequestPostProcessor, RequestPostProcessor...) method for authentication and authorization.
  • Method Details

    • principal

      MockMvcRequestSpecification principal(Principal principal)
      Authenticate using the given principal. This method simply delegates to AbstractMockHttpServletRequestBuilder.principal(java.security.Principal).
      Parameters:
      principal - The principal to use.
      Returns:
      The request specification
      See Also:
      • AbstractMockHttpServletRequestBuilder.principal(java.security.Principal)
    • with

      MockMvcRequestSpecification with(org.springframework.test.web.servlet.request.RequestPostProcessor requestPostProcessor, org.springframework.test.web.servlet.request.RequestPostProcessor... additionalRequestPostProcessor)
      Authenticate using a RequestPostProcessor. This is mainly useful when you have added the spring-security-test artifact to classpath. This allows you to do for example:
       given().auth().with(user("username").password("password")). ..
       
      where user is statically imported from org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.
      Parameters:
      requestPostProcessor - The first request post processor to be used for authentication
      additionalRequestPostProcessor - Additional request post processors to be used for authentication
      Returns:
      A MockMvcAuthenticationScheme instance.
    • principal

      MockMvcRequestSpecification principal(Object principal)
      Authenticate using the given principal. The principal will be used like this:
       SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(principal, ""));
       

      Note that this method require Spring Security to be on the classpath. Also note that if you use this method Rest Assured will NOT be thread-safe.

      Parameters:
      principal - The principal to use.
      Returns:
      The request specification
    • principalWithCredentials

      MockMvcRequestSpecification principalWithCredentials(Object principal, Object credentials, String... authorities)
      Authenticate using the given principal and credentials. The principal and credentials will be used like this:
       SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(principal, credentials, authorities));
       

      Note that this method require Spring Security to be on the classpath. Also note that if you use this method Rest Assured will NOT be thread-safe.

      Parameters:
      principal - The principal to use.
      credentials - The credentials to use
      authorities - Optional list of authorities
      Returns:
      The request specification
    • authentication

      MockMvcRequestSpecification authentication(Object authentication)
      Authenticate using the given authentication instance. The authentication instance will be used like this:
       SecurityContextHolder.getContext().setAuthentication(authentication);
       
      This means that the supplied object must be an instance of org.springframework.security.core.Authentication.

      Note that this method require Spring Security to be on the classpath. Also note that if you use this method Rest Assured will NOT be thread-safe.

      Parameters:
      authentication - the spring security authentication to use.
      Returns:
      The request specification
    • none

      Explicitly state that you don't which to use any authentication in this request. This is useful only in cases where you've specified a default authentication scheme and you wish to override it for a single request.
      Returns:
      The request specification