如何在Feign客户端中添加请求拦截器?

20

我希望每次通过Feign客户端发出请求时,都能够使用我的已验证用户设置特定的标头。

这是我的过滤器,从中获取身份验证信息并将其设置为Spring安全上下文:

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class PerformanceApplication {
    @Bean
    public Filter requestDetailsFilter() {
        return new RequestDetailsFilter();
    }

    public static void main(String[] args) {
        SpringApplication.run(PerformanceApplication.class, args);
    }

    private class RequestDetailsFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {

        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            String userName = ((HttpServletRequest)servletRequest).getHeader("Z-User-Details");
            String pass = ((HttpServletRequest)servletRequest).getHeader("X-User-Details");
            if (pass != null)
                pass = decrypt(pass);
            SecurityContext secure = new SecurityContextImpl();
            org.springframework.security.core.Authentication token = new UsernamePasswordAuthenticationToken(userName, pass);
            secure. setAuthentication(token);
            SecurityContextHolder.setContext(secure);
            filterChain.doFilter(servletRequest, servletResponse);
        }

        @Override
        public void destroy() {

        }
    }
    private String decrypt(String str) {
        try {
            Cipher dcipher = new NullCipher();

            // Decode base64 to get bytes
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);

            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }
}

这是我的Feign客户端:

@FeignClient("holiday-client")
public interface EmailClient {
    @RequestMapping(value = "/api/email/send", method = RequestMethod.POST)
    void sendEmail(@RequestBody Email email);
}

我这里有一个请求拦截器:

@Component
public class FeignRequestInterceptor implements RequestInterceptor {
    private String headerValue;

    public FeignRequestInterceptor() {
    }

    public FeignRequestInterceptor(String username, String password) {
        this(username, password, ISO_8859_1);
    }

    public FeignRequestInterceptor(String username, String password, Charset charset) {
        checkNotNull(username, "username");
        checkNotNull(password, "password");
        this.headerValue = "Basic " + base64encode((username + ":" + password).getBytes(charset));
    }

    private static String base64encode(byte[] bytes) {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }

    @Override
    public void apply(RequestTemplate requestTemplate) {
        requestTemplate.header("Authorization", headerValue);
    }
}

我不知道如何为我的客户端配置这个拦截器,并设置带有用户名和密码的标头。我该如何完成?

2个回答

23

你其实不需要自己实现FeignRequestInterceptor,因为在feign.auth包中已经有BasicAuthRequestInterceptor,它完全可以胜任同样的工作。

话虽如此,你基本上已经准备好了所有东西。剩下要做的就是定义具有特定用户名和密码的basicAuthRequestInterceptor bean:

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

有没有可能为 RequestInterceptor @Bean 添加 @Qualifier? - hellboy
当然可以。但是Feign不知道自定义限定符。如果您需要在RequestInterceptors之间切换,则需要引入一些自定义的Feign配置。 - Aleksandr Erokhin

7

我知道这个帖子有点旧,但想在这里解释一下正在发生的事情。

如果您想自定义Feign请求,可以使用RequestInterceptor。这可以是自定义实现,也可以重用Feign库中提供的内容,例如BasicAuthRequestInterceptor

如何注册?这取决于您如何使用Feign,有两种方式。

如果您正在使用普通的Feign而没有使用Spring,则必须将拦截器设置为Feign构建器。一个示例在这里

Feign.builder()
     .requestInterceptor(new MyCustomInterceptor())
     .target(MyClient.class, "http://localhost:8081");

如果你正在使用Spring Cloud OpenFeign,并且你使用@FeignClient注解来构建客户端,那么你必须通过将它定义为@Component或在一个@Configuration类中定义为@Bean来创建一个来自RequestInterceptor的bean。例子在这里
@Component
public class MyCustomInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        // do something
    }
}

另外,你可以查看我的一篇文章,也许能更好地澄清这个话题:使用Spring Cloud Feign自定义每个请求


2
我正在使用Spring Cloud OpenFeign,并使用@FeignClient注释。有没有一种方法只将请求拦截器添加到特定的feign客户端,或者唯一的方法是使用Feign.builder().requestInterceptor? - BoomShaka
4
@BoomShaka,@FeignClient注解提供了一个configuration属性,您可以在其中指定要为该特定feign client选择哪个配置类。在您的情况下,创建一个包含请求拦截器定义为Spring Bean的配置类,并将该配置应用于需要它的@FeignClient注解中。 - Arnold Galovics
太棒了,干得好。干杯,伙计。 - Arnold Galovics

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接