I have a test that tests groups within claims from jsonwebtoken. On SpringBoot version 2.7.15 it works fine, but after updating to 3.1.4 my test behaves differently. I'am using Jjwt library for creating jwt-tokens:
implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
One Part of my Test is, to retrieve Claims from SecurityContextHolder:
@Configuration
public class JwtClaims {
@Bean
@RequestScope
public Claims claims() {
Object details = SecurityContextHolder.getContext().getAuthentication().getDetails();
if (details instanceof Claims) {
return (Claims) details;
} else {
log.error("Error"); //i get this error, that is not instance of Claims
}
}
}
In Version 2.7.15 it was type of Claims, but with Version 3.1.4 it is type of
class org.springframework.security.web.authentication.WebAuthenticationDetails
Here is my Test:
import io.jsonwebtoken.Claims;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static com.me.JwtRequestPostProcessor.jwt;
import static com.me.security.JwtTestTokenFactory.ApiManager.withGroups;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
class JwtClaimsProviderTest {
@BeforeAll
static void startServer() {
MockServerUtil.startMockServer(6691);
}
@RestController
@RequestMapping("/dummy")
public static class DummyController {
@Autowired
Claims claims;
@GetMapping
Claims dummy() {
return claims;
}
}
@Nested
@SpringBootTest
@Import(DummyController.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@ActiveProfiles("test")
@AutoConfigureMockMvc
@ImportAutoConfiguration
class IntegrationTest {
@Autowired
MockMvc mvc;
@Test
void claimsAreRequestedScoped() throws Exception {
MvcResult mvcResult = mvc.perform(get("/dummy")
.with(jwt(withGroups("my_jwt", "your_jwt")))) //here i'am building jwt-token and setting him to authorization-header
.andExpect(MockMvcResultMatchers.jsonPath("$.groups").value(hasItem("my_jwt"))).andReturn();
String contentAsString = mvcResult.getResponse().getContentAsString();
}
}
}
The Error is, that "$.groups" are not present. Does has anybody ideas or useful links, how it could be implemented on new SpringBoot3?