Class SecurityMockMvcRequestPostProcessors
- java.lang.Object
-
- org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors
-
public final class SecurityMockMvcRequestPostProcessors extends java.lang.ObjectContainsMockMvcRequestPostProcessorimplementations for Spring Security.- Since:
- 4.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSecurityMockMvcRequestPostProcessors.CsrfRequestPostProcessorPopulates a validCsrfTokeninto the request.static classSecurityMockMvcRequestPostProcessors.DigestRequestPostProcessorstatic classSecurityMockMvcRequestPostProcessors.JwtRequestPostProcessorstatic classSecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessorstatic classSecurityMockMvcRequestPostProcessors.OAuth2LoginRequestPostProcessorstatic classSecurityMockMvcRequestPostProcessors.OidcLoginRequestPostProcessorstatic classSecurityMockMvcRequestPostProcessors.OpaqueTokenRequestPostProcessorstatic classSecurityMockMvcRequestPostProcessors.UserRequestPostProcessorCreates aUsernamePasswordAuthenticationTokenand sets the principal to be aUserand associates it to theMockHttpServletRequest.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.springframework.test.web.servlet.request.RequestPostProcessoranonymous()Establish aSecurityContextthat uses anAnonymousAuthenticationToken.static org.springframework.test.web.servlet.request.RequestPostProcessorauthentication(Authentication authentication)Establish aSecurityContextthat uses the specifiedAuthenticationfor theAuthentication.getPrincipal()and a customUserDetails.static SecurityMockMvcRequestPostProcessors.CsrfRequestPostProcessorcsrf()Creates aRequestPostProcessorthat will automatically populate a validCsrfTokenin the request.static SecurityMockMvcRequestPostProcessors.DigestRequestPostProcessordigest()Creates a DigestRequestPostProcessor that enables easily adding digest based authentication to a request.static SecurityMockMvcRequestPostProcessors.DigestRequestPostProcessordigest(java.lang.String username)Creates a DigestRequestPostProcessor that enables easily adding digest based authentication to a request.static org.springframework.test.web.servlet.request.RequestPostProcessorhttpBasic(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.static SecurityMockMvcRequestPostProcessors.JwtRequestPostProcessorjwt()Establish aSecurityContextthat has aJwtAuthenticationTokenfor theAuthenticationand aJwtfor theAuthentication.getPrincipal().static SecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessoroauth2Client()Establish anOAuth2AuthorizedClientin the session.static SecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessoroauth2Client(java.lang.String registrationId)Establish anOAuth2AuthorizedClientin the session.static SecurityMockMvcRequestPostProcessors.OAuth2LoginRequestPostProcessoroauth2Login()Establish aSecurityContextthat has aOAuth2AuthenticationTokenfor theAuthentication, aOAuth2Useras the principal, and aOAuth2AuthorizedClientin the session.static SecurityMockMvcRequestPostProcessors.OidcLoginRequestPostProcessoroidcLogin()Establish aSecurityContextthat has aOAuth2AuthenticationTokenfor theAuthentication, aOidcUseras the principal, and aOAuth2AuthorizedClientin the session.static SecurityMockMvcRequestPostProcessors.OpaqueTokenRequestPostProcessoropaqueToken()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.RequestPostProcessortestSecurityContext()Creates aRequestPostProcessorthat can be used to ensure that the resulting request is ran with the user in theTestSecurityContextHolder.static SecurityMockMvcRequestPostProcessors.UserRequestPostProcessoruser(java.lang.String username)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.RequestPostProcessorx509(java.lang.String resourceName)Finds an X509Cetificate using a resoureName and populates it on the request.static org.springframework.test.web.servlet.request.RequestPostProcessorx509(java.security.cert.X509Certificate... certificates)Populates the provided X509Certificate instances on the request.
-
-
-
Method Detail
-
digest
public static SecurityMockMvcRequestPostProcessors.DigestRequestPostProcessor 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(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
RequestPostProcessorto use.
-
x509
public static org.springframework.test.web.servlet.request.RequestPostProcessor x509(java.lang.String resourceName) throws java.io.IOException, java.security.cert.CertificateExceptionFinds an X509Cetificate using a resoureName and populates it on the request.- Parameters:
resourceName- the name of the X509Certificate resource- Returns:
- the
RequestPostProcessorto use. - Throws:
java.io.IOExceptionjava.security.cert.CertificateException
-
csrf
public static SecurityMockMvcRequestPostProcessors.CsrfRequestPostProcessor 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 sue
-
user
public static SecurityMockMvcRequestPostProcessors.UserRequestPostProcessor user(java.lang.String username)
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
public static SecurityMockMvcRequestPostProcessors.JwtRequestPostProcessor 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
public static SecurityMockMvcRequestPostProcessors.OpaqueTokenRequestPostProcessor 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(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
RequestPostProcessorto use
-
oauth2Login
public static SecurityMockMvcRequestPostProcessors.OAuth2LoginRequestPostProcessor 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
public static SecurityMockMvcRequestPostProcessors.OidcLoginRequestPostProcessor 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
public static SecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessor 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 via the
HttpSessionOAuth2AuthorizedClientRepository- Returns:
- the
SecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessorfor additional customization - Since:
- 5.3
-
oauth2Client
public static SecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessor oauth2Client(java.lang.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 via the
HttpSessionOAuth2AuthorizedClientRepository- Parameters:
registrationId- The registration id for theOAuth2AuthorizedClient- Returns:
- the
SecurityMockMvcRequestPostProcessors.OAuth2ClientRequestPostProcessorfor additional customization - Since:
- 5.3
-
-