春季全球客户拦截器

时间:2019-11-19 04:25:51

标签: spring spring-integration spring-ws

我有一个使用大量客户服务的旧版应用程序。最近,需要为每个请求添加一个自定义的http安全标头。一种方法是为每个WebServiceTemplate添加一个ClientInterceptor,但我觉得这是重复的代码。有没有办法应用全局ClientInterceptor?

正如我所说的,这是一个遗留系统,仍然建立在Spring Fw 3.2.2.RELEASE和spring-ws 2.0.6-RELEASE之上

1 个答案:

答案 0 :(得分:2)

您可以实现通用的ClientInterceptor,如下所示:

public class AddCustomSecurityHeader implements ClientInterceptor {

    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
       TransportContext context = TransportContextHolder.getTransportContext();
       HttpComponentsConnection connection =(HttpComponentsConnection) context.getConnection();
       connection.addRequestHeader("custom-security-header", "lorem ipsum");

       return true;
    }
}

然后,您声明类型为@Bean的{​​{1}}:

WebServiceTemplate

希望对您有帮助!