Manuel Gonçalves
Manuel Gonçalves

Reputation: 79

How to pass parameters to FeignRequestInterceptor?

I've got this code

@Component()
public class FeignRequestInterceptor {

    @Bean
    public RequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("username", "password");
    }
}

Is there a way I can pass username and password as parameter?

I've got a filter that intercepts a request, there I get some headers, and I want to use those headers to set the user and password so when I use the feign client later for another request, I've got those headers

Upvotes: 1

Views: 3171

Answers (1)

Manuel Gonçalves
Manuel Gonçalves

Reputation: 79

Guys I found this solutions, don't know if it's the best one, but it worked.

I delete the code from above and even the filter to itercept the request and I used this one

@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String token = "Basic " + Base64.getEncoder().encodeToString(
                (request.getParameter("username") + ":" + request.getParameter("password")).getBytes(Charsets.UTF_8));
        template.header("Authorization", token);
    }
}

Upvotes: 2

Related Questions