Package com.totvs.tjf.mock.test
Class JWTTestProvider
- java.lang.Object
-
- org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter
-
- com.totvs.tjf.mock.test.JWTTestProvider
-
- All Implemented Interfaces:
org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurer
@Configuration @ConditionalOnResource(resources="classpath:com/totvs/tjf/autoconfigure/WebSecurityAutoConfiguration.class") public class JWTTestProvider extends org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter
Configures the the application tests context to use aInMemoryTokenStore
. This permits this class to generate authentication tokens for use withMockMvc
tests. Just need put this project on Maven dependencies with test scope, and useMockMvc
with() method to use the supplied oauth2JWTRequestPostProcessor
to generate and add the authorization header. The token generated is automatically inserted on theInMemoryTokenStore
, so in the authentication process the JWT token is accepted.- Author:
- Mauricio Rogerio Obenaus
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
JWTTestProvider.TestAuthentication
Test class extending the AbstractAuthenticationToken to use with the generated token.
-
Constructor Summary
Constructors Constructor Description JWTTestProvider()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
configure(org.springframework.security.config.annotation.web.builders.HttpSecurity http)
void
configure(org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer resources)
Configure the Oauth2 Resource Server to use theInMemoryTokenStore
String
newRacJWT(String username, String roles)
Generate a JWT Token string.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 withMockMvc
Will create a JWT Token, store onInMemoryTokenStore
and add the Authorization header inMockHttpServletRequest
.static org.springframework.test.web.servlet.request.RequestPostProcessor
oauth2JWT(String username, String roles)
Static method to use withMockMvc
Will create a JWT Token, store onInMemoryTokenStore
and add the Authorization header inMockHttpServletRequest
.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, store onInMemoryTokenStore
and add the Authorization header inMockHttpServletRequest
.
-
-
-
Method Detail
-
configure
public void configure(org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer resources) throws Exception
Configure the Oauth2 Resource Server to use theInMemoryTokenStore
- Specified by:
configure
in interfaceorg.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurer
- Overrides:
configure
in classorg.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter
- Throws:
Exception
-
configure
public void configure(org.springframework.security.config.annotation.web.builders.HttpSecurity http) throws Exception
- Specified by:
configure
in interfaceorg.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurer
- Overrides:
configure
in classorg.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter
- Throws:
Exception
-
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, store onInMemoryTokenStore
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, store onInMemoryTokenStore
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, store onInMemoryTokenStore
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
-String
of user name available on security context- Returns:
RequestPostProcessor
to be used withMockMvc
object
-
newRacJWT
public String newRacJWT(String username, String roles)
Generate a JWT Token string.
Will create a JWT Token, store onInMemoryTokenStore
and return the JWT string.
The token is not signed, no need because is not validated when usingInMemoryTokenStore
-
-