Writing a Custom Login Portlet

If you need to customize your users’ authentication experience completely, you can write your own Login Portlet. The mechanics of this on the macro level are no different from writing any other portlet, so if you need to familiarize yourself with that, please see the portlets section of tutorials.

This tutorial shows only the relevant parts of a Liferay MVC Portlet that authenticates the user. You’ll learn how to call the authentication pipeline and then redirect the user to a location of your choice.

Authenticating to Liferay DXP

You can use the example project in this ZIP file as a starting point for your own.

It has only one view, which is used for logging in or showing the user who is already logged in:

<%@ include file="/init.jsp" %>

    <b><liferay-ui:message key="myloginportlet_MyLogin.caption"/></b>

    <c:when test="<%= themeDisplay.isSignedIn() %>">

        String signedInAs = HtmlUtil.escape(user.getFullName());

        if (themeDisplay.isShowMyAccountIcon() && (themeDisplay.getURLMyAccount() != null)) {
            String myAccountURL = String.valueOf(themeDisplay.getURLMyAccount());

            signedInAs = "<a class=\"signed-in\" href=\"" + HtmlUtil.escape(myAccountURL) + "\">" + signedInAs + "</a>";

        <liferay-ui:message arguments="<%= signedInAs %>" key="you-are-signed-in-as-x" translateArguments="<%= false %>" />
        String redirect = ParamUtil.getString(request, "redirect");
        <portlet:actionURL name="/login/login" var="loginURL">
            <portlet:param name="mvcRenderCommandName" value="/login/login" />
        <aui:form action="<%= loginURL %>" autocomplete='on' cssClass="sign-in-form" method="post" name="loginForm">
            <aui:input name="saveLastPath" type="hidden" value="<%= false %>" />
            <aui:input name="redirect" type="hidden" value="<%= redirect %>" />
            <aui:input autoFocus="true" cssClass="clearable" label="email-address" name="login" showRequiredLabel="<%= false %>" type="text" value="">
                <aui:validator name="required" />

            <aui:input name="password" showRequiredLabel="<%= false %>" type="password">
                <aui:validator name="required" />
                <aui:button cssClass="btn-lg" type="submit" value="sign-in" />

Note that in the form, authentication by email address (the default setting) is hard-coded, as this is an example project. The current page is sent as a hidden field on the form so the portlet can redirect the user to it, but you can of course set this to any value you want.

The portlet handles all processing of this form using a single Action Command (imports left out for brevity):

    property = {
    service = MVCActionCommand.class
public class MyLoginMVCActionCommand extends BaseMVCActionCommand {

    protected void doProcessAction(ActionRequest actionRequest,
            ActionResponse actionResponse) throws Exception {

        ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
        HttpServletRequest request = PortalUtil.getOriginalServletRequest(
        HttpServletResponse response = PortalUtil.getHttpServletResponse(

        String login = ParamUtil.getString(actionRequest, "login");
        String password = actionRequest.getParameter("password");
        boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");
        String authType = CompanyConstants.AUTH_TYPE_EA;
            request, response, login, password, rememberMe, authType);


The only tricky/unusual code here is the need to grab the HttpServletRequest and the HttpServletResponse. This is necessary to call Liferay DXP’s API for authentication. At the end of the Action Command, the portlet sends a redirect that sends the user to the same page. You can of course make this any page you want.

Implementing your own login portlet gives you complete control over the authentication process.

Password-Based Authentication Pipelines

Auto Login

« Password-Based Authentication PipelinesService Access Policies »
Este artigo foi útil?
Utilizadores que acharam útil: 0 de 0