Forums de discussion

how to set a cookie in an iframe portlet

Marie Piolot, modifié il y a 10 années.

how to set a cookie in an iframe portlet

New Member Publications: 12 Date d'inscription: 26/08/13 Publications récentes
Hi,
My name is Marie and I'm French.

I'm trying to write a Liferay portlet where I would embed a vcloud director URL : https://1.2.3.4/cloud/org/toto.
First, I have to login to vcloud by using the REST API of vCloud. I do it in Java. It gives me a token.

String httpsURL = "https://1.2.3.4/api/sessions";
URL url = new URL(httpsURL);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted (X509Certificate[] certs, String authType) {}
public void checkServerTrusted (X509Certificate[] certs, String authType) {}
}
};
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new SecureRandom());
conn.setSSLSocketFactory(sslContext.getSocketFactory());
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true); // true indicates POST request
conn.setRequestMethod("POST");
// headers de la requête POST
String notEncodedCredentials = "login@domain:password";
byte[] encodedCredentials = Base64.encodeBase64(notEncodedCredentials.getBytes());
conn.setRequestProperty("Authorization", "Basic " + new String(encodedCredentials));
conn.setRequestProperty("Accept", "application/*+xml;version=1.5");
// requête POST
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
String urlParameters = "";
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
// en-tête de la réponse
String token = conn.getHeaderField("x-vcloud-authorization");


Second, I must put this token in a cookie. I do it in Java.

// cookie
Cookie cookie = new Cookie("vcloud_session_id", token);
cookie.setPath("/");
cookie.setMaxAge(60 * 60 * 24 * 365);
cookie.setSecure(true);
response.addProperty(cookie);
request.setAttribute("set-cookie", cookie);


Third, I try to set the cookie in the view.jsp.

<%
// cookie
Cookie cookie = (Cookie) request.getAttribute("set-cookie");
String name = cookie.getName();
String value = cookie.getValue();
int maxAge = cookie.getMaxAge();
String path = cookie.getPath();
boolean secure = cookie.getSecure();
%>
<script type="text/javascript">
var date = new Date();
date.setTime(date.getTime() + maxAge);
document.cookie = name + "=" + value
+ "; expires=" + date.toUTCString()
+ "; path=" + path
+ "; secure";
</script>
<iframe
id="my_frame"
width="100%"
height="100%"
name="my_frame"
src="https://1.2.3.4/cloud/org/bluage">
</iframe>


For some reason, even though my browser does enable cookies, even thirs-party cookies, it does not work. I'd appreciate your help.

Thank you

Marie Piolot
thumbnail
ritresh girdhar, modifié il y a 10 années.

RE: how to set a cookie in an iframe portlet

Junior Member Publications: 67 Date d'inscription: 15/07/11 Publications récentes
Hi

Due to security reason , its not possible.
While integrating SugarCRM in liferay i find the same problem , After that i came to know that Iframe can not access resource's of parent window due to security reason.

Have a Good Day!!!

Regards
Rinku