3

How to I retrieve the authToken for the current page in Liferay Velocity?

For example, in the HTML code there is the following but I do not know what is the coding to retrieve the corresponding value 0H4mKLWq for the auth Token assigned for the current session of the webpage.

The following is the string in the HTML:

Liferay.authToken="0H4mKLWq";
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Lester
  • 31
  • 1

1 Answers1

2

In Freemarker you can do it using static method:

<#assign authTokenUtil = staticUtil["com.liferay.portal.security.auth.AuthTokenUtil"] />
${authTokenUtil.getToken(request)}

In velocity you can call static methods by adding AuthTokenUtil to context or loading it using Class.forName

#set($authTokenUtil=$String.class.forName('com.liferay.portal.security.auth.AuthTokenUtil'))
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • I tried the following in my Velocity template to retrieve the authToken of the current session but it doesn't seem to work and the string "$authToken" is used in the href code instead of the value of $authToken in the set statement: – Lester Sep 26 '19 at 03:32
  • #set($authTokenUtil = $String.class.forName("com.liferay.portal.security.auth.AuthTokenUtil")) #set($authToken = $authTokenUtil.getAuthToken()) – Lester Sep 26 '19 at 03:35
  • What should I do in order to retrieve the Liferay.authToken value (which is "0H4mKLWq" in this example as seen in the following towards the end of the following code Liferay.authToken="0H4mKLWq";) for the current HTML session that I can see in the header session of the HTML code like the following: – Lester Sep 26 '19 at 03:35
  • Some Title – Lester Sep 26 '19 at 03:35