Access Sharepoint Webservices From Java Behind Proxy

- - posted in Technical | Tagged as java,sharepoint | Comments

In my previous post I told you how to access sharepoint webservices from java But what if you would like to access the sharepoint from behind a proxy, let's say from your place of work.

The approach for Authenticator mentioned here works if you only need to provide username/password for your sharepoint site and not for the proxy. Or the other way around, when you need to provide username/password for proxy but your sharepoint site does not require any authentication, which is hardly true I guess. You can achieve this by setting up a default Authenticator as mentioned here

But What should you do when you require the the authentication for both proxy and the sharepoint site. You cannot setup a single default Authenticator.
You would get "ERROR 407 proxy authentication required", or your sharepoinnt site will deny access, based on what you setup in default autneticator.
So what must you do? Well you can use the following code, it works in my case, hope it will work for you also.

The following approach works:
1. If you are accessing webservices from behind the proxy. Hence you need to specify proxyhost, proxyport, proxyuser, proxypass.
2. If you need a username/password for sharepoint site as well. Hence you need to specify sharepointusername, sharepointpassword for authentication.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[SimpleAuthenticator with proxy support - SimpleAuthenticator.java]

public class SimpleAuthenticator  extends Authenticator
{
  private final static String proxyHost = "proxy.co.uk";
  private final static String proxyuser = "proxyuser";
  private final static String proxypass = "proxypass";
  private final static String sharepointusername = "sharepointusername";
  private final static String sharepointpassword = "sharepointpassword";
  private String username;
  private final char[] password;
   
   public SimpleAuthenticator(String username,String password)
   {
     super();
     this.username = new String(username);
     this.password = password.toCharArray(); 
   }
   
   protected PasswordAuthentication getPasswordAuthentication()
   {
    String requestingHost = getRequestingHost();
    if (requestingHost == proxyHost){
        System.out.println("getPasswordAuthentication() request recieved from->" + requestingHost );
        return new PasswordAuthentication(proxyuser,proxypass.toCharArray());
    }
    else{
        System.out.println("getPasswordAuthentication() request recieved from->" + requestingHost );
        return new PasswordAuthentication(sharepointusername,sharepointpassword.toCharArray());
    }
        
   }
}

In your main class , don't set the default Authenticator and instead use this.

1
2
3
4
5
6
7
8
[SharePointClient - SharePointClient.java https://gist.github.com/TheAshwanik/983fde4be42022ac1f29]

  Authenticator.setDefault(new SimpleAuthenticator("", ""));
  System.out.println("Configuring Proxy settings");
  System.getProperties().put("http.proxyHost",proxyHost);
  System.getProperties().put("http.proxyPort",proxyPort);
  System.getProperties().put("https.proxyHost",proxyHost);
  System.getProperties().put("https.proxyPort",proxyPort);

That's it. I update the previous gist here






Gravatar of Ashwani Kumar

Recent posts


Subscribe



Your Feedback encourages me




Learning and Developments

One Month Rails



, 2FA, AWS AWS, Active Authenticator Directory, Facebook Flash, Forwarding, GOD,Chat,Coffee Github,Feedback,Repo Google Google,Search HAProxy, IOT, IP-block JQuery LetsEncrypt Load MQ MQTT, Messaging Octopress Octopress, OpenVpn OpenVpn, PI, Plugin Plugin, Port Raspberry, S3, SSH, Shell,Commands Soapui, Tag Tag, Tree, Tunneling XML XML, XServer, Xming ajax, angular, animated architecture architecture, azure balancing cloud, commenting, connectivity datapower datatables diagrams diaspora dropdown geocoding grep, hashicorp, ipaddress, ipv6, java, java,python mysql nokogiri, octopress-migration octopress-plugin oidc openapi, openssl powershell proxy rails, repo reviews ruby, script scripts, security, sharepoint shell spiffe spire spring springboot, ssh, swagger, telnet, vault vi, vieditor vim, visualblock, webattacks windows,cleanup windowsxp workload identity