[英]How to use NTLM authentication with Apache Wink client
我正在嘗試使用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();
}
我知道了。
我的最終目標是創建一個可以移植到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的依賴項。
請按照此處的步驟操作。
現在,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實現。 即使我在我的應用變量中注冊它,我也遇到了一些問題,使其無法正常工作。 我看到了一些與傑克遜有關的錯誤。 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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.