我正在尝试直接从Cookie请求标头中挖掘到我以前存储在Play会话中的值
Http.Context.current().session().put("my-fancy-key", "some-interesting-value");
我只能访问play.mvc.Http.Request,我可以从中获取play.mvc.Http.Cookie ......但是从那里我绊倒了。
此代码段不起作用......提示?
注意:我完全愿意使用不在Play框架中的对象。我看到Netty有cookie r / w函数,我正在研究那些......也许是直接在javax中的东西?
String playSessionCookieName = Configuration.root().getString("session.cookieName", "PLAY_SESSION");
Http.Cookie playSessionCookie = r.cookie(playSessionCookieName);
if (playSessionCookie != null) {
// FIXME: What to do here to get my value?
Logger.debug("Found the cookie! Serialized value: " + playSessionCookie.value());
try {
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(playSessionCookie.value().getBytes()));
Http.Session session = (Http.Session) objIn.readObject();
// Here's the goal
Logger.debug("Found my value: " + session.get("my-fancy-key"));
} catch (Exception e) {
Logger.warn("Couldn't deserialize the value.", e);
}
}
答案 0 :(得分:2)
我不知道为什么你不使用简单的session(key)
获取会话值,但是如果你需要从会话cookie中获取会话值你可以使用类似的东西(播放2.0)。
String cookieVal = request().cookies().get("PLAY_SESSION").value();
cookieVal = cookieVal.substring(cookieVal.indexOf("-")+1);
for(String a: cookieVal.split("%00")) {
String[] k = a.split("%3A");
// k[0] - session key; k[1] - session value
Logger.info(k[0] + " = " + k[1]);
}
答案 1 :(得分:0)
Cookie有一个方法值()。我不知道它是否符合您的要求,但我会从那里开始。
答案 2 :(得分:0)
如果您有权访问结果,则可能更容易迭代存储在结果中的会话而不是cookie:
Session resultSession = play.test.Helpers.session(result);
for (Entry<String, String> entry : resultSession.entrySet()) {
System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
}