Spring Boot microservices with JWT (Cannot run Integration test (401 Unauthorized Error))

I try to implement an example of Spring Boot microservices with JWT. I have some problem. I cannot run all integration tests of product service even if I defined bearer token in terms of admin and user for its relevant endpoints. I got 401 Authorized error for all tests. How can I fix it?

Here is the repo : Link

Here is the security config of product service.

    @Configuration
    @EnableWebSecurity
    @RequiredArgsConstructor
    @EnableMethodSecurity
    public class SecurityConfig {
    
        private final UserServiceClient userServiceClient;
    
        @Bean
        protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
            return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
        }
    
        @Bean
        public SecurityFilterChain filterChain(
                final HttpSecurity httpSecurity,
                final CustomBearerTokenAuthenticationFilter customBearerTokenAuthenticationFilter,
                final CustomAuthenticationEntryPoint customAuthenticationEntryPoint
        ) throws Exception {
    
            httpSecurity
                    .exceptionHandling(customizer -> customizer.authenticationEntryPoint(customAuthenticationEntryPoint))
                    .cors(customizer -> customizer.configurationSource(corsConfigurationSource()))
                    .csrf(AbstractHttpConfigurer::disable)
                    .authorizeHttpRequests(customizer -> customizer
                            .anyRequest().authenticated()
                    )
                    .sessionManagement(customizer -> customizer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                    .addFilterBefore(customBearerTokenAuthenticationFilter, BearerTokenAuthenticationFilter.class);
    
            return httpSecurity.build();
        }
    
        private CorsConfigurationSource corsConfigurationSource() {
            CorsConfiguration configuration = new CorsConfiguration();
            configuration.setAllowedOrigins(List.of("*"));
            configuration.setAllowedMethods(List.of("*"));
            configuration.setAllowedHeaders(List.of("*"));
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", configuration);
            return source;
        }
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
    }

Here is the test method of integration test

    @Test
        void givenProductPagingRequest_whenGetProductsFromAdmin_thenReturnCustomPageProduct() throws Exception {
    
            // Given
            ProductPagingRequest pagingRequest = ProductPagingRequest.builder()
                    .pagination(
                            CustomPaging.builder()
                                    .pageSize(1)
                                    .pageNumber(1)
                                    .build()
                    ).build();
    
            String productId = UUID.randomUUID().toString();
    
            ProductEntity expected = ProductEntity.builder()
                    .id(productId)
                    .name("Test Product")
                    .unitPrice(BigDecimal.valueOf(12))
                    .amount(BigDecimal.valueOf(5))
                    .build();
    
            List<ProductEntity> productEntities = new ArrayList<>();
            productEntities.addAll(Collections.singletonList(expected));
    
            Page<ProductEntity> productEntityPage = new PageImpl<>(productEntities, PageRequest.of(1, 1), productEntities.size());
    
            List<Product> productDomainModels = productEntities.stream()
                    .map(entity -> new Product(entity.getId(), entity.getName(), entity.getAmount(),entity.getUnitPrice()))
                    .collect(Collectors.toList());
    
            CustomPage<Product> productPage = CustomPage.of(productDomainModels, productEntityPage);
    
            // When
            when(productReadService.getProducts(any(ProductPagingRequest.class))).thenReturn(productPage);
    
            // Then
            mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/products")
                            .contentType(MediaType.APPLICATION_JSON)
                            .content(objectMapper.writeValueAsString(pagingRequest))
                            .header(HttpHeaders.AUTHORIZATION, "Bearer " + mockAdminToken.getAccessToken()))
                    .andDo(MockMvcResultHandlers.print())
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.jsonPath("$.httpStatus").value("OK"))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.isSuccess").value(true))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.response.content[0].id").value(expected.getId()))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.response.content[0].name").value(expected.getName()))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.response.content[0].amount").value(expected.getAmount()))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.response.content[0].unitPrice").value(expected.getUnitPrice()));
    
            // Verify
            verify(productReadService, times(1)).getProducts(any(ProductPagingRequest.class));
    
        }

Here is CustomBearerTokenAuthenticationFilter shown below

@Slf4j
@Component
@RequiredArgsConstructor
public class CustomBearerTokenAuthenticationFilter extends OncePerRequestFilter {

    private final UserServiceClient userServiceClient;

    @Override
    protected void doFilterInternal(@NonNull final HttpServletRequest httpServletRequest,
                                    @NonNull final HttpServletResponse httpServletResponse,
                                    @NonNull final FilterChain filterChain) throws ServletException, IOException {

        log.debug("API Request was secured with Security!");

        final String authorizationHeader = httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION);

        if (Token.isBearerToken(authorizationHeader)) {
            final String jwt = Token.getJwt(authorizationHeader);
            userServiceClient.validateToken(jwt);
        }

        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }
}

Here is the screenshot of any test result shown below

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /api/v1/products
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8", Authorization:"Bearer eyJ0eXAiOiJCZWFyZXIiLCJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJmOGM3M2JhNy0zNDU2LTQ4NDgtOTFiYy1iN2E3OWM2M2E5ODciLCJpc3MiOiJJU1NVRVIiLCJpYXQiOjE3MjExNjE5MjYsImV4cCI6MTcyMTE2MzcyNiwidXNlclN0YXR1cyI6IkFDVElWRSIsInVzZXJMYXN0TmFtZSI6IkRvZSIsInVzZXJQaG9uZU51bWJlciI6IjEyMzQ1Njc4OTAxMDExIiwidXNlckVtYWlsIjoidXNlcmFkbWluQGV4YW1wbGUuY29tIiwidXNlclR5cGUiOiJVU0VSIiwidXNlckZpcnN0TmFtZSI6IkpvaG4iLCJ1c2VySWQiOiJjZTJkOGI2Yi0wZGVlLTRlNGYtODdjOS05ZTRkY2Y4ZDI5OGUifQ.SH5mUFw59Ux2HX6VCIeIifslZFx1RQSTzT1R_zgNbWX1K5vngoAkzFP4kjrOUgS8tqJnBuzY98t5bCZA74L0vuZkNibDdI7Pc8HwHL3k2H2x6vtGPIC0sEJOVWPiNu7Lgb0XF77xp0_KEKw_UkIwfgYY-CCKL-fcAKBwf4z5QY26rtgXxrHn8Ajmh9DCpya9_LnEcplLfcxRWFWmkN2IL8OsklO5EtSSRo14uaKb7ZE4J3lV57ZJG1ADmYfDFO_nJBNFmwSpaUa1VM_6AB1vOTiv4OliVhbA6PQzrQ7xeIGlaAinrV1AoZfOQIFO-rkkkwYd2D91ymTCVEpBrk60Cg", Content-Length:"44"]
             Body = {"pagination":{"pageNumber":0,"pageSize":1}}
    Session Attrs = {}

Handler:
             Type = null

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 401
    Error message = null
          Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", Content-Type:"application/json", X-Content-Type-Options:"nosniff", X-XSS-Protection:"0", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
     Content type = application/json
             Body = {"time":"2024-07-16T23:32:07.4830196","httpStatus":"UNAUTHORIZED","header":"AUTH ERROR","isSuccess":false}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Status expected:<200> but was:<401>
Expected :200
Actual   :401

15

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật