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
    Modifier and Type
    Field
    Description
    static final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    newRacJWT(String username, String roles)
    Generate a JWT Token string.
    static String
    newRacJWT(String username, String roles, String tenantId)
    Generate a JWT Token string.
    static org.springframework.test.web.servlet.request.RequestPostProcessor
    oauth2JWT(String username)
    Static method to use with MockMvc

    Will create a JWT Token and add the Authorization header in MockHttpServletRequest.
    static org.springframework.test.web.servlet.request.RequestPostProcessor
    oauth2JWT(String username, String roles)
    Static method to use with MockMvc

    Will create a JWT Token and add the Authorization header in MockHttpServletRequest.
    static org.springframework.test.web.servlet.request.RequestPostProcessor
    oauth2JWT(String username, String roles, String tenantId)
    Static method to use with MockMvc

    Will create a JWT Token and add the Authorization header in MockHttpServletRequest.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • 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 with MockMvc

      Will create a JWT Token and add the Authorization header in MockHttpServletRequest.

      usage:
       mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/testapi").with(oauth2JWT("username", "ROLE1,ROLE2"))
                      .contentType(MediaType.APPLICATION_JSON_VALUE).content("{}")).andExpect(status().is2xxSuccessful());
       
      Parameters:
      username - String of user name available on security context
      roles - String with a list of comma separated roles
      tenantId - String with the current tenant identifier
      Returns:
      RequestPostProcessor to be used with MockMvc object
    • oauth2JWT

      public static org.springframework.test.web.servlet.request.RequestPostProcessor oauth2JWT(String username, String roles)
      Static method to use with MockMvc

      Will create a JWT Token and add the Authorization header in MockHttpServletRequest.

      usage:
       mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/testapi").with(oauth2JWT("username", "ROLE1,ROLE2"))
                      .contentType(MediaType.APPLICATION_JSON_VALUE).content("{}")).andExpect(status().is2xxSuccessful());
       
      Parameters:
      username - String of user name available on security context
      roles - String with a list of comma separated roles
      Returns:
      RequestPostProcessor to be used with MockMvc object
    • oauth2JWT

      public static org.springframework.test.web.servlet.request.RequestPostProcessor oauth2JWT(String username)
      Static method to use with MockMvc

      Will create a JWT Token and add the Authorization header in MockHttpServletRequest.

      usage:
       mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/testapi").with(oauth2JWT("username"))
                      .contentType(MediaType.APPLICATION_JSON_VALUE).content("{}")).andExpect(status().is2xxSuccessful());
       
      Parameters:
      username - String of user name available on security context
      Returns:
      RequestPostProcessor to be used with MockMvc object
    • newRacJWT

      public static String newRacJWT(String username, String roles)
      Generate a JWT Token string.
      Will create a JWT Token and return the JWT string.
      Parameters:
      username - String of user name available on security context
      roles - String with a list of comma separated roles
      Returns:
      String of the generated JWT
    • newRacJWT

      public static String newRacJWT(String username, String roles, String tenantId)
      Generate a JWT Token string.
      Will create a JWT Token and return the JWT string.
      Parameters:
      username - String of user name available on security context
      roles - String with a list of comma separated roles
      tenantId - String with the current tenant identifier
      Returns:
      String of the generated JWT