Whimusical
Whimusical

Reputation: 6649

@WithUserDetails and @WithMockUser when using springSecurity() in MockMVC with production authentication logic

After debugging for some time, I found that @WithUserDetails and @WithMockUser dummy the SecurityContext but don't add any fake Authentication Provider, so in case this is used with production code,

So.. is there any built-in way to use test convenient @With.. annotations over production auth code? Is it a bug? Can I change the order of previous bullet points so at least the testing preparation happens later?

Upvotes: 0

Views: 1549

Answers (1)

Filip Hanik VMware
Filip Hanik VMware

Reputation: 1622

Correct, the @WithUserDetails and @WithMockUser are intended when you wish to mock a request that is already authenticated.

So all the filters that do authentication, should not trigger at this point. If you're filters are triggering, then you can rewrite the filters to account for the use case when the authentication already has taken place.

Another option is to rewrite the test to do authentication the way your application expects it.

Upvotes: 2

Related Questions