Introduction
Liferay provides a feature to execute code before and after specific events, such as Login and Logout, as well as services. Here we are discussing the login and logout events.
Prerequisites
Liferay
Basic knowledge of Liferay
Purpose
Managing session data
Track user activity
Redirect User
Clearing session data
Show personalized data
Steps to create pre-login events.
Step 1 : Create a module
First, create the service module in the Liferay workspace.
In the reference Image, we have a constant file for all the strings used in code.
Step 2: Register a LifecycleAction
To register a lifecycle action, add the LifecycleAction class in the Component annotation.
Initialize the key in an OSGI component so Liferay can recognize whether it is a pre-event or post-event for login or logout.
- The LifecycleAction class in Liferay is an interface located within the com.liferay.portal.kernel.events package. It serves as a mechanism to define actions that are executed during specific lifecycle events within the Liferay DXP platform.
@Component(
property = {
"key=login.events.post"
},
service = LifecycleAction.class
)
- For login and logout events, keys will be changed as per the table.
Events | Login | Logout |
Pre | login.events.pre | logout.events.pre |
Post | login.events.post | logout.events.post |
Pre-login events execute before a user is authenticated and run custom logic such as modifying login data or applying extra security.
Post-login events execute after a successful login, allowing for actions like redirecting the user, logging user activity, or initializing session data.
Step 3: Override methods of the LifecycleAction interface
After registering the services with OSGi, we need to implement a LifecycleAction interface.
- Once we implement the interface, we need to override its methods.
Here, the processLifecycleEvent method is overridden.
This method contains the custom logic for login or logout events.
Note :
Here, an Activity object is created to store the user’s activity data.
The activity object contains three fields :
IPAddress
Activity Type (Type of activity the user is performing)
Detail (Email or any other information of the user)
Here are some reference images of the activity object.
The processLifecycleEvent method contains the logic of fetching all the fields of the activity object and adding an entry in the activity object to monitor users’ activity when a user logs in.
EmployeeLoginConstant.java
package com.employee.login.constant;
public class EmployeeLoginConstant {
public static final String ACTIVITY = "C_ACTIVITY";
public static final String ACTIVITY_TYPE = "activityType";
public static final String DETAILS = "details";
public static final String IGNEK_INTRANET = "Ignek";
public static final String IP_ADDRESS = "iPAddress";
public static final String LOGIN ="LOGIN";
public static final String LOGOUT = "LOGOUT";
}
EmployeeLoginEvents.java
package com.employee.login.events;
public class EmployeeLoginEvents implements LifecycleAction {
@Override
public void processLifecycleEvent(LifecycleEvent lifecycleEvent) throws ActionException {
try {
log.info("Post Login event is Started");
HttpServletRequest request = portal.getOriginalServletRequest(lifecycleEvent.getRequest());
String ipAddress = request.getRemoteAddr();
log.info("Client IP Address: " + ipAddress);
long companyId = portal.getUser(request).getCompanyId();
Group group = groupLocalService.getGroup(companyId, EmployeeLoginConstant.IGNEK_INTRANET);
long groupId = group.getGroupId();
ObjectDefinition objectDefinition =
objectDefinitionLocalService.fetchObjectDefinitionByExternalReferenceCode(
EmployeeLoginConstant.ACTIVITY,companyId);
if (Validator.isNotNull(objectDefinition)) {
ServiceContext serviceContext = new ServiceContext();
Map values = new HashMap<>();
values.put(EmployeeLoginConstant.ACTIVITY_TYPE,EmployeeLoginConstant.LOGIN);
values.put(EmployeeLoginConstant.DETAILS, portal.getUser(request).getEmailAddress());
values.put(EmployeeLoginConstant.IP_ADDRESS, ipAddress);
log.info("Values are set for activity");
ObjectEntry objectEntry = objectEntryLocalService
.addObjectEntry(portal.getUser(request).getUserId() ,groupId,
objectDefinition.getObjectDefinitionId(), values ,serviceContext);
log.info("Object entry created with ID: " + objectEntry.getObjectEntryId());
}
} catch(Exception e) {
log.info("Error occured while generation activity entry "+ e);
}
}
private static final Log log = LogFactoryUtil.getLog(EmployeeLoginEvents.class);
@Reference
private ObjectEntryLocalService objectEntryLocalService;
@Reference
private ObjectDefinitionLocalService objectDefinitionLocalService;
@Reference
private GroupLocalService groupLocalService;
@Reference
private Portal portal;
}
Code Explanation :
- First, fetch the HttpServletRequest from the lifecycleEvent object.
- Fetch an activity object definition with the help of companyID and ExternalReferenceCode.
- After fetching the object definition, create a Map<String, Serializable> data-type variable to store all the values.
- At last, add values to the object entry.
Now, whenever a user logs in, an entry in the activity object will be created with these fields :
- Activity ID
- Details (email ID of that user)
- IPAddress (IPAddress of the user device)
- Activity type
For reference, here is the image of the activity object data stored in the database.
This way, you can map the users’ activity while the user logs in and write any custom logic according to your business requirements.
Conclusion :
Log-in and Log-out events are for customizing your Liferay portal according to your needs. These particular events can be used for monitoring user activity, clearing data, or redirecting the user.

