I tried to implement some controller test with the usage of jwt in Spring Boot Microservices.
When I run the test method shown below, I got this error
java.lang.NoClassDefFoundError: org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter
Caused by: java.lang.ClassNotFoundException: org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter
Here is the code snippets shown below.
@Test
public void test_WhenPlaceOrderWithWrongAccess_thenThrow403() throws Exception {
OrderRequest orderRequest = getMockOrderRequest();
MvcResult mvcResult
= mockMvc.perform(MockMvcRequestBuilders.post("/order/placeOrder")
.with(jwt().authorities(new SimpleGrantedAuthority("ADMIN"))) // HERE IS THE ERROR LINE
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(orderRequest))
).andExpect(MockMvcResultMatchers.status().isForbidden())
.andReturn();
}
Even if I added spring-security-oauth2-resource-server
dependency in pom.xml of order service, It didn't help me fix the issue.
How can I fix the issue?
Here is the link of example : Link
CodePudding user response:
Probably, you only need the dependencies
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
but not spring-security-oauth2-resource-server
CodePudding user response:
Double check that spring-security-oauth2-resource-server
dependency is correctly declared: scope compile
(must be there at runtime
and test
) and version 5.2 or above (JwtGrantedAuthoritiesConverter
was not there before). You might pull it as transitive dependency of spring-boot-starter-oauth2-resource-server
.
You might find really usefull to go through those tutorials. It covers in details resource-servers configuration and testing.