我正在尝试使用wink-client v1.4与Sharepoint RESTful Web服务进行通信。我创建了一个简单的Java SE Maven项目,可以使用BasicAuthSecurityHandler在Windows上执行此任务。但是,这个项目在Mac OS X上不起作用。我在Mac上收到401 HTTP状态代码。从Windows运行时,Wink以某种方式使用我的NTLM凭据。我在两个平台上都使用JDK 7.
如何在Apache Wink客户端上使用NTLM身份验证?
public String getSharepointInfo() {
spUser = "user";
spPassword = "password";
spUri = "https://someSharepointURL/";
ClientConfig clientConfig = new ClientConfig();
Application app = new Application() {
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(WinkMOXyJsonProvider.class);
return classes;
}
};
clientConfig.applications(app);
BasicAuthSecurityHandler basicAuthSecurityHandler = new BasicAuthSecurityHandler();
basicAuthSecurityHandler.setUserName(spUser);
basicAuthSecurityHandler.setPassword(spPassword);
clientConfig.handlers(basicAuthSecurityHandler);
RestClient client = new RestClient(clientConfig);
Resource resource = client.resource(spUri);
ClientResponse response = resource.accept("*/*").get();
String blah = response.getEntity(String.class);
System.out.println("The response is " + blah);
return blah.toString();
}
答案 0 :(得分:0)
我已经明白了。
我的最终目标是创建一个可以移植到WebSphere Application Server v8.0的简单测试用例。 Apache Wink客户端无法自行处理NTLM身份验证。您必须使用单独的Http客户端来处理NTLM身份验证。我选择了Apache Http Cient v4.0.1,因为那个有缺陷的版本是在WAS v8.0中打包的。覆盖所提供的版本也是一个巨大的痛苦。这就是为什么我没有选择更新,更好的Apache HttpClient版本。
因此,以下是Apache Http Client v4.0.1处理NTLM身份验证的方法: 使用以下依赖项...
<dependency>
<groupId>jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.17</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-client</artifactId>
<version>1.4</version>
</dependency>
我正在使用WAS v8.0中包含的com.ibm.ws.prereq.jaxrs.jar来获取Apache Http Client v4.0.1。这是在我的Maven仓库中安装的,我指定它作为获取Http Client v4.0.1的依赖项。
按照here步骤进行操作。
现在,Wink发挥作用:
public int attemptWinkHttpClienGET() {
ClientResponse response = null;
try {
String spUri = "https://some-sharepoint-url/listdata.svc/";
StringBuilder sb = new StringBuilder();
sb.append(spUri).append("UserInformationList").toString();
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getAuthSchemes().register("ntlm",new JCIFSNTLMSchemeFactory());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
NTCredentials ntcred = new NTCredentials("username_here", "password_here", InetAddress.getLocalHost().getHostName(), "domain_here");
credsProvider.setCredentials(new AuthScope("base_url_here_sans_https://", 443, AuthScope.ANY_REALM, "NTLM"), ntcred);
httpClient.setCredentialsProvider(credsProvider);
org.apache.wink.client.ClientConfig httpClientConfig = new org.apache.wink.client.ApacheHttpClientConfig(httpClient);
Application app = new Application() {
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(WinkMOXyJsonProvider.class);
return classes;
}
};
httpClientConfig.applications(app);
RestClient client = new RestClient(httpClientConfig);
Resource resource = client.resource(sb.toString());
response = resource.accept(MediaType.APPLICATION_JSON_TYPE).get();
UserInformationListResponse blah = response.getEntity(UserInformationListResponse.class);
Results[] results = blah.getD().getResults();
for (Results result : results) {
System.out.println("User Name: " + result.getFirstName() + " " + result.getLastName());
}
System.out.println("The response is " + response.getStatusCode());
response.consumeContent();
} catch (UnknownHostException ex) {
Logger.getLogger(HttpTest.class.getName()).log(Level.SEVERE, null, ex);
}
return response.getStatusCode();
}
现在,最后一点。我使用MOXy作为我的JAXB实现。即使我在app变量中注册它,我也遇到了一些问题。我看到了一些与杰克逊有关的错误。 Apache HttpClient v4.0.1显然默认使用引擎盖下的Jackons。以下是我为克服这个问题所做的工作。
我添加了以下依赖项:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.0-rc2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.13</version>
</dependency>
这是WinkMOXyJsonProvider.java
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class WinkMOXyJsonProvider extends MOXyJsonProvider {
}
我观察到从Sharepoint返回的String结果,然后创建了一堆模仿JSON对象层次结构的MOXy POJO。