gpt4 book ai didi

rest - jersey 2.x 的 HTTP 摘要身份验证失败,适用于 curl

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:26:19 26 4
gpt4 key购买 nike

我在为 http 摘要认证的休息服务实现客户端时遇到问题。使用 CURL 我能够正确调用该服务:

curl -v --digest -u "username:password" -H "Content-Type: application/json" --data @entity.json http://<server>:<port>/path

其中entity.json包含要发送到服务器的实体对象的JSON结构

使用 wireshark 我可以看到发送 HTTP POST 的跟踪,并收到服务器响应 401 授权。发送使用 md5 密码 nounce 等正确组合的新帖子,并收到带有 json 响应实体对象的 200 OK。一切都很好。

我尝试使用 jersey 2.x 来实现:

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class RestApi
{
private static final Logger log = LoggerFactory.getLogger(RestApi.class)

private Client client;

private String server;
private String username;
private String password;

public RestApi(String server, String username, String password) {
this.server = server;
this.username = username;
this.password = password;
}

public Client getClient() {
if(this.client == null) {
HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest(username, password);
this.client = ClientBuilder.newClient();
this.client.register(feature)
}
return this.client;
}

public <T> T post(
final String path,
final Object bodyValue,
final Class<T> c) throws RestPostException {
log.debug("POST path='{}', server='{}', bodyValue='{}'", path, server, bodyValue );

WebTarget webTarget = getClient().target(server).path(path);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);

Response response = invocationBuilder.post(Entity.entity(bodyValue, MediaType.APPLICATION_JSON_TYPE));

if (response.getStatus() != 200) {
String errorString = response.readEntity(String.class);
throw new RestPostException(
"POST failed to '" + server + "/" + path + "' body value: '" + bodyValue + "' : HTTP error code : " + response.getStatus() +
". Error string:" + errorString);
}
T retval = response.readEntity(c);
return retval;
}
}

像这样从另一个类调用 post 方法:

String returnedValue = getRestApi().post(
path,
v,
String.class);

其中 v 是我要在请求中发送的 json 对象。

使用 wireshark 对此进行跟踪,我看到发送了 POST(根据跟踪,POST 与我使用 curl 发送的 POST 完全相同),并带有 401 未经授权的响应。发送了一个新帖子,但我未能获得 200 OK。相反,我得到了一个新的 401 Unauthorized。

有没有人有这方面的经验?您是否看到我使用 Java 代码的方式缺少任何设置、配置等?

最佳答案

我无法让它与“HttpAuthenticationFeature.digest”一起工作。

但它以标准的 apache http 客户端方式工作。下面是如何使用 dropwizard 的 JerseyClientBuilder 注册 CredentialsProvider 的示例:

    JerseyClientConfiguration configuration = config.httpClient;
HttpHost target = HttpHost.create(config.apiUrl);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials(config.user, config.password));

final Client httpClient = new JerseyClientBuilder(environment)
.using(configuration)
.using(credsProvider)
.build("my-client");

关于rest - jersey 2.x 的 HTTP 摘要身份验证失败,适用于 curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39117073/

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