Forum

Accueil » Liferay Portal » English » 3. Development

Vue combinée Vue Plate Arborescence
Sujets [ Précédent | Suivant ]
toggle
Marie Piolot
how to set a cookie in an iframe portlet
5 novembre 2013 02:18
Réponse

Marie Piolot

Rang: New Member

Publications: 12

Date d'inscription: 26 août 2013

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
Ritresh Girdhar
RE: how to set a cookie in an iframe portlet
5 novembre 2013 02:28
Réponse

Ritresh Girdhar

Rang: Junior Member

Publications: 67

Date d'inscription: 15 juillet 2011

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