Java 如何在Apache Wink客户端上使用NTLM身份验证
我正在尝试使用wink client v1.4与Sharepoint RESTful web服务通信。我创建了一个简单的Java SE Maven项目,可以在Windows上使用BasicAuthSecurityHandler完成此任务。然而,这个项目在MacOSX上不起作用。我在Mac上收到一个401HTTP状态码。从Windows运行时,Wink不知何故使用了我的NTLM凭据。我在两种平台上都使用JDK 7 如何在Apache Wink客户端上使用NTLM身份验证Java 如何在Apache Wink客户端上使用NTLM身份验证,java,sharepoint,ntlm,apache-wink,Java,Sharepoint,Ntlm,Apache Wink,我正在尝试使用wink client v1.4与Sharepoint RESTful web服务通信。我创建了一个简单的Java SE Maven项目,可以在Windows上使用BasicAuthSecurityHandler完成此任务。然而,这个项目在MacOSX上不起作用。我在Mac上收到一个401HTTP状态码。从Windows运行时,Wink不知何故使用了我的NTLM凭据。我在两种平台上都使用JDK 7 如何在Apache Wink客户端上使用NTLM身份验证 public String
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();
}
公共字符串getSharepointInfo(){
spUser=“用户”;
spPassword=“密码”;
斯普瑞=”https://someSharepointURL/";
ClientConfig ClientConfig=new ClientConfig();
应用程序app=新应用程序(){
publicset>classes=newhashset我已经弄明白了
我的最终目标是创建一个可以移植到WebSphere Application Server v8.0的简单测试用例。Apache Wink客户端无法单独处理NTLM身份验证。您必须使用单独的Http客户端来处理NTLM身份验证。我选择了Apache Http Cient v4.0.1,因为该错误版本打包在was v8.0中。这是一个巨大的问题这就是为什么我没有选择一个更新、更好的Apache HttpClient版本
下面介绍如何让Apache Http客户端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>
我观察了从Sharepoint返回的字符串结果,然后创建了一组模仿JSON对象层次结构的MOXy POJO。我收到一个GET请求,要求在OS X上使用Apache HttpClient v4.0.1进行NTLM身份验证。我遵循以下链接:。这是至关重要的第一步,因为HttpClient版本在WAS v8.0和v8中使用。5.要覆盖该版本,您必须采取荒谬的措施,例如隔离共享库。这对某些人来说可能是不可行的,就像我的情况一样。现在,我只需要弄清楚如何将Apache HttpClient v4.0.1与Apache 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();
}
<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>
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 {
}