成功登录后续订JSESSIONID

时间:2019-01-24 12:14:18

标签: session jboss liferay hook jsessionid

我希望在用户正确登录时更改JSESSIONID ID。

我有自己的Hook来处理AutologinTokenClient库中的自动登录(first_hook),还有一个Hook来验证用户是否是管理员,将其重定向到管理面板(second_hook)。

我的配置:

[
    ['family'],
    'required',
    'when' => function ($model) {
        return $model->name != null || $model->patronymic != null;
    },
],
[
    ['name'],
    'required',
    'when' => function ($model) {
        return $model->family != null || $model->patronymic != null;
    },
],
[
    ['patronymic'],
    'required',
    'when' => function ($model) {
        return $model->family != null || $model->name != null;
    },
],

我试图将标记添加到我在第一个挂钩(first_hook)中修改的login.jsp中。但是它始终是相同的ID。

JBOSS: jboss-eap-6.4
Liferay Portal Enterprise Edition 6.2.10 EE GA1

在first_hook中,我有一个带有某些方法的postlogin类,我尝试放置以下函数,但是每次执行该函数时,我都会退出会话,必须重新登录。

<% @ page session = "false"%>

错误如下:

private void renewSessionID (HttpServletRequest request)
{
    LOG.info ("--------------------------------------------------------------------");
    HttpSession oldSession = request.getSession (true);
    LOG.info("OLD_SESSION: "+oldSession.getId ());

    Enumeration attrNames = oldSession.getAttributeNames ();
    Properties props = new Properties ();

    if (attrNames != null)
    {
        while (attrNames.hasMoreElements ())
        {
            String key = (String) attrNames.nextElement ();
            props.put (key, oldSession.getAttribute (key));
        }

        // Invalidating previous session
        oldSession.invalidate ();
        // Generate new session
        HttpSession newSession = request.getSession (true);
        attrNames = props.keys ();

        while (attrNames.hasMoreElements ())
        {
            String key = (String) attrNames.nextElement ();
            newSession.setAttribute (key, props.get (key));
        }

        LOG.info ("NEW_SESSION: "+newSession.getId ());
    }
}

我也尝试过将以下文本行添加到portal-ext.properties文件中,但是它没有任何改变:

Caused by: java.lang.IllegalStateException: JBWEB000043: Can not create a session after the response has been committed
at org.apache.catalina.connector.Request.doGetSession (Request.java:2649)
at org.apache.catalina.connector.Request.getSession (Request.java:2382)

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

您无需在自己的代码中添加逻辑即可提供此功能。您只需要在属性文件中启用此功能。

session.enable.phishing.protection = true

仅此而已。但是,您将无法检测到这种情况,因为在将控件转移回Liferay之后,会话将被更新,这意味着登录后您需要第二个钩子以验证ID是否已更改(或者您可以简单地检查在浏览器的Cookie视图中)。

关于此功能的实现:

request#getSession的Javadocs:

  

返回与此请求关联的当前HttpSession,或者返回   当前没有会话,并且create为true,则返回新会话。

     

如果create为false并且请求没有有效的HttpSession,则此   方法返回null。

     

要确保正确维护会话,必须调用此   提交响应之前的方法。如果容器正在使用   Cookies,以保持会话的完整性,并被要求创建一个新的   提交响应时的会话,则IllegalStateException是   抛出。

相关问题