kerberos回退到ntlm java代码

时间:2017-05-27 21:28:28

标签: java https ntlm

我正在与Microsoft Sharepoint集成SAP。 sharepoint支持'kerberos fallback to ntlm'方法进行身份验证。

我希望利用ntlm身份验证,因为我不想进入kerberos设置。我的理解是,ntlm身份验证可以使用以下代码进行管理,但是,我收到了“401 Unauthorized”错误。

可能需要在密码验证中添加以下内容。

if(getRequestingScheme()。equalsIgnoreCase(“negotiate”)|| getRequestingScheme()。equalsIgnoreCase(“ntlm”)|| getRequestingScheme()。equalsIgnoreCase(“kerberos”))

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.lang.Object;
import java.nio.charset.StandardCharsets;
import java.io.OutputStream;
import java.net.InetAddress;

public class Main {
  public static void main(String[] argv) throws Exception {
    Authenticator.setDefault(new MyAuthenticator());
    URL url = new URL ("https://sharepointlink");

                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                               conn.setDoOutput( true );
                                //conn.setInstanceFollowRedirects( false );
                                conn.setRequestMethod( "POST" );
                                conn.setRequestProperty("Accept", "*/*");
                                 //MyAuthenticator m = new MyAuthenticator();
String urlParameters  = "tests";
byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
int    postDataLength = postData.length;                
byte[] data = ("").getBytes("UTF-8");     

      OutputStream out = conn.getOutputStream();
        out.write(postData);
        out.flush();      

             StringBuilder response = new StringBuilder();

                    InputStream stream = conn.getInputStream();
                    //  InputStream estream = conn.getErrorStream();

                    BufferedReader in = new BufferedReader(new InputStreamReader(stream));
                    String str = "";
                    while ((str = in.readLine()) != null) {
                        response.append(str) ;
                    }
                    in.close();                        
System.out.println(response.toString());
                   // return response.toString() ;
  }
}

 class MyAuthenticator extends Authenticator {
//    static String user = System.getProperty("user");
  //  static String pass = System.getProperty("pass");
  //  static String kuser = System.getProperty("kuser");
   // static String kpass = System.getProperty("kpass");
   // static String showhint = System.getProperty("showhint");

  protected PasswordAuthentication getPasswordAuthentication() {
    String promptString = getRequestingPrompt();
    System.out.println("prompt string " + promptString);
    String hostname = getRequestingHost();
    System.out.println("host name " + hostname);
    InetAddress ipaddr = getRequestingSite();
    System.out.println(ipaddr);
    int port = getRequestingPort();
    RequestorType reqType = getRequestorType();
    System.out.println ("reqeust type = " + reqType.toString());
    System.out.println ("Protocol type = " + getRequestingProtocol());
    System.out.println ("Scheme type = " + getRequestingScheme());
    if (getRequestingScheme().equalsIgnoreCase("negotiate") || getRequestingScheme().equalsIgnoreCase("ntlm") ||  getRequestingScheme().equalsIgnoreCase("kerberos")) {
                     String krb5user="tp1\\user";
                     String krb5pass ="pwd";
                     // get krb5user and krb5pass in your own way

            return  (new PasswordAuthentication (krb5user,    krb5pass.toCharArray()));
          }


    String username = "TP1\\user";
    String password = "pwd";
    return new PasswordAuthentication(username, password.toCharArray());
  }
}

任何人都可以帮忙。

0 个答案:

没有答案
相关问题