Package com.totvs.tjf.mock.test
Class JWTTestProvider
java.lang.Object
com.totvs.tjf.mock.test.JWTTestProvider
@Configuration
@ConditionalOnResource(resources="classpath:com/totvs/tjf/autoconfigure/WebSecurityAutoConfiguration.class")
public class JWTTestProvider
extends Object
Configures the the application tests context to use a generated key pair and
validate request using custom JWT decoder. This permits this class to
generate authentication tokens for use with
MockMvc tests.
Just need put this project on Maven dependencies with test scope, and use
MockMvc with() method to use the supplied oauth2JWT
RequestPostProcessor to generate and add the authorization header.
The token generated is automatically validated, so in the authentication
process the JWT token is accepted.- Author:
- Marcos Paulo
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringGenerate a JWT Token string.static StringGenerate a JWT Token string.static org.springframework.test.web.servlet.request.RequestPostProcessorStatic method to use withMockMvc
Will create a JWT Token and add the Authorization header inMockHttpServletRequest.static org.springframework.test.web.servlet.request.RequestPostProcessorStatic method to use withMockMvc
Will create a JWT Token and add the Authorization header inMockHttpServletRequest.static org.springframework.test.web.servlet.request.RequestPostProcessorStatic method to use withMockMvc
Will create a JWT Token and add the Authorization header inMockHttpServletRequest.
-
Field Details
-
EXPIRATIONTIME
public static final long EXPIRATIONTIME- See Also:
-
-
Constructor Details
-
JWTTestProvider
public JWTTestProvider()
-
-
Method Details
-
oauth2JWT
public static org.springframework.test.web.servlet.request.RequestPostProcessor oauth2JWT(String username, String roles, String tenantId) Static method to use withMockMvc
Will create a JWT Token and add the Authorization header inMockHttpServletRequest.
usage:mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/testapi").with(oauth2JWT("username", "ROLE1,ROLE2")) .contentType(MediaType.APPLICATION_JSON_VALUE).content("{}")).andExpect(status().is2xxSuccessful()); -
oauth2JWT
public static org.springframework.test.web.servlet.request.RequestPostProcessor oauth2JWT(String username, String roles) Static method to use withMockMvc
Will create a JWT Token and add the Authorization header inMockHttpServletRequest.
usage:mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/testapi").with(oauth2JWT("username", "ROLE1,ROLE2")) .contentType(MediaType.APPLICATION_JSON_VALUE).content("{}")).andExpect(status().is2xxSuccessful()); -
oauth2JWT
public static org.springframework.test.web.servlet.request.RequestPostProcessor oauth2JWT(String username) Static method to use withMockMvc
Will create a JWT Token and add the Authorization header inMockHttpServletRequest.
usage:mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/testapi").with(oauth2JWT("username")) .contentType(MediaType.APPLICATION_JSON_VALUE).content("{}")).andExpect(status().is2xxSuccessful());- Parameters:
username-Stringof user name available on security context- Returns:
RequestPostProcessorto be used withMockMvcobject
-
newRacJWT
Generate a JWT Token string.
Will create a JWT Token and return the JWT string. -
newRacJWT
Generate a JWT Token string.
Will create a JWT Token and return the JWT string.
-