Getting the Liferay Session ID in Javascript

Issue

  • We can't extract the session ID using JavaScript, because the Liferay.ThemeDisplay.getSessionId() function consistently returns an empty string.

Environment

  • Liferay DXP 7.4

Resolution

  • By default, the Liferay.ThemeDisplay.getSessionId() function will always return a blank string. To change this, you need to adjust your settings.
  • This is how you can resolve the issue:
    1. You need to set the property 'session.enable.url.with.session.id' to 'true' within the portal-ext.properties file.
    2. Once this change is made, the session ID can be retrieved using the Liferay.ThemeDisplay.getSessionId() function.

Additional Information

  • While this solution will allow you to retrieve the session ID, it's crucial to understand the potential risk involved:
    • The 'session.enable.url.with.session.id' property is usually set to 'false' to safeguard against potential security risks associated with session IDs being visible.
    • Keeping session identifiers in cookies is generally safer and more efficient.
    • This is because if a session ID is attached to a URL, there's a risk that if you share that URL, you could inadvertently share your session with the recipient if the code doesn't manage this scenario appropriately.
  • Follow the link for more information regarding Session Management in Liferay.
Was this article helpful?
1 out of 1 found this helpful