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 Summary
Modifier and TypeMethodDescriptionauthentication(Object authentication) Authenticate using the given authentication instance.none()Explicitly state that you don't which to use any authentication in this request.Authenticate using the given principal.Authenticate using the given principal.principalWithCredentials(Object principal, Object credentials, String... authorities) Authenticate using the given principal and credentials.with(org.springframework.test.web.servlet.request.RequestPostProcessor requestPostProcessor, org.springframework.test.web.servlet.request.RequestPostProcessor... additionalRequestPostProcessor) Authenticate using aRequestPostProcessor.
-
Method Details
-
principal
Authenticate using the given principal. This method simply delegates toAbstractMockHttpServletRequestBuilder.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 aRequestPostProcessor. This is mainly useful when you have added thespring-security-testartifact to classpath. This allows you to do for example:given().auth().with(user("username").password("password")). ..whereuseris statically imported fromorg.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.- Parameters:
requestPostProcessor- The first request post processor to be used for authenticationadditionalRequestPostProcessor- Additional request post processors to be used for authentication- Returns:
- A
MockMvcAuthenticationSchemeinstance.
-
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 useauthorities- Optional list of authorities- Returns:
- The request specification
-
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 oforg.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
MockMvcRequestSpecification 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
-