使用Java HttpClient从Kerberos身份验证到SharePoint

6
我有一个Linux\Java6客户端,可以使用NTLM验证到SharePoint2010,并使用Apache Commons HttpClient发送HTTP REST Web服务。
我可以使用NTLM进行此操作,但我想使用相同的REST API访问使用Kerberos身份验证的SharePoint 2010。
是否有示例如何使用Kerberos Sharepoint进行身份验证和发送REST over HTTP(最好使用HttpClient)?
附言: 我没有访问Sharepoint代码的权限,但我可以访问Sharepoint管理配置。 这是大致上我使用NTLM进行身份验证的方法:
HttpClient httpClient = new HttpClient(new SimpleHttpConnectionManager(true));
AuthPolicy.registerAuthScheme(AuthPolicy.NTLM, JCIFS_NTLMScheme.class);
String  localHostName = Inet4Address.getLocalHost().getHostName();
authscope = new AuthScope(uri.getHost(), AuthScope.ANY_PORT);
httpClient.getState().setCredentials(authscope,new NTCredentials(
             getUsername(),getPassword(),localHostName,getDomain()));

// after the initial ntlm auth I can call my REST service with "httpClient.executeMethod" 

int status = httpClient.executeMethod(new GetMethod(accessURI + "/sitecollection/info"));

1
你看过http://thejavamonkey.blogspot.com/2008/04/clientserver-hello-world-in-kerberos.html吗? - James Black
这不是我需要的,我有一个现有的 REST Web 服务 API(org.apache.commons.httpclient.HttpClient),它可以使用 NTLM 进行工作,并且在处理使用 Kerberos 的 SharePoint 服务器时需要使用相同的 Web 服务。 - dov.amir
我提到的文章中哪一部分会有问题,因为你需要获取门票,而我不确定你打算如何做到这一点。如果你能提供更多细节,可能会有所帮助。 - James Black
也许我理解有误,但我的目的是像这样向Kerberos SharePoint服务器发送Web请求:httpClient.executeMethod(new GetMethod(accessURI));在文章中进行身份验证后,我能否运行HTTP请求?此外,我不确定如何从我的SharePoint服务器获取正确的主体(principal="webserver/bully@EXAMPLE.COM";)。 - dov.amir
你可能想看一下这个问题,因为它与你想要的类似: https://dev59.com/ZkbRa4cB1Zd3GeqP27Rf - James Black
显示剩余3条评论
1个回答

3
请确认您的环境已正确设置Kerberos,可通过运行kinit实现。如果失败,则需要确保您的krb5.ini(Windows)或krb5.conf(Linux)已正确设置以指向您的域控制器。
一旦确认Kerberos功能正常,您可以使用HttpClient中提供的示例代码,如下所示。
请注意,有许多问题可能导致Kerberos失败,例如时间同步,支持的加密类型,跨域森林的信任关系,还值得确保客户端与服务器不在同一台机器上。
以下是HttpClient下载中提供的示例代码,您需要确保您的JAAS配置和krb5.conf或ini文件正确!
public class ClientKerberosAuthentication {

    public static void main(String[] args) throws Exception {

        System.setProperty("java.security.auth.login.config", "login.conf");
        System.setProperty("java.security.krb5.conf", "krb5.conf");
        System.setProperty("sun.security.krb5.debug", "true");
        System.setProperty("javax.security.auth.useSubjectCredsOnly","false");

        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory());

            Credentials use_jaas_creds = new Credentials() {

                public String getPassword() {
                    return null;
                }

                public Principal getUserPrincipal() {
                    return null;
                }

            };

            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope(null, -1, null),
                    use_jaas_creds);

            HttpUriRequest request = new HttpGet("http://kerberoshost/");
            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println("----------------------------------------");
            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }
            System.out.println("----------------------------------------");

            // This ensures the connection gets released back to the manager
            EntityUtils.consume(entity);

        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }

}

我没有keytab和krb5.conf文件,我需要定义它们吗?我正在尝试访问的是SharePoint Kerberos URL:https://sp10-krb.qa.eng.mycompany.com/sites/mysite/myrestservice.aspx Kinit测试命令是否为:kinit myspadminusername@QA.ENG.MYCOMPANY.COM mysppassword? - dov.amir
需要krb5.conflogin.config文件。如果您的客户端始终使用相同的用户身份连接,则使用keytab可能是一个好主意,以避免使用麻烦的kinit用户/密码身份验证来存储密码。 - Yves Martin
如果我不使用keytab,那么我在哪里提供主体名称和密码? - dov.amir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接