Is NativeWebRequest available in @Configuration class

59 views Asked by At

I'm implementing JWT-token interception and bypassing from incoming REST request into request executed by Feign client. The issue I ran into is about injection of NativeWebRequest: I can reach it within @Service-annotated class, but not from @Configuration.

This code is fine:

@FeignClient(name = "client", url = "${url}")
public interface AccountClient {
  @GetMapping
  ResponseEntity<Accounts> getAccounts(@RequestParam("customerXRef") String customerXRef, @RequestHeader(HttpHeaders.AUTHORIZATION) String bearerToken);
}

@Service
public class AccountInformationService{
    private final AccountClient accountClient;
    private final NativeWebRequest request;

    public Accounts getAccounts(AccountDetailsRequestParams accountDetailsRequestParams) {
        var customerXRef = accountDetailsRequestParams.getCustomerXRefID();

        var response = accountClient.getAccounts(customerXRef, request.getHeader(HttpHeaders.AUTHORIZATION));

        return accountFilteringHandler.filter(accountDetailsRequestParams, response.getBody());
    }
}

But as soon as I reafactor it to

@Configuration
public class InternalAccountFeignConfiguration {
    @Bean
    RequestInterceptor authInterceptor(@Lazy NativeWebRequest request) {
        return template -> template.header(HttpHeaders.AUTHORIZATION, request.getHeader(HttpHeaders.AUTHORIZATION));
    }
}

@FeignClient(name = "client", url = "${url}", configuration = InternalAccountFeignConfiguration.class)
public interface AccountClient {
    @GetMapping
    ResponseEntity<Accounts> getAccounts(@RequestParam("customerXRef") String customerXRef);
}

@Service
public class AccountInformationService{
    private final AccountClient accountClient;

    public Accounts getAccounts(AccountDetailsRequestParams accountDetailsRequestParams) {
        var customerXRef = accountDetailsRequestParams.getCustomerXRefID();

        var response = accountClient.getAccounts(customerXRef);

        return accountFilteringHandler.filter(accountDetailsRequestParams, response.getBody());
    }
}

it's not working any more. At runtime in the log I see

Resolved org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'j.s.h.HttpServletRequest' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@o.s.c.a.Lazy(true)}

Am I missing something here?

0

There are 0 answers