1

I am working on Microsoft Sharepoint integration with SAP. The sharepoint supports 'kerberos fallback to ntlm' method for authentication.

I wish to take advantage of ntlm authentication, as I dont wish to go into kerberos set up. My understanding is that ntlm authentication can be managed with following piece of code however, I am getting '401 Unauthorized' error.

Probably something needs to be added to following line in password authentication.

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());
  }
}

Can anyone help please.

Ravi Gupta
  • 67
  • 8
  • SPNEGO **always** starts with a 401 response... plus a response header that says *"OK, let's start negotiating the authentication"*. Read that post for more details: https://stackoverflow.com/questions/43456734/spnego-subsequent-calls-after-a-successful-negotiation-and-authentication – Samson Scharfrichter May 28 '17 at 14:20
  • Did you find a viable solution? – IgorGanapolsky Aug 24 '17 at 19:42

0 Answers0