CXF RESTful 客户端 - 如何在没有 Spring 的情况下进行基本的 http 身份验证?

发布于 2024-12-11 16:22:32 字数 851 浏览 0 评论 0原文

我熟悉使用 Jersey 创建 RESTful Web 服务服务器和客户端,但由于 类加载问题,我正在尝试将 Jersey 客户端转换为 CXF。我相信我想使用以 HTTP 为中心的客户端,但我们不使用 Spring。我们需要使用基本的 HTTP 身份验证。 用户指南有以下示例:

WebClient client = WebClient.create("http:books", "username", "password", "classpath:/config/https.xml");

第一个参数是' t 一个 URI 字符串。这是Spring使用的格式吗?这个方法只能用于使用Spring创建WebClient吗?

显示的另一种进行身份验证的方法是添加标头字符串:

String authorizationHeader = "Basic " + org.apache.cxf.common.util.Base64Utility.encode("user:password".getBytes());
webClient.header("Authorization", authorizationHeader);

我猜测 "user:password" 应该替换为真实值,但希望得到确认。

I am familiar with using Jersey to create RESTful webservice servers and clients, but due to class loading issues, I am trying to convert a Jersey client into CXF. I believe I want to use an HTTP-centric client but we don't use Spring. We need to use basic HTTP authentication. The user guide has this example:

WebClient client = WebClient.create("http:books", "username", "password", "classpath:/config/https.xml");

The first parameter isn't a URI string. Is it a format used by Spring? Can this method only be used to create WebClients using Spring?

The other way of doing authentication shown is to add a header string:

String authorizationHeader = "Basic " + org.apache.cxf.common.util.Base64Utility.encode("user:password".getBytes());
webClient.header("Authorization", authorizationHeader);

I am guessing that "user:password" should be substituted with the real values, but would appreciate confirmation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2024-12-18 16:22:32

这个答案来自 CXF 用户邮件列表。

上面引用的第一个示例中有一个拼写错误。它已更新为:

WebClient client = WebClient.create("http://books", "username", "password", "classpath:/config/https.xml");

如果未使用 Spring 配置文件(因此 Spring),第四个参数可以为 null。

所以,这对我有用:

private WebClient webClient;

public RESTfulClient(String url, String username, String password)
        throws IllegalArgumentException
{
    this.username = username;
    this.password = password;
    this.serviceURL = url;

    if (username == null || password == null || serviceURL == null)
    {
        String msg = "username, password and serviceURL MUST be defined.";
        log.error(msg);
        throw new IllegalArgumentException(msg);
    }

    webClient = WebClient.create(this.serviceURL,
            this.username,
            this.password,
            null); // Spring config file - we don't use this
}

This answer came from the CXF users mailing list.

The first example referenced above had a typo in it. It has been updated to:

WebClient client = WebClient.create("http://books", "username", "password", "classpath:/config/https.xml");

The fourth argument can be null if a Spring config file is (and therefore Spring) is not being used.

So, this worked for me:

private WebClient webClient;

public RESTfulClient(String url, String username, String password)
        throws IllegalArgumentException
{
    this.username = username;
    this.password = password;
    this.serviceURL = url;

    if (username == null || password == null || serviceURL == null)
    {
        String msg = "username, password and serviceURL MUST be defined.";
        log.error(msg);
        throw new IllegalArgumentException(msg);
    }

    webClient = WebClient.create(this.serviceURL,
            this.username,
            this.password,
            null); // Spring config file - we don't use this
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文