Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By gimbal2
  • 2 Post By gimbal2

Thread: Need Java client to Upload files to Sharepoint 2010 using Kerberos authentication

  1. #1
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Need Java client to Upload files to Sharepoint 2010 using Kerberos authentication

    Hi All,

    Need help on Java client to upload file to Sharepoint which is using KERBEROS authentication.

    We have sharepoint server which is upgraded to 2010 version and is configured with Kerberos authentication. Earlier sharepoint version was using NTLM authentication for which I have javaq client program to upload files from local system. Since sharepoint got upgraded with Kerberos authentication, I need to modify current NTLM versioned java program to use Kerberos. I got code snippet for authentication and connectivity which is working fine. I am able to read Sharepoint URL and download a specific file though java program. Now I am trying upload file to Sharepoint but not getting the required java classes and jar files to be used for this.

    I had Kerberos configuration setup using SPNEGO API to connect sharepoint.

    Conf files:
    krb5.conf
    login.conf

    API used to Kerberos Auth:
    spnego-r7.jar


    Connectivity:
    Following code I am using for connectivity and File download which is perfectly working.

    spnego = new SpnegoHttpURLConnection("spnego-client", <<sharepoint_user>>, <<sharepoint_password>>);

    //New Lines added to omit SSL Handshake exception
    TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers(){
    return null;
    }
    public void checkClientTrusted(java.security.cert.X509Certific ate[] certs, String authType){
    //No need to implement.
    }
    public void checkServerTrusted(java.security.cert.X509Certific ate[] certs, String authType){
    //No need to implement.
    }
    }
    };
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.g etSocketFactory());
    spnego.connect(new URL(spLocation));
    System.out.println("spnego.getResponseCode():: "+spnego.getResponseCode());
    if(spnego.getResponseCode() >= 200) {
    log.debug("Authentication Successful");
    }

    File Read/Download:
    java.io.BufferedInputStream in = new java.io.BufferedInputStream( spnego.getInputStream());
    java.io.FileOutputStream fos = new java.io.FileOutputStream(outputFile);
    java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
    byte[] data = new byte[1024];
    int x=0;
    System.out.println("4" + outputFile.length());
    while((x=in.read(data,0,1024))>=0) {
    bout.write(data,0,x);
    }
    bout.close();
    in.close();



    Kindly advise how to upload files to Sharepoint folder using java code. I searched many forums for hours but not getting exact code for File upload. Your advice on this much appreciated.

    Thanks in advance.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Need Java client to Upload files to Sharepoint 2010 using Kerberos authentication

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: Need Java client to Upload files to Sharepoint 2010 using Kerberos authentication

    Warning: this reply was crossposted.
    PhHein likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Need Java client to Upload files to Sharepoint 2010 using Kerberos authentication

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need Java client to Upload files to Sharepoint 2010 using Kerberos authentication

    Quote Originally Posted by DarrylBurke View Post
    Sorry for cross post. Henceforth I will ensure to post only in one forum.

    Please advise your suggestion on this: Uploading file to Sharepoint through Java client using Kerberos Authentication.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: Need Java client to Upload files to Sharepoint 2010 using Kerberos authentication

    You don't need to restrict yourself to one forum - just mention that you crossposted, and post links to your other posts so people can follow those and see if you've already gotten a reply before spending time writing an answer already given. That will require that you edit existing posts to add the links in, but you shouldn't be bothered by doing a little bit of extra effort to do the right thing.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Default Re: Need Java client to Upload files to Sharepoint 2010 using Kerberos authentication

    Finally after 10 days of continuous research and search through many blogs, I got consolidated solution for my problem. I feel I should share it to all and I hope this helps:

    UPLOAD MULTIPLE FILES TO SHAREPOINT (KERBEROS AUTHENTICATED):

    System.setProperty("java.security.krb5.conf", workareaFolder+"/"+props.getProperty("kerberos.conf.file"));
    System.setProperty( "java.security.auth.login.config", workareaFolder+"/"+props.getProperty("jass.conf.file"));
    System.setProperty( "javax.security.auth.useSubjectCredsOnly", "false");

    krb5MechOid = new Oid("1.2.840.113554.1.2.2");
    spnegoMechOid = new Oid("1.3.6.1.5.5.2");

    shost= targetSPN.toLowerCase();
    if (shost.startsWith("http/") || shost.startsWith("cifs/") ) {
    shost = shost.substring(5);
    }
    else {
    log.debug("Entered invalid SPN. Must begin with HTTP/ or CIFS/");
    System.exit(-1);
    }
    this.checkSPNHostname(shost);

    //login to the KDC using JAAS login module
    this.subject = login(username, password);

    log.debug(this.subject);

    SSLContext sslContext = SSLContext.getInstance("SSL");

    // set up a TrustManager that trusts everything
    sslContext.init(null, new TrustManager[] { new X509TrustManager() {
    public X509Certificate[] getAcceptedIssuers() {
    return null;
    }
    public void checkClientTrusted(X509Certificate[] certs,
    String authType) {
    }
    public void checkServerTrusted(X509Certificate[] certs,
    String authType) {
    }
    } }, new SecureRandom());

    Scheme httpScheme80 = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());
    SSLSocketFactory sf = new SSLSocketFactory(sslContext,SSLSocketFactory.ALLOW _ALL_HOSTNAME_VERIFIER);
    Scheme httpsScheme = new Scheme("https", 443, sf);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);
    schemeRegistry.register(httpScheme80);

    // Create Connection Manager instance for use by Httpclient
    cm = new SingleClientConnManager(schemeRegistry);
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VE RSION, HttpVersion.HTTP_1_1);

    httpclient = new DefaultHttpClient(cm,params);
    httpclient.setRedirectStrategy(new DefaultRedirectStrategy());

    File[] listOfFiles = folder.listFiles(new FileFilter() {
    public boolean accept(File f) {
    if (f.isFile()) {
    return true;
    }
    return false;
    }
    });
    String[] url = new String[listOfFiles.length];
    String spTargetFolder = new String();

    totalFilesInReportsFolder = listOfFiles.length;

    for (int i = 0; i < totalFilesInReportsFolder; i++) {
    uploadFileName = listOfFiles[i].getName();
    log.info("\nFile: "+uploadFileName);

    spTargetFolder = this.getSPFolder(uploadFileName);
    spDestinationFolderURL = sharedDocumentsRoot + instanceFolder + "/" + spTargetFolder + "/" + uploadFileName;
    //log.info("Destination URL : " + spDestinationFolderURL);

    httpPut = new HttpPut(new URI(sharedDocumentsRoot + instanceFolder + "/" + spTargetFolder + "/" + uploadFileName));
    httpPut.getParams().setParameter("http.protocol.ha ndle-redirects",true);
    InputStreamEntity inputStreamEntity = new InputStreamEntity(new FileInputStream(listOfFiles[i]), listOfFiles[i].length());
    httpPut.setEntity(inputStreamEntity);
    // Get the service ticket for targetSPN and set it in HttpPut Authorization header
    this.serviceTicket= initiateSecurityContext( targetSPN );
    encodedBytes = org.apache.commons.codec.binary.Base64.encodeBase6 4(this.serviceTicket);
    encoded = new String(encodedBytes);
    httpPut.addHeader("Authorization", "Negotiate " + encoded);

    httpResponse = null;

    try {
    log.info("Uploading File... ");
    log.debug("Executing httpPut request: " + httpPut.getRequestLine());
    httpResponse = httpclient.execute(httpPut);
    log.debug("After Post - Status code:" +httpResponse.getStatusLine().getStatusCode());
    BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getCont ent(), "UTF-8"));
    StringBuilder s = new StringBuilder();
    String sResponse;
    while ((sResponse = reader.readLine()) != null) {
    s = s.append(sResponse);
    }


    if (httpResponse.getStatusLine() != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    log.info("Response Code received: "+ httpResponse.getStatusLine() +" [200 - CREATE / OVERWRITE]");
    log.info("File Sucessfully uploaded to Sharepoint location: "+spDestinationFolderURL );
    uploadSuccessCount++;
    } else {
    log.error("Error while uploading file to sharepoint");
    if (httpResponse.getStatusLine() != null && httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED){
    log.error("Response code: "+httpResponse.getStatusLine().getStatusCode() );
    log.error("Response Text:" + s);
    }
    uploadFailCount++;
    }

    log.debug("----------------------------------------");
    log.debug("Response StatusLine: "+ httpResponse.getStatusLine());
    log.debug("----------------------------------------");
    log.debug("Return Code : " + httpResponse.getStatusLine().getStatusCode());
    log.debug("----------------------------------------");

    } catch (Exception exp) {
    log.error("Exception while uploading report \""+uploadFileName+"\" to Share point="+exp);
    exp.printStackTrace();
    }
    }

Similar Threads

  1. Kerberos Authentication in Applet
    By Szpan in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2013, 08:01 PM
  2. SSL Client authentication question
    By lamikam in forum Advanced Java
    Replies: 0
    Last Post: 04-27-2012, 09:45 PM
  3. upload image from client to server
    By srujiram0 in forum Java Applets
    Replies: 4
    Last Post: 04-26-2012, 06:53 PM
  4. JAVA coded button not working on Sharepoint page
    By selmore in forum Java Applets
    Replies: 16
    Last Post: 04-02-2012, 09:10 PM
  5. Multi-client authentication server
    By uchihassj4 in forum New To Java
    Replies: 0
    Last Post: 10-06-2011, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •