简体   繁体   中英

Accessing Sharepoint List in java using NTLM authentication

I am using JAX-WS for accessing sharepoint list from java client. I am not able to crack the ntlm authentication part. It's giving me 403 forbidden error.But I am able to authenticate when there is basic authentication enabled. My code is as below. Has anyone worked before on that? Thanks in advance.

public static void main(String[] args) {
    try {
        String userName = "INDIA\\arindam";
        String password = "September@123";
        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));
    }
}

I'm still finding more problems the longer I work with jax-ws and ntlm, but I've at least managed to avoid 403 errors. Try adding these two lines of code after you create your port:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM