在 Jersey API 客户端配置身份验证 Header

Configuring Authentication Header in Jersey API Client Side

在 Jersey API 中有以下配置身份验证的方法 header 。

//Universal builder having different credentials for different schemes

HttpAuthenticationFeature feature = HttpAuthenticationFeature.universalBuilder()

.credentialsForBasic("username1", "password1")

.credentials("username2", "password2").build();



final Client client = ClientBuilder.newClient();

client.register(feature);

但无法弄清楚如何将额外参数传递给身份验证 header 例如IntegatorKey,SendBehalfOf。这些是特定的 REST 服务调用。

在我的案例中,调用 REST 服务需要传递以下参数作为身份验证的一部分 header。

  1. 用户名

  2. 密码

  3. IntegatorKey

  4. SendBehalfOf

我应该如何使用 Jersey API 实现此目的?

您在问题中没有提供足够的信息。很难猜测您想要达到的目的。您真的应该考虑使用更多详细信息更新您的问题。


根据您提供的表面信息,我猜测您正在尝试访问 DocuSign REST API. If so, you could create a ClientRequestFilter,如下所示:

public class DocuSignAuthenticator implements ClientRequestFilter {

    private String user;
    private String password;
    private String integatorKey;
    private String sendBehalfOf;

    public DocuSignAuthenticator(String username, String password, 
                                 String integatorKey, String sendBehalfOf) {
        this.username = username;
        this.password = password;
        this.integatorKey = integatorKey;
        this.sendBehalfOf = sendBehalfOf;
    }

    @Override
    public void filter(ClientRequestContext requestContext) throws IOException {
        requestContext.getHeaders().add(
            "X-DocuSign-Authentication", getAuthenticationHeader());
    }

    private String getAuthenticationHeader() {

        StringBuilder builder = new StringBuilder();
        builder.append("<DocuSignCredentials>");

        builder.append("<SendOnBehalfOf>");
        builder.append(sendBehalfOf);
        builder.append("</SendOnBehalfOf>");

        builder.append("<Username>");
        builder.append(username);
        builder.append("</Username>");

        builder.append("<Password>");
        builder.append(password);
        builder.append("</Password>");

        builder.append("<IntegratorKey>");
        builder.append(integatorKey);
        builder.append("</IntegratorKey>");

        builder.append("</DocuSignCredentials>");
        return builder.toString();
    }
}

并在创建Client实例时注册:

    Client client = ClientBuilder.newClient().register(
        new DocuSignAuthenticator(username, password, integatorKey, sendBehalfOf));