2014-03-19 86 views
0

我正尝试使用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身份验证

如何在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

我已经得到了一个GET请求,在OS X上使用Apache HttpClient v4.0.1进行NTLM身份验证。我遵循以下链接: http://zhangyelei.blogspot.com/2014/03/httpclient-version-conflict -on-was.html。这是至关重要的第一步,因为在WAS v8.0和v8.5中使用了HttpClient版本。要覆盖该版本,您必须采取荒谬的措施,例如隔离共享库。对我来说,这可能是不可行的。 现在,我只需要弄清楚如何在Apache Wink中使用Apache HttpClient v4.0.1。 –

回答

0

我已经想通了。

我的最终目标是创建一个简单的测试案例,以便将其移植到WebSphere Application Server V8.0。 Apache Wink客户端无法自行处理NTLM身份验证。您必须使用单独的Http客户端来处理NTLM身份验证。我选择了Apache Http Cient v4.0.1,因为那个bug版本是打包在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> 

我使用com.ibm.ws.prereq.jaxrs。 jar包含在WAS v8.0中以获得Apache Http Client v4.0.1。这是安装在我的Maven仓库中的,我将它指定为获取Http Client v4.0.1的依赖项。

请按照步骤here。现在

,眨眼进场:

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实现。即使我在应用程序变量中注册,我也遇到了一些问题。我看到一些杰克逊相关的错误。 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返回,然后创建一堆MOXY POJO的模仿JSON对象分层结构。

相关问题