OAuth2FeignRequestInterceptor class deprecated in Spring Boot 2.3

Viewed 5111

In our last feign client security configuration we have this Bean:

    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor(
            ClientCredentialsResourceDetails oauth2RemoteResource) {
        return new OAuth2FeignRequestInterceptor(
                new DefaultOAuth2ClientContext(),
                oauth2RemoteResource
        );
    }

In 2.3 spring version OAuth2FeignRequestInterceptor is deprecated! But we cannot found the new one.

Anyone knows something about that?

2 Answers

I had the same problem, I needed a request interceptor to call through a Feign client to a another microservice.

The idea is very easy, The only thing that I needed to implement was a custom RequestInterceptor annonted with @Component that inject the current JWT from the security context to the Authorization Header.

You can view this component as follows:

@Component
@Slf4j
public class FeignClientInterceptor implements RequestInterceptor {

    private static final String AUTHORIZATION_HEADER = "Authorization";
    private static final String TOKEN_TYPE = "Bearer";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        log.debug("FeignClientInterceptor -> apply CALLED");
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication instanceof JwtAuthenticationToken) {
            final JwtAuthenticationToken jwtAuthToken = (JwtAuthenticationToken) authentication;
            requestTemplate.header(AUTHORIZATION_HEADER, String.format("%s %s", TOKEN_TYPE, jwtAuthToken.getToken().getTokenValue()));
        }
    }

}

Next, I can use the feign client successfully

    final APIResponse<ProcessedFileDTO> response = filesMetadataClient.getProcessedFileByName(uploadFile.getOriginalFilename());

    if (response.getStatus() == ResponseStatusEnum.ERROR
            && response.getHttpStatusCode() == HttpStatus.NOT_FOUND) {
        sendFileToSftp(uploadFile);
    } else {
        throw new FileAlreadyProcessedException();
    }
Related