SEC-3211: SecurityContextRequestPostProcessorSupport dirties context and causes side effects #3409
Labels
in: test
An issue in spring-security-test
type: bug
A general bug
type: jira
An issue that was migrated from JIRA
Milestone
Martin Bonato (Migrated from SEC-3211) said:
Using SecurityMockMvcRequestPostProcessors like
authentication()
,securityContext()
,testSecurityContext()
,user()
, etc. in unit tests might cause side effects on other tests in the same application context.SecurityMockMvcRequestPostProcessors.SecurityContextRequestPostProcessorSupport.save(SecurityContext, HttpServletRequest)
callsWebTestUtils.setSecurityContextRepository(HttpServletRequest, SecurityContextRepository)
to set aTestSecurityContextRepository
in theSecurityContextPersistenceFilter
registered in the application context. This dirties the context, because a registered bean (SecurityContextPersistenceFilter
) is modified such that it causes side effects.Since the side effects are rather rare and seem to show up randomly, it took me a while to track down the issue.
Here is an example of two unit tests where the first unit test influences the second:
If test2() is executed in its own application context it succeeds, since the default
HttpSessionSecurityContexRepository
used by theSecurityContextPersistenceFilter
does not store anonymous authentication tokens. However, if test2() is executed after test1() in the same context it fails, because @WithMocUser caused theTestSecurityContextRepository
to be registered which stores anonymous authentication tokens.The text was updated successfully, but these errors were encountered: