Google App Engine会话失去了属性 [英] Google App Engine session loses attribute

查看:197
本文介绍了Google App Engine会话失去了属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序存储已经工作多年的会话授权详细信息,现在session属性返回null。我在每个请求开始时检索会话数据,并在返回响应之前设置它。该应用程序是运行GAE标准的Spring 3 MVC。

I have an application that stores session authorization details that has been working for years, now the session attribute is returning null. I retrieve the session data at the beginning of every request, and set it before returning the response. The application is Spring 3 MVC running on GAE standard.

会话已启用,< sessions-enabled> true< / sessions-enabled> 。我的所有对象都实现了Serializable。 UPDATE - 在检查项目历史记录时,我最近升级到了Java 8.我恢复到Java 7,并且会话在设置后仍然返回null属性。回到Java 8。

Sessions are enabled, <sessions-enabled>true</sessions-enabled>. All of my objects implement Serializable. UPDATE - In checking the project history, I recently upgraded to Java 8. I reverted to Java 7, and the session is still returning a null attribute after being set. Set back to Java 8.

protected void setSessionAccess(HttpServletRequest request, Access access) {
    logger.info("setting session access...");
    request.getSession().setAttribute(ACCESS, access);
}

protected Access getSessionAccess(HttpServletRequest request) {
    logger.info("getting session access...");
    Access access = (Access) request.getSession().getAttribute(ACCESS);
    if (access == null) {
        logger.log(Level.WARNING, "access is null, initializing access...");
        access = new Access();
    }
    return access;
}

在排除故障时,我添加了< async- session-persistence enabled =true/> ,并将其删除,因为会话数据也用于导航目的。

While troubleshooting, I had added <async-session-persistence enabled="true" />, and removed it because the session data is also used for navigation purposes.

更新 - 用户报告他们能够与某些浏览器/平台保持会话,而不是其他浏览器/平台,这让我相信这与cookie有关。我禁用会话cookie时可以复制该问题。 URL重写可以解决这个问题,但为什么会突然中断呢?

UPDATE - Users are reporting they're able to maintain session with some browsers/platforms, and not others, leading me to believe this is cookie related. I can duplicate the issue when I disable session cookies. URL rewriting would resolve this issue, but why did this break all of a sudden?

非常感谢任何帮助!

推荐答案

我向公共问题跟踪器提交了一份请求, Ggrimaldo 建议。

I submitted a request with the public issue tracker as Ggrimaldo suggests.

他们确认这很可能与浏览器相关问题,因为某些浏览器和平台不会丢失会话。他们建议使用会话ID设置cookie,如果会话数据为null,则读取会话ID。我发现这个发布描述如何从数据存储区读取_ah_session数据,并以这种方式实现,

They confirmed it's most likely a browser related issue, since sessions aren't lost with some browsers and platforms. They suggested setting a cookie with the session id, and reading the session id if the session data was null. I found this post describing how to read the _ah_session data from the datastore, and implemented it this way,

try {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key sessionKey = KeyFactory.createKey("_ah_SESSION", "_ahs" + sessionId);
    Entity sessionEntity = datastore.get(sessionKey);
    Blob blob = (Blob) sessionEntity.getProperty(_VALUES);
    byte[] sessionBytes = blob.getBytes();

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(sessionBytes);
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    return (Map<String, Object>) objectInputStream.readObject();
} catch (EntityNotFoundException e) {   
    logger.log(Level.INFO, e.getMessage());
} catch (Exception e) {
    logger.log(Level.SEVERE, e.getMessage(), e);
}

从此处调用,

String mySessionId = null;
for (Cookie cookie : cookies) {
    if ("MYSESSIONID".equals(cookie.getName())) {
        mySessionId = cookie.getValue();
    }
}
// returning code is above
sessionMap = gcpSessionService.getGcpSession(swgcSessionId);
if (sessionMap != null) {
    access = (Access) sessionMap.get(ACCESS);
}

我会要求我的用户重试并稍微查看日志。这是一个特别恼人的问题,但由于我将该网站作为俱乐部的志愿者维护,因此没有任何人想要接受维护的投诉。我会发布更新。如果jsessionid是易变的,我将不得不求助于创建我自己的cookie(argh)。

I'll ask my users to retry and watch the logs a bit. This has been a particularly annoying issue, but since I maintain this site as a volunteer for a club, no complaints from anyone wanting to take up maintenance. I'll post an update. If the jsessionid is volatile, I'll have to resort to creating my own cookie (argh).

所以,这是我的cookie设置代码,从其他地方复制stackoverflow,

So, argh, here's my cookie setting code, copied from elsewhere on stackoverflow,

protected void setMySessionCookie(HttpServletResponse response, String jSessionId) {

    logger.log(Level.INFO, "setting MYSESSIONID = " + jSessionId);

    final String cookieName = "MYSESSIONID";
    // you could assign it some encoded value
    final String cookieValue = jSessionId; 
    // TODO enforce SSL
    final Boolean useSecureCookie = false; 
    final int expiryTime = -1; // 24h in seconds
    final String cookiePath = "/";

    Cookie cookie = new Cookie(cookieName, cookieValue);
    // determines whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL
    cookie.setSecure(useSecureCookie);
    // A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.
    cookie.setMaxAge(expiryTime);
    // The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories
    cookie.setPath(cookiePath);

    response.addCookie(cookie);
}

我在会话首次建立时设置此项,并在每次更新时更新会话已保存。每次会话属性保存在会话中时,都会有一些额外的条件逻辑用于设置或更新cookie中的会话值。它工作正常,我的用户很高兴。

I set this when the session is first established, and update it each time the session is saved. There's some additional conditional logic for setting or updating the session value in the cookie each time the session attribute is saved in the session. It's working and my users are happy.

首先没有明显的原因发生这种情况,所以希望没有人会碰到它。我这样做,投票或发表评论让我知道如果只是我,谢谢!

There's no obvious reason why this occurred in the first place, so hopefully no one will run into it. I you do, vote up or leave a comment so I get an idea if it's just me, thanks!

这篇关于Google App Engine会话失去了属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