gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:00:06 25 4
gpt4 key购买 nike

我熟悉使用 Jersey 创建 RESTful web 服务服务器和客户端,但由于 class loading issues ,我正在尝试将 Jersey 客户端转换为 CXF。我相信我想使用以 HTTP 为中心的客户端,但我们不使用 Spring。我们需要使用基本的 HTTP 身份验证。 user guide有这个例子:

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

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

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

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

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

最佳答案

这个答案来自 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
}

关于java - CXF RESTful Client - 如何在没有 Spring 的情况下进行基本的 http 身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7880707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com