SAML token based authentication for consuming share point 2013 rest web services from JAVA/J2ee application

1k Views Asked by At

I have a java/j2ee web application consuming SP web services but recently the SP site got migrated to 2013 and deployed in cloud/office 0365 due to which authentication got broken. SP people suggested to change authentication mechanism to SAML token based authentication and use Microsoft Azure AD. So i on boarded my application into Azure and received Client ID, Authority using which i am able to generate security token(used adal4j java api) . Now i need to complete below 2 steps to complete the authentication process in office 0365 to access SP 2013 web services.

  1. Get access token cookies
  2. Get request digest token

But not able to find any java based API for above 2 steps. Refereed below tutorial buts its something related to aps/.net

http://paulryan.com.au/2014/spo-remote-authentication-rest/

Please help me in providing sample code base for the same.

Appreciate your support

2

There are 2 best solutions below

0
On

Per my experience, I think you can try to directly follow your refered article step by step to use the Apache HttpClient to construct the request.

For example, the code below is using the HttpClient to do the post request with the xml body to get the security token.

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://login.microsoftonline.com/extSTS.srf");
String xmlBody = "...";
InputStreamEntity reqEntity = new InputStreamEntity(
                    new ByteArrayInputStream(xmlBody.getBytes(), -1, ContentType.APPLICATION_OCTET_STREAM);
            reqEntity.setChunked(true);
httpPost.addHeader("Accept", "application/json; odata=verbose")
httpPost.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
String respXmlBody = EntityUtils.toString(response.getEntity());
//Parse the respXmlBody and extract the security token

You can try to follow the code above to get the response includes access token via do the post request with the security token body for the url https://yourdomain.sharepoint.com/_forms/default.aspx?wa=wsignin1.0, and use the code Header[] hs = response.getHeaders("Set-Cookie"); to get the Set-Cookie header array as access token.

Then using them to set the two headers Cookie for getting the request digest token, and parse the response body to extract the FormDigestValue as the request digest token.

0
On

So you used Microsoft Azure Active Directory Authentication Library (ADAL) for Java?

In which case, have a look at AAD Java samples.

You want the ones that consume a Web API.