我尝试按照描述的方式通过 JAX-WS 访问 Sharepoint 列表 here
但是,当运行下面的代码时,我得到:
java.lang.Exception: Exception. See stacktrace.com.sun.xml.internal.ws.client.ClientTransportException: The server sent HTTP status code 401: Unauthorized
Sharepoint 需要 NTLM 身份验证。可能是什么问题?非常感谢!
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 服务时遇到了同样的问题,以下是对我有用的方法:
首先,创建一个 validator :
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));
}
}
在您的应用程序中,将身份 validator 设置为默认值:
String username = "DOMAIN\\USERNAME";
String password = "PASSWORD"
NtlmAuthenticator authenticator = new NtlmAuthenticator(username, password);
Authenticator.setDefault(authenticator);
请注意,我使用方法 #2 指定域,如 Java documentation. 中所述
关于java - JAX-WS Sharepoint 401 未经授权的 NTLM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4865165/