6

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?

ChetoCovers
  • 61
  • 1
  • 2
  • 1
    Good question. In the JavaDocs it says ` @deprecated will move to Spring Cloud Openfeign in next major release.` but I can also not find it over there so I will keep using it as of now. – Ralf Aug 14 '20 at 07:25
  • 4
    As I still bet those Warnings and have also no good idea how to replace it I asked the Question directly at the spring-cloud-openfeign Github: https://github.com/spring-cloud/spring-cloud-openfeign/issues/417 – Ralf Oct 21 '20 at 13:09

2 Answers2

1

You can create your own RequestInterceptor to add the Authorization header.

There's an example here: https://developer.okta.com/blog/2018/02/13/secure-spring-microservices-with-oauth

slodj
  • 33
  • 6
0

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();
    }
Sergio Sánchez Sánchez
  • 1,694
  • 3
  • 28
  • 48