如何使用Jersey(JAX-RS)向NTLM身份验证服务器发出简单的POST请求



我在网上搜索过,发现了一些文章,比如:

使用jersey和apache httpClient进行REST调用的NonRepeatableRequestException

和:

如何使用球衣发送NTLM身份验证的投递请求?

第二个问题的答案只为GET请求提供了一个解决方案。我正在按如下方式设置我的客户:

private void init() {
if (client == null) {
client = ClientBuilder.newClient(prepareClientConfig());
}
}
private ClientConfig prepareClientConfig() {
ClientConfig config = new ClientConfig();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new NTCredentials(userId, password, null, null));
config.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
config.connectorProvider(new ApacheConnectorProvider());
return config;
}

这是我提出GET请求的地方:

@Override
public Response get(String uri, List<HttpUrlParameter> params) throws WebCallException {
init();
String url = baseUrl + restUri + apiVersion + uri;
WebTarget webTarget = client.target(url);
for (HttpUrlParameter param : params) {
webTarget = webTarget.queryParam(param.getKey(), param.getValue());
}
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
return response;
}

HttpUrlParameter和WebCallException类是我定义的。上面的GET请求非常有效。然后我尝试POST调用:

@Override
public Response post(String uri, Object entity, List<HttpUrlParameter> params) throws WebCallException {
init();
String url = baseUrl + restUri + apiVersion + uri;
WebTarget webTarget = client.target(url);
for (HttpUrlParameter param : params) {
webTarget = webTarget.queryParam(param.getKey(), param.getValue());
}
Response response = webTarget
.request(MediaType.APPLICATION_JSON)
.post(Entity.json(entity));
return response;
}

我在这个特定测试中通过的实体非常直接:

import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
public class CrmAccount {
@JsonProperty("accountid")
private String accountId;
@JsonProperty("accountnumber")
private String accountNumber;
private String name;
@JsonProperty("emailaddress1")
private String emailAddress1;
@JsonProperty("telephone1")
private String phoneNumber;
@JsonProperty("address1_addressid")
private String address1Id;
@JsonProperty("address1_line1")
private String address1Line1;
@JsonProperty("address1_line2")
private String address1Line2;
@JsonProperty("address1_city")
private String address1City;
@JsonProperty("address1_stateorprovince")
private String address1StateOrProvince;
@JsonProperty("address1_postalcode")
private String address1PostalCode;
@JsonProperty("address1_county")
private String address1Country;
public CrmAccount() {
super();
}
public String getAccountId() {
return accountId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailAddress1() {
return emailAddress1;
}
public void setEmailAddress1(String emailAddress1) {
this.emailAddress1 = emailAddress1;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress1Id() {
return address1Id;
}
public void setAddress1Id(String address1Id) {
this.address1Id = address1Id;
}
public String getAddress1Line1() {
return address1Line1;
}
public void setAddress1Line1(String address1Line1) {
this.address1Line1 = address1Line1;
}
public String getAddress1Line2() {
return address1Line2;
}
public void setAddress1Line2(String address1Line2) {
this.address1Line2 = address1Line2;
}
public String getAddress1City() {
return address1City;
}
public void setAddress1City(String address1City) {
this.address1City = address1City;
}
public String getAddress1StateOrProvince() {
return address1StateOrProvince;
}
public void setAddress1StateOrProvince(String address1StateOrProvince) {
this.address1StateOrProvince = address1StateOrProvince;
}
public String getAddress1PostalCode() {
return address1PostalCode;
}
public void setAddress1PostalCode(String address1PostalCode) {
this.address1PostalCode = address1PostalCode;
}
public String getAddress1Country() {
return address1Country;
}
public void setAddress1Country(String address1Country) {
this.address1Country = address1Country;
}
}

我可以通过Postman进行POST调用而不会出现任何问题,所以我知道该调用(在我的Java代码之外(运行良好。当我使用Jersey进行POST调用时,我会得到以下异常:

javax.ws.rs.ProcessingException: org.apache.http.client.ClientProtocolException
at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:481)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at com.harpercollinschristian.integration.util.rest.crm.RestApiClientCrmImpl.post(RestApiClientCrmImpl.java:119)
at com.harpercollinschristian.integration.service.crm.CrmRestfulApi$4.request(CrmRestfulApi.java:127)
at com.harpercollinschristian.integration.util.rest.crm.CrmRestApiRequest.exec(CrmRestApiRequest.java:53)
at com.harpercollinschristian.integration.service.crm.CrmRestfulApi.createAccount(CrmRestfulApi.java:132)
at com.harpercollinschristian.integration.service.crm.CrmRestfulApiTest.testCreateAccount(CrmRestfulApiTest.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:435)
... 44 more
Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity.
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
... 46 more

在我看来,传递给post((方法的JSON实体可能被设置为只能调用一次的流?我需要为所有POST和PATCH调用发送的JSON消息并不长,并且可以使用FasterXML/Jowson库轻松地表示为String,因此我们应该能够轻松地"重复"实体。我知道我可能遗漏了一些显而易见的东西,但我找不到解决方案。该项目正在使用Jersey Client for REST API调用来调用另外两个非Microsoft(因此也是非NTLM(服务,因此我需要使用Jersey来处理这些服务(换句话说,如果我能帮助的话,我不想退出Jersey(。

如有任何帮助,我们将不胜感激!

我收到了我们的Microsoft CRM合作伙伴的一段代码片段,它给出了答案。我们设置配置的方法需要更新为:

private ClientConfig prepareClientConfig() {
ClientConfig config = new ClientConfig();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final AuthScope ntlmAuthScope = new AuthScope(null, -1, AuthScope.ANY_REALM, "NTLM");
credentialsProvider.setCredentials(ntlmAuthScope, new NTCredentials(userId, password, null, null));
config.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
config.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
config.connectorProvider(new ApacheConnectorProvider());
return config;
}

注意REQUEST_ENTITY_PROCESSING的config属性-设置为BUFFERED。我相信,这正是我们所需要的,以允许对实体进行缓冲,从而在后续请求中重用(以满足NTLM协议(。还有一些其他添加(例如特定的AuthScope(。

最新更新