我试图从Java应用程序访问SharePoint网站.SharePoint服务器更喜欢Kerberos身份验证.能否请您提供仅实施Kerberos身份验证的示例?
因此,为了帮助您扩大搜索范围,有一点关于此处使用的Kerberos身份验证的SharePoint特定内容.事实上,SharePoint并没有真正拥有自己的身份验证机制(至少假设我们在这里谈论WSS 3/MOSS).它只依赖于底层的ASP.NET/IIS身份验证功能.
Sooo,如果您使用现代JDK运行Java,您可能会有一个轻松的时间.请参阅有关HTTP身份验证机制的文档.那里有一些不错的代码片段.其中一个我将在这里复制以供参考.真的,请查看链接.
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Authenticator; import java.net.PasswordAuthentication; import java.net.URL; public class RunHttpSpnego { static final String kuser = "username"; // your account name static final String kpass = "password"; // your password for the account static class MyAuthenticator extends Authenticator { public PasswordAuthentication getPasswordAuthentication() { // I haven't checked getRequestingScheme() here, since for NTLM // and Negotiate, the usrname and password are all the same. System.err.println("Feeding username and password for " + getRequestingScheme()); return (new PasswordAuthentication(kuser, kpass.toCharArray())); } } public static void main(String[] args) throws Exception { Authenticator.setDefault(new MyAuthenticator()); URL url = new URL(args[0]); InputStream ins = url.openConnection().getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(ins)); String str; while((str = reader.readLine()) != null) System.out.println(str); } }
以下是开源SPNEGO HTTP Servlet过滤器库的Java文档中的示例.
该库有一个客户端,可以连接到已启用集成Windows身份验证的Web服务器.
该项目还提供了有关如何为Kerberos/SPNEGO身份验证设置环境的示例.
public static void main(final String[] args) throws Exception { System.setProperty("java.security.krb5.conf", "krb5.conf"); System.setProperty("sun.security.krb5.debug", "true"); System.setProperty("java.security.auth.login.config", "login.conf"); SpnegoHttpURLConnection spnego = null; try { spnego = new SpnegoHttpURLConnection("spnego-client", "dfelix", "myp@s5"); spnego.connect(new URL("http://medusa:8080/index.jsp")); System.out.println(spnego.getResponseCode()); } finally { if (null != spnego) { spnego.disconnect(); } } }