Iam able to authenticate to external RestAPI using the following java code, iam wondering how to authenticate in Meteor. i tried with http package but ended up with the certificate error. Not sure what iam missing here? Appreciate for any pointers. thanks
//create an instance of SSLContext:
SSLContext mySSLContext = SSLContext.getInstance("SSL");
//initialize the SSLContext with the TrustManager instance we created:
mySSLContext.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(mySSLContext.getSocketFactory());
//Authenticate credentials **********************
DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//Instantiate an object HttpGet with the URL of the server you want to connect,
HttpGet httpGetID = new HttpGet("https://localhost:9443/ccm/authenticated/identity");
httpclient.execute(httpGetID, localContext);
httpGetID.abort();
//to make sure that you created your cookies correctly, try to retrieve the saved information:
List<Cookie> cookies1 = cookieStore.getCookies();
for (Cookie cookie : cookies1) {
System.out.println("\t" + cookie.getName() + " : " + cookie.getValue());
}
//The next step is to authenticate with the j_security_check, using a valid username and password credentials.
//For this step, we use the Apache HttpClient class NameValuePair:
List<NameValuePair> authFormParams = new ArrayList<NameValuePair>();
authFormParams.add(new BasicNameValuePair("j_username", "Myusername"));
authFormParams.add(new BasicNameValuePair("j_password", "Mypassword"));
UrlEncodedFormEntity encodedentity = new UrlEncodedFormEntity(authFormParams,
"UTF-8");
HttpPost httpPostAuth = new HttpPost("https://localhost:9443/ccm/authenticated/j_security_check");
httpPostAuth.setEntity(encodedentity);
httpclient.execute(httpPostAuth, localContext);
//to display the cookie data
List<Cookie> cookies2 = cookieStore.getCookies();
for (Cookie cookie : cookies2) {
System.out.println("\t" + cookie.getName() + " : " + cookie.getValue());
}
The following is the Meteor Code i have tried:
var result = HTTP.call("GET", https://localhost:9443/ccm/authenticated/identity,
function(error,result){
if(result){
console.log("result is" + JSON.stringify(result));
//console.log("myresult" + result);
}else{
console.log("error is" + error);
}
}
);
console.log("my result" + result);
var result1 = HTTP.call("POST", https://localhost:9443/ccm/authenticated/j_security_check,
{auth: {j_username: 'MyUserName',j_password: 'MyPassword' }
}
);