For retrieving UserDetails object in spring security protected REST api with @AuthenticationPrincipal annotation, I add a customized filter in the MockMvc object, something likes:
@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class BaseSpringIntegrationTest {
@Autowired
private lateinit var context: WebApplicationContext
lateinit var mockMvc: MockMvc
val mockUser = UsernamePasswordAuthenticationToken(MyUserDetails("MOCK_USER"), "MOCK_PASSWORD", listOf(SimpleGrantedAuthority("MOCKED_ROLE")))
@PostConstruct
fun init() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply<DefaultMockMvcBuilder>(springSecurity(mockSpringSecurityFilter))
.build()
}
private val mockSpringSecurityFilter = object: Filter {
override fun doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain) {
with ((req as HttpServletRequest).userPrincipal) {
if (this != null)
SecurityContextHolder.getContext().authentication = this as Authentication
}
chain.doFilter(req, res)
}
override fun destroy() {
SecurityContextHolder.clearContext()
}
}
}
And then, I try to call the mockMvc object twice in the some test as follows:
class DummyTests: BaseSpringIntegrationTest() {
@Test
fun dummyTest() {
mockMvc.perform(MockMvcRequestBuilders.post("<MY_URL>").principal(mockUser))
.andExpect(MockMvcResultMatchers.status().`is`(HttpStatus.OK.value()))
mockMvc.perform(MockMvcRequestBuilders.post("<ANOTHER_URL>").principal(mockUser))
.andExpect(MockMvcResultMatchers.status().`is`(HttpStatus.OK.value()))
}
}
When running the test, the java.IllegalStateException is thrown for the second perform likes:
Method not found: BaseSpringIntegrationTest$mockSpringSecurityFilter$1.getFilters(org.springframework.mock.web.MockHttpServletRequest)
java.lang.IllegalStateException: Method not found: BaseSpringIntegrationTest$mockSpringSecurityFilter$1.getFilters(org.springframework.mock.web.MockHttpServletRequest)
at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:104)
at org.springframework.test.util.ReflectionTestUtils.invokeMethod(ReflectionTestUtils.java:502)
at org.springframework.test.util.ReflectionTestUtils.invokeMethod(ReflectionTestUtils.java:427)
at org.springframework.security.test.web.support.WebTestUtils.findFilter(WebTestUtils.java:121)
at org.springframework.security.test.web.support.WebTestUtils.getSecurityContextRepository(WebTestUtils.java:63)
at org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors$SecurityContextRequestPostProcessorSupport.save(SecurityMockMvcRequestPostProcessors.java:725)
at org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors$TestSecurityContextHolderPostProcessor.postProcessRequest(SecurityMockMvcRequestPostProcessors.java:805)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.postProcessRequest(MockHttpServletRequestBuilder.java:831)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:173)
And if there is only one perform call in a test, everything works properly.
Could anybody help to handle the problem please.