Class SecurityMockMvcRequestPostProcessors
MockMvc RequestPostProcessor implementations for Spring
Security.- Since:
- 4.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classPopulates a validCsrfTokeninto the request.static classstatic final classstatic final classstatic final classstatic final classstatic final classstatic final classCreates aUsernamePasswordAuthenticationTokenand sets the principal to be aUserand associates it to theMockHttpServletRequest. -
Method Summary
Modifier and TypeMethodDescriptionstatic org.springframework.test.web.servlet.request.RequestPostProcessorEstablish aSecurityContextthat uses anAnonymousAuthenticationToken.static org.springframework.test.web.servlet.request.RequestPostProcessorauthentication(Authentication authentication) Establish aSecurityContextthat uses the specifiedAuthenticationfor theAuthentication.getPrincipal()and a customUserDetails.csrf()Creates aRequestPostProcessorthat will automatically populate a validCsrfTokenin the request.digest()Creates a DigestRequestPostProcessor that enables easily adding digest based authentication to a request.Creates a DigestRequestPostProcessor that enables easily adding digest based authentication to a request.static org.springframework.test.web.servlet.request.RequestPostProcessorConvenience mechanism for setting the Authorization header to use HTTP Basic with the given username and password.jwt()Establish aSecurityContextthat has aJwtAuthenticationTokenfor theAuthenticationand aJwtfor theAuthentication.getPrincipal().Establish anOAuth2AuthorizedClientin the session.oauth2Client(String registrationId) Establish anOAuth2AuthorizedClientin the session.Establish aSecurityContextthat has aOAuth2AuthenticationTokenfor theAuthentication, aOAuth2Useras the principal, and aOAuth2AuthorizedClientin the session.Establish aSecurityContextthat has aOAuth2AuthenticationTokenfor theAuthentication, aOidcUseras the principal, and aOAuth2AuthorizedClientin the session.Establish aSecurityContextthat has aBearerTokenAuthenticationfor theAuthenticationand aOAuth2AuthenticatedPrincipalfor theAuthentication.getPrincipal().static org.springframework.test.web.servlet.request.RequestPostProcessorsecurityContext(SecurityContext securityContext) Establish the specifiedSecurityContextto be used.static org.springframework.test.web.servlet.request.RequestPostProcessorCreates aRequestPostProcessorthat can be used to ensure that the resulting request is ran with the user in theTestSecurityContextHolder.Establish aSecurityContextthat has aUsernamePasswordAuthenticationTokenfor theAuthentication.getPrincipal()and aUserfor theUsernamePasswordAuthenticationToken.getPrincipal().static org.springframework.test.web.servlet.request.RequestPostProcessoruser(UserDetails user) Establish aSecurityContextthat has aUsernamePasswordAuthenticationTokenfor theAuthentication.getPrincipal()and a customUserDetailsfor theUsernamePasswordAuthenticationToken.getPrincipal().static org.springframework.test.web.servlet.request.RequestPostProcessorFinds an X509Cetificate using a resoureName and populates it on the request.static org.springframework.test.web.servlet.request.RequestPostProcessorx509(X509Certificate... certificates) Populates the provided X509Certificate instances on the request.
-
Method Details
-
digest
Creates a DigestRequestPostProcessor that enables easily adding digest based authentication to a request.- Returns:
- the DigestRequestPostProcessor to use
-
digest
public static SecurityMockMvcRequestPostProcessors.DigestRequestPostProcessor digest(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(X509Certificate... certificates) Populates the provided X509Certificate instances on the request.- Parameters:
certificates- the X509Certificate instances to pouplate- Returns:
- the
RequestPostProcessorto use.
-
x509
public static org.springframework.test.web.servlet.request.RequestPostProcessor x509(String resourceName) throws IOException, CertificateException Finds an X509Cetificate using a resoureName and populates it on the request.- Parameters:
resourceName- the name of the X509Certificate resource- Returns:
- the
RequestPostProcessorto use. - Throws:
IOExceptionCertificateException
-
csrf
Creates aRequestPostProcessorthat will automatically populate a validCsrfTokenin the request.- Returns:
- the
SecurityMockMvcRequestPostProcessors.CsrfRequestPostProcessorfor further customizations.
-
testSecurityContext
public static org.springframework.test.web.servlet.request.RequestPostProcessor testSecurityContext()Creates aRequestPostProcessorthat can be used to ensure that the resulting request is ran with the user in theTestSecurityContextHolder.- Returns:
- the
RequestPostProcessorto use
-
user
Establish aSecurityContextthat has aUsernamePasswordAuthenticationTokenfor theAuthentication.getPrincipal()and aUserfor theUsernamePasswordAuthenticationToken.getPrincipal(). 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:
- Invoking apply
SecurityMockMvcConfigurers.springSecurity() - Adding Spring Security's FilterChainProxy to MockMvc
- Manually adding
SecurityContextPersistenceFilterto the MockMvc instance may make sense when using MockMvcBuilders standaloneSetup
- Parameters:
username- the username to populate- Returns:
- the
SecurityMockMvcRequestPostProcessors.UserRequestPostProcessorfor additional customization
- Invoking apply
-
user
public static org.springframework.test.web.servlet.request.RequestPostProcessor user(UserDetails user) Establish aSecurityContextthat has aUsernamePasswordAuthenticationTokenfor theAuthentication.getPrincipal()and a customUserDetailsfor theUsernamePasswordAuthenticationToken.getPrincipal(). 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:
- Invoking apply
SecurityMockMvcConfigurers.springSecurity() - Adding Spring Security's FilterChainProxy to MockMvc
- Manually adding
SecurityContextPersistenceFilterto the MockMvc instance may make sense when using MockMvcBuilders standaloneSetup
- Parameters:
user- the UserDetails to populate- Returns:
- the
RequestPostProcessorto use
- Invoking apply
-
jwt
Establish aSecurityContextthat has aJwtAuthenticationTokenfor theAuthenticationand aJwtfor theAuthentication.getPrincipal(). All details are declarative and do not require the JWT to be valid.The support works by associating the authentication 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:
- Invoking apply
SecurityMockMvcConfigurers.springSecurity() - Adding Spring Security's FilterChainProxy to MockMvc
- Manually adding
SecurityContextPersistenceFilterto the MockMvc instance may make sense when using MockMvcBuilders standaloneSetup
- Returns:
- the
SecurityMockMvcRequestPostProcessors.JwtRequestPostProcessorfor additional customization
- Invoking apply
-
opaqueToken
Establish aSecurityContextthat has aBearerTokenAuthenticationfor theAuthenticationand aOAuth2AuthenticatedPrincipalfor theAuthentication.getPrincipal(). All details are declarative and do not require the token to be validThe support works by associating the authentication 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:
- Invoking apply
SecurityMockMvcConfigurers.springSecurity() - Adding Spring Security's FilterChainProxy to MockMvc
- Manually adding
SecurityContextPersistenceFilterto the MockMvc instance may make sense when using MockMvcBuilders standaloneSetup
- Returns:
- the
SecurityMockMvcRequestPostProcessors.OpaqueTokenRequestPostProcessorfor additional customization - Since:
- 5.3
- Invoking apply
-
authentication
public static org.springframework.test.web.servlet.request.RequestPostProcessor authentication(Authentication authentication) Establish aSecurityContextthat uses the specifiedAuthenticationfor theAuthentication.getPrincipal()and a customUserDetails. 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:
- Invoking apply
SecurityMockMvcConfigurers.springSecurity() - Adding Spring Security's FilterChainProxy to MockMvc
- Manually adding
SecurityContextPersistenceFilterto the MockMvc instance may make sense when using MockMvcBuilders standaloneSetup
- Parameters:
authentication- the Authentication to populate- Returns:
- the
RequestPostProcessorto use
- Invoking apply
-
anonymous
public static org.springframework.test.web.servlet.request.RequestPostProcessor anonymous()Establish aSecurityContextthat uses anAnonymousAuthenticationToken. 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
RequestPostProcessorto use
-
securityContext
public static org.springframework.test.web.servlet.request.RequestPostProcessor securityContext(SecurityContext securityContext) Establish the specifiedSecurityContextto be used.This works by associating the user to the
HttpServletRequest. To associate the request to theSecurityContextHolderyou need to ensure that theSecurityContextPersistenceFilter(i.e. Spring Security's FilterChainProxy will typically do this) is associated with theMockMvcinstance. -
httpBasic
public static org.springframework.test.web.servlet.request.RequestPostProcessor httpBasic(String username, 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
RequestPostProcessorto use
-
oauth2Login
Establish aSecurityContextthat has aOAuth2AuthenticationTokenfor theAuthentication, aOAuth2Useras the principal, and aOAuth2AuthorizedClientin the session. All details are declarative and do not require associated tokens to be valid.The support works by associating the authentication 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:
- Invoking apply
SecurityMockMvcConfigurers.springSecurity() - Adding Spring Security's FilterChainProxy to MockMvc
- Manually adding
SecurityContextPersistenceFilterto the MockMvc instance may make sense when using MockMvcBuilders standaloneSetup
- Returns:
- the
SecurityMockMvcRequestPostProcessors.OidcLoginRequestPostProcessorfor additional customization - Since:
- 5.3
- Invoking apply
-
oidcLogin
Establish aSecurityContextthat has aOAuth2AuthenticationTokenfor theAuthentication, aOidcUseras the principal, and aOAuth2AuthorizedClientin the session. All details are declarative and do not require associated tokens to be valid.The support works by associating the authentication 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:
- Invoking apply
SecurityMockMvcConfigurers.springSecurity() - Adding Spring Security's FilterChainProxy to MockMvc
- Manually adding
SecurityContextPersistenceFilterto the MockMvc instance may make sense when using MockMvcBuilders standaloneSetup
- Returns:
- the
SecurityMockMvcRequestPostProcessors.OidcLoginRequestPostProcessorfor additional customization - Since:
- 5.3
- Invoking apply
-
oauth2Client
Establish anOAuth2AuthorizedClientin the session. All details are declarative and do not require associated tokens to be valid.The support works by associating the authorized client to the HttpServletRequest using an
OAuth2AuthorizedClientRepository- Returns:
- the
SecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessorfor additional customization - Since:
- 5.3
-
oauth2Client
public static SecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessor oauth2Client(String registrationId) Establish anOAuth2AuthorizedClientin the session. All details are declarative and do not require associated tokens to be valid.The support works by associating the authorized client to the HttpServletRequest using an
OAuth2AuthorizedClientRepository- Parameters:
registrationId- The registration id for theOAuth2AuthorizedClient- Returns:
- the
SecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessorfor additional customization - Since:
- 5.3
-