使用 NTLM 身份验证在 ja​​va 中访问 Sharepoint 列表

发布于 2024-12-05 11:07:21 字数 2012 浏览 2 评论 0原文

我正在使用 JAX-WS 从 java 客户端访问共享点列表。我无法破解 ntlm 身份验证部分。它给了我 403 禁止错误。但是当启用基本身份验证时我能够进行身份验证。我的代码如下。以前有人从事过这方面的工作吗?提前致谢。

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 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));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-12-12 11:07:21

使用 jax-ws 和 ntlm 的时间越长,我仍然发现更多的问题,但我至少设法避免了 403 错误。创建端口后尝试添加这两行代码:

((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, userName);
((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文