liferayliferay-6liferay-ideliferay-velocityliferay-aui

How to handle invalid authentication in liferay sign in portlet


Im trying to customize the login portlet for my purpose using this portlet login sample.

But when i login with test@liferay.com with a wrong password it shows "web page is not available"

SignInPortlet.java

public class SignInPortlet extends MVCPortlet {

    @Override
    public void processAction(
            ActionRequest actionRequest, ActionResponse actionResponse)
        throws IOException, PortletException {

        String className = "com.liferay.portlet.login.action.LoginAction";
        PortletConfig portletConfig = getPortletConfig();
        NoRedirectActionResponse noRedirectActionResponse =
            new NoRedirectActionResponse(actionResponse);

        try {
            PortletActionInvoker.processAction(
                className, portletConfig, actionRequest,
                noRedirectActionResponse);
        }
        catch (Exception e) {
            _log.error(e, e);
        }

        String login = ParamUtil.getString(actionRequest, "login");
        String password = ParamUtil.getString(actionRequest, "password");
        String rememberMe = ParamUtil.getString(actionRequest, "rememberMe");

        if (Validator.isNull(noRedirectActionResponse.getRedirectLocation())) {
            actionResponse.setRenderParameter("login", login);
            actionResponse.setRenderParameter("rememberMe", rememberMe);
        }
        else {
            String redirect =
                PortalUtil.getPathMain() + "/portal/login?login=" + login +
                    "&password=" + password + "&rememberMe=" + rememberMe;

            actionResponse.sendRedirect(redirect);
        }
    }

    private static Log _log = LogFactoryUtil.getLog(SignInPortlet.class);

}

View.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<%@ page import="com.liferay.portal.CookieNotSupportedException" %>
<%@ page import="com.liferay.portal.NoSuchUserException" %>
<%@ page import="com.liferay.portal.PasswordExpiredException" %>
<%@ page import="com.liferay.portal.UserEmailAddressException" %>
<%@ page import="com.liferay.portal.UserLockoutException" %>
<%@ page import="com.liferay.portal.UserPasswordException" %>
<%@ page import="com.liferay.portal.UserScreenNameException" %>
<%@ page import="com.liferay.portal.kernel.language.LanguageUtil" %>
<%@ page import="com.liferay.portal.kernel.util.ClassResolverUtil" %>
<%@ page import="com.liferay.portal.kernel.util.Constants" %>
<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>
<%@ page import="com.liferay.portal.kernel.util.HtmlUtil" %>
<%@ page import="com.liferay.portal.kernel.util.MethodKey" %>
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="com.liferay.portal.kernel.util.PortalClassInvoker" %>
<%@ page import="com.liferay.portal.kernel.util.PropsUtil" %>
<%@ page import="com.liferay.portal.model.Company" %>
<%@ page import="com.liferay.portal.security.auth.AuthException" %>

<%@ page import="javax.portlet.WindowState" %>

<portlet:defineObjects />

