2011-09-16 53 views
1

我正在使用JAX-WS从java客户端访问sharepoint列表。我无法破解ntlm认证部分。它给了我403禁止的错误。但是我能够在启用基本身份验证时进行身份验证。我的代码如下。有没有人在此之前工作?提前致谢。使用NTLM身份验证访问java中的Sharepoint列表

public static void main(String[] args) { 
    try { 
     String userName = "INDIA\\arindam"; 
     String password = "[email protected]"; 
     String end = "http://www.sharepoint.com/_vti_bin/lists.asmx"; 
     com.microsoft.schemas.sharepoint.soap.ListsSoap port = null; 
     com.microsoft.schemas.sharepoint.soap.Lists service = new Lists(); 
     port = service.getListsSoap(); 
     NtlmAuthenticator authenticator = new NtlmAuthenticator(userName, password); 
     Authenticator.setDefault(authenticator); 
     String listName = "Shared Documents"; 
     String rowLimit = "150"; 
     String viewName = ""; 
     com.microsoft.schemas.sharepoint.soap.GetListItems.ViewFields viewFields = null; 
     com.microsoft.schemas.sharepoint.soap.GetListItems.Query query = null; 
     com.microsoft.schemas.sharepoint.soap.GetListItems.QueryOptions queryOptions = null; 
     String webID = ""; 
     com.microsoft.schemas.sharepoint.soap.GetListItemsResponse.GetListItemsResult result = port.getListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, webID); 
     System.out.println(result.toString()); 
    } catch (Exception ex) { 
     System.err.println(ex); 
    } 
} 

*

public class NtlmAuthenticator extends Authenticator { 
    private final String username; 
    private final char[] password; 
    com.microsoft.schemas.sharepoint.soap.ListsSoap port = null; 
    com.microsoft.schemas.sharepoint.soap.Lists service = new Lists(); 

    public NtlmAuthenticator(final String username, final String password) { 
     super(); 
     this.username = new String(username); 
     this.password = password.toCharArray(); 
    } 
    @Override 
    public PasswordAuthentication getPasswordAuthentication() { 
     return (new PasswordAuthentication(username, password)); 
    } 
} 

回答

0

我还在寻找更多的问题的时间越长我与JAX-WS和NTLM工作,但我至少设法避免403错误。尝试在创建端口后添加以下两行代码:

((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, userName); 
((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);