danvega / jwt

Spring Security + JWT
83 stars 37 forks source link

Fix tests #4

Closed ch4mpy closed 1 year ago

ch4mpy commented 1 year ago

@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

danvega commented 1 year ago

This is great! Thank you so much for not only the fix but teaching me something new today.