JAX-WS Sharepoint 401 未经授权的 NTLM [英] JAX-WS Sharepoint 401 Unauthorized NTLM
问题描述
我尝试通过 JAX-WS 访问 Sharepoint 列表,如这里
I try to access a Sharepoint list via JAX-WS as described here
但是,当运行下面的代码时,我得到:
However, when running the code below I get:
java.lang.Exception: Exception. See stacktrace.com.sun.xml.internal.ws.client.ClientTransportException: The server sent HTTP status code 401: Unauthorized
Sharepoint 需要 NTLM 身份验证.可能是什么问题?非常感谢!
Sharepoint requires NTLM authentication. What may be the problem? Thanks a lot!
public static ListsSoap sharePointListsAuth(String userName, String password) throws Exception {
ListsSoap port = null;
if (userName != null && password != null) {
try {
Lists service = new Lists();
port = service.getListsSoap();
System.out.println("Web Service Auth Username: " + userName);
((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, userName);
((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
} catch (Exception e) {
throw new Exception("Error: " + e.toString());
}
} else {
throw new Exception("Couldn't authenticate: Invalid connection details given.");
}
return port;
}
推荐答案
我在使用 JAX-WS 连接到 Exchange Web 服务时遇到了同样的问题,以下是对我有用的方法:
I was facing the same problem when connecting with JAX-WS to Exchange web services, and here's what worked for me:
首先,创建一个验证器:
First, create an authenticator:
import java.net.Authenticator;
import java.net.PasswordAuthentication;
public class NtlmAuthenticator extends Authenticator {
private final String username;
private final char[] password;
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));
}
}
在您的应用程序中,将身份验证器设置为默认值:
In your application, set up the authenticator as the default:
String username = "DOMAIN\USERNAME";
String password = "PASSWORD"
NtlmAuthenticator authenticator = new NtlmAuthenticator(username, password);
Authenticator.setDefault(authenticator);
请注意,我使用方法 #2 来指定域,如 Java 文档.
Note that I'm using method #2 for specifying the domain as described in the Java documentation.
这篇关于JAX-WS Sharepoint 401 未经授权的 NTLM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!