How to Hook Login Events in Liferay 7 ?
In this tutorial we are going to learn how we can hook the Login POST and PRE actions in liferay 7. To hook the post login or pre login event we need to use the liferay's com.liferay.portal.kernel.events.LifecycleAction class to process the events.
As we know that everything in liferay 7 is a component and we need to define this too as a component using the @Component annotation. In the component we need to define the LifecycleAction.class as a service. So the final @Component setup will look like this.
For Post Login Event we need to setup the @Component like this -:
@Component(
immediate = true, property = {"key=login.events.post"},
service = LifecycleAction.class
)
For Pre Login Event we need to setup the @Component like this -:
@Component(
immediate = true, property = {"key=login.events.pre"},
service = LifecycleAction.class
)
In both of the cases we need to implement the method processLifecycleEvent(LifecycleEvent lifecycleEvent) .
So below is the code snippet for both of the events.
Code for LoginPostAction
Code for LoginPreAction
For Post Login Event we need to setup the @Component like this -:
@Component(
immediate = true, property = {"key=login.events.post"},
service = LifecycleAction.class
)
For Pre Login Event we need to setup the @Component like this -:
@Component(
immediate = true, property = {"key=login.events.pre"},
service = LifecycleAction.class
)
So below is the code snippet for both of the events.
Code for LoginPostAction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.liferayrevisited.events; | |
import org.osgi.service.component.annotations.Component; | |
import com.liferay.portal.kernel.events.ActionException; | |
import com.liferay.portal.kernel.events.LifecycleAction; | |
import com.liferay.portal.kernel.events.LifecycleEvent; | |
/** | |
* @author LiferayRevisited | |
*/ | |
@Component( | |
immediate = true, property = {"key=login.events.post"}, | |
service = LifecycleAction.class | |
) | |
public class LoginPostAction implements LifecycleAction { | |
@Override | |
public void processLifecycleEvent(LifecycleEvent lifecycleEvent) throws ActionException { | |
System.out.println(" ==== >> >> CALLING THE LOGIN POST ACTION ==== >>> >>>" ); | |
} | |
} |
Code for LoginPreAction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.liferayrevisited.events; | |
import org.osgi.service.component.annotations.Component; | |
import com.liferay.portal.kernel.events.ActionException; | |
import com.liferay.portal.kernel.events.LifecycleAction; | |
import com.liferay.portal.kernel.events.LifecycleEvent; | |
/** | |
* @author LiferayRevisited | |
*/ | |
@Component( | |
immediate = true, property = {"key=login.events.pre"}, | |
service = LifecycleAction.class | |
) | |
public class LoginPreAction implements LifecycleAction { | |
@Override | |
public void processLifecycleEvent(LifecycleEvent lifecycleEvent) throws ActionException { | |
System.out.println("LoginPreAction.processLifecycleEvent() ==== >> >> CALLING THE LOGIN PRE ACTION ==== >>> >>>" ); | |
} | |
} |
how to create post logout hook ?
ReplyDelete