Rapter1990/springbootmicroserviceswithsecurity

Issue in UserControllerTest : givenValidToken_whenGetAuthentication_thenReturnAuthentication() is throwing 401 error and we expect 200

madaffrager opened this issue · 2 comments

This issue happens due to the SecurityConfig :
.authorizeHttpRequests(customizer-> customizer
.requestMatchers(HttpMethod.POST, "/api/v1/users/**").permitAll()
.anyRequest().authenticated()
We permit POST requests on /api/v1/users although /api/v1/users/authenticate expects GET request with token in Param

Possible fix :

  • Add to the SecurityConfig:
    .requestMatchers(HttpMethod.GET, "/api/v1/users/authenticate").permitAll()
  • Add A valid AUTH header to the test:
    .header(HttpHeaders.AUTHORIZATION, "Bearer " + mockUserToken.getAccessToken())

FYI : this second fix means that we need to add an AUTHORIZATION HEADER to product-service in authenticate endpoint for UserServiceClient as well.

@madaffrager This test already works as you can see. That's why I close the issue.

image

Thanks for the review