@WithMockUser populates test security-context with UsernamePasswordAuthenticationToken when your runtime security configuration sets security-context with JwtAuthenticationToken instances.
You should either use org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt MockMvc request post processor from spring-security-test:
this.mvc.perform(get("/").with(jwt()))...
Or @WithMockJwtAuth from spring-addons-oauth2-test, like I do in this pull-request.
P.S.
I am the author of both solutions (look at the @author tag of JwtRequestPostProcessor), but have a preference for the annotations, which enable to test @Controller, but also other kind of @Compenent (@Service, @Repository, ...) secured with @PreAuthorize or @PostFilter
@WithMockUser
populates test security-context withUsernamePasswordAuthenticationToken
when your runtime security configuration sets security-context withJwtAuthenticationToken
instances.You should either use
org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt
MockMvc request post processor fromspring-security-test
:Or
@WithMockJwtAuth
fromspring-addons-oauth2-test
, like I do in this pull-request.P.S. I am the author of both solutions (look at the
@author
tag ofJwtRequestPostProcessor
), but have a preference for the annotations, which enable to test@Controller
, but also other kind of@Compenent
(@Service
,@Repository
, ...) secured with@PreAuthorize
or@PostFilter