Set Liferay Hook on session timeout

2020-06-27 04:15发布

问题:

I want to write a Hook in Java that is executed if the session of my Liferay 5.2.3 Portal times out.

I managed to write a Hook that is executed whenever the user clicks the logout link with the following setup in the liferay-hook.xml:

<hook>
    <event>
        <event-class>com.extensions.hooks.LogoutHook</event-class>
        <event-type>logout.events.pre</event-type>
    </event>
</hook>

However the Logout Hook does not get called if the session times out, but I need to execute the same method on a timeout. I did not find an event-type for a session timeout.

Is there a way to execute a Java-Method when the session times out and identify the User-ID of the ended session?

回答1:

There is an event which will be triggered upon Session Expiry/TimeOut event of User Session,

# Servlet session destroy event
servlet.session.destroy.events = com.extensions.hooks.CustomPreSessionExpireAction

You can either add this property in liferay-hook.xml or portal.properties [Written in Hook] or portal-ext.properties.

And can be used as ,

public class CustomPreSessionExpireAction extends SessionAction {

    @Override
    public void run(HttpSession session) throws ActionException {
        //Code
    }
}

However, We can only use HttpSession here. So, you need to figure out the way to get userId here.

Thanks