<liferay-theme:defineObjects />

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

        <%
        String signedInAs = user.getFullName();

        if (themeDisplay.isShowMyAccountIcon()) {
            signedInAs = "<a href=\"" + themeDisplay.getURLMyAccount().toString() + "\">" + signedInAs + "</a>";
        }
        %>

        <%= LanguageUtil.format(pageContext, "you-are-signed-in-as-x", signedInAs, false) %>
    </c:when>
    <c:otherwise>

        <%
        MethodKey methodKey = new MethodKey(ClassResolverUtil.resolveByPortalClassLoader("com.liferay.portlet.login.util.LoginUtil"), "getLogin", HttpServletRequest.class, String.class, Company.class);

        String login = GetterUtil.getString((String)PortalClassInvoker.invoke(false, methodKey, request, "login", company));

        boolean rememberMe = ParamUtil.getBoolean(request, "rememberMe");
        %>

        <portlet:actionURL var="loginURL" />

        <aui:form action="<%= loginURL %>" method="post" name="fm">
            <aui:input name="saveLastPath" type="hidden" value="<%= false %>" />
            <aui:input name="<%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />
            <aui:input name="rememberMe" type="hidden" value="<%= rememberMe %>" />

            <liferay-ui:error exception="<%= AuthException.class %>" message="authentication-failed" />
            <liferay-ui:error exception="<%= CookieNotSupportedException.class %>" message="authentication-failed-please-enable-browser-cookies" />
            <liferay-ui:error exception="<%= NoSuchUserException.class %>" message="please-enter-a-valid-login" />
            <liferay-ui:error exception="<%= PasswordExpiredException.class %>" message="your-password-has-expired" />
            <liferay-ui:error exception="<%= UserEmailAddressException.class %>" message="please-enter-a-valid-login" />
            <liferay-ui:error exception="<%= UserLockoutException.class %>" message="this-account-has-been-locked" />
            <liferay-ui:error exception="<%= UserPasswordException.class %>" message="please-enter-a-valid-password" />
            <liferay-ui:error exception="<%= UserScreenNameException.class %>" message="please-enter-a-valid-screen-name" />

            <table class="lfr-table">
            <tr>
                <td>
                    <aui:input name="login" style="width: 120px;" type="text" value="<%= HtmlUtil.escape(login) %>" />
                </td>
            </tr>
            <tr>
                <td>
                    <aui:input name="password" style="width: 120px;" type="password" value="" />

                    <span id="<portlet:namespace />passwordCapsLockSpan" style="display: none;"><liferay-ui:message key="caps-lock-is-on" /></span>
                </td>
            </tr>

            <c:if test='<%= company.isAutoLogin() && !GetterUtil.getBoolean(PropsUtil.get("session.disabled")) %>'>
                <tr>
                    <td>
                        <aui:input checked="<%= rememberMe %>" name="rememberMe" type="checkbox" />
                    </td>
                </tr>
            </c:if>

            </table>

            <br />

            <input type="submit" value="<liferay-ui:message key="sign-in" />" />
        </aui:form>

        <c:if test="<%= renderRequest.getWindowState().equals(WindowState.MAXIMIZED) %>">
            <aui:script>
                Liferay.Util.focusFormField(document.<portlet:namespace />fm.<portlet:namespace />login);
            </aui:script>
        </c:if>

        <aui:script use="aui-base">
            A.one('#<portlet:namespace />password').on(
                'keypress',
                function(event) {
                    Liferay.Util.showCapsLock(event, '<portlet:namespace />passwordCapsLockSpan');
                }
            );
        </aui:script>
    </c:otherwise>
</c:choose>

Does any one know how to solve this?


Solution

  • I changed the portlet code like this .Now it is ok

    public class SignInPortlet extends MVCPortlet {
    
    @Override
    public void processAction(
    ActionRequest actionRequest, ActionResponse actionResponse)
    throws IOException, PortletException {
    
    String className = "com.liferay.portlet.login.action.LoginAction";
    PortletConfig portletConfig = getPortletConfig();
    NoRedirectActionResponse noRedirectActionResponse =
    new NoRedirectActionResponse(actionResponse);
    
    try {
    PortletActionInvoker.processAction(
    className, portletConfig, actionRequest,
    noRedirectActionResponse);
    }
    catch (Exception e) {
    _log.error(e, e);
    }
    
    String login = ParamUtil.getString(actionRequest, "login");
    String password = ParamUtil.getString(actionRequest, "password");
    String rememberMe = ParamUtil.getString(actionRequest, "rememberMe");
    Object object = SessionErrors.get( actionRequest, AuthException.class.getName() );
    if (( object != null ) || Validator.isNull(noRedirectActionResponse.getRedirectLocation())) {
    actionResponse.setRenderParameter("login", login);
    actionResponse.setRenderParameter("rememberMe", rememberMe);
    }
    else {
    String redirect =
    PortalUtil.getPathMain() + "/portal/login?login=" + login +
    "&password=" + password + "&rememberMe=" + rememberMe;
    
    actionResponse.sendRedirect(redirect);
    }
    }
    
    private static Log _log = LogFactoryUtil.getLog(SignInPortlet.class);
    
    }