Liferay - PreLoginAction - 阻止用户登录

时间:2016-07-11 08:06:37

标签: java tomcat liferay liferay-hook

我使用基于Liferay的网站并尝试实施登录挂钩,以防止用户在特定条件下登录。我希望尽可能简单:

    @Override
    public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException
    {
        if (condition)
        {
            //redirect to login page, send a message back but just don't let the user login
            //but don't block or ban him either.
        }
    }

它必须看起来像一个错误的密码错误,但它必须在门​​户端,在用户甚至登录之前(因此PreLoginAction类)

编辑:好的,我使用了注销和重定向方法返回登录页面。但我仍然想生成错误消息。我试过这样的

    @Override
    public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException
    {
        if (condition)
        {
            SessionErrors.add(request, "error");
            response.sendRedirect("/c/portal/logout");
        }
    }

并且对于login.jsp我添加了顶行,但我猜它对于Login Actions不起作用。没有显示错误消息

<liferay-ui:error key="error" message="this-account-has-been-locked" />
<liferay-ui:error exception="<%= AuthException.class %>" message="authentication-failed" />
<liferay-ui:error exception="<%= CompanyMaxUsersException.class %>" message="unable-to-login-because-the-maximum-number-of-users-has-been-reached" />
<liferay-ui:error exception="<%= CookieNotSupportedException.class %>" message="authentication-failed-please-enable-browser-cookies" />
<liferay-ui:error exception="<%= NoSuchUserException.class %>" message="authentication-failed" />
<liferay-ui:error exception="<%= PasswordExpiredException.class %>" message="your-password-has-expired" />
<liferay-ui:error exception="<%= UserEmailAddressException.class %>" message="authentication-failed" />
<liferay-ui:error exception="<%= UserLockoutException.class %>" message="this-account-has-been-locked" />
<liferay-ui:error exception="<%= UserPasswordException.class %>" message="authentication-failed" />
<liferay-ui:error exception="<%= UserScreenNameException.class %>" message="authentication-failed" />

还有其他方法吗? 我也试过了这行

request.getSession().setAttribute("loginError", "failure message");

并尝试在jsp中阅读${loginError},但它也无效。

2 个答案:

答案 0 :(得分:1)

您只需将用户重定向到response.redirect("/c/portal/logout")即可实际注销用户并将其重定向到主页。

如果您想以编程方式自行注销用户,然后重定向,则可以使用以下代码:

    @Override
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {

    if (condition) {

        // Whatever here ...

        // Logout the user.
        request.getSession().invalidate();

        // Redirect
        response.redirect(<YOUR_PAGE>)
    }
}

答案 1 :(得分:0)

您可以使用验证管道来制作检查条件的验证器

public class CustomAuth implements Authenticator {
    @Override
    public int authenticateByEmailAddress(long companyId, String emailAddress, String password, Map<String, String[]> headerMap, Map<String, String[]> parameterMap) throws AuthException {
        return 0;
    }

    @Override
    public int authenticateByScreenName(long companyId, String screenName, String password, Map<String, String[]> headerMap, Map<String, String[]> parameterMap) throws AuthException {
        return 0;
    }

    @Override
    public int authenticateByUserId(long companyId, long userId, String password, Map<String, String[]> headerMap, Map<String, String[]> parameterMap) throws AuthException {
        return 0;
    }
}

将您的身份验证器添加到portal.properties

中的管道
auth.pipeline.pre=package.CustomAuth

auth.pipeline.post=package.CustomAuth

或者您可以使用身份验证验证程序

public class CustomVerifier implements AuthVerifier{
    @Override
    public String getAuthType() {
        return null;
    }

    @Override
    public AuthVerifierResult verify(AccessControlContext accessControlContext, Properties properties) throws AuthException {
        return null;
    }
}

并将其配置为portal.properties

中的管道
auth.verifier.pipeline=

在您的情况下,最好的选择可能是 AuthVerifier