我是新来的。到目前为止,我一直在使用MVC模型并使用基本会话管理模型,即在会话中存储令牌并检查每个请求。
我试图用电梯做同样的事,但我的会话突然过期了。甚至有一段时间我刚刚登录并退出了。我有分析,每当我得到这样的日志消息:
INFO - Session ucjrn5flnq9q1ke52z5zixgtt expired
我已经搜索过,但我找不到任何一步一步的导师
答案 0 :(得分:2)
会话由servlet容器管理。你在用哪一个?您应该查看容器的文档。
答案 1 :(得分:2)
请勿尝试使用S.get等人访问会话绑定信息。这很危险。这样做:
class Thing {
object SessionThing extends SessionVar[Box[String]](Empty)
...
def someMethod = {
...
SessionThing.is // returns you a Box[String].
// operates on the session variable if it exists,
// otherwise provides a sensible default
SessionThing.is.map(_.toLowerCase).openOr("default")
...
}
}
你需要真正理解片段和状态生命周期,因为你似乎还没有完全理解电梯的会话机制是如何工作的。
答案 2 :(得分:1)
我找到了问题的解决方案。我使用的是嵌入式jetty服务器,我使用ServletContextHandler来注册提升过滤器。我将其更改为WebAppContext,它开始正常工作。
普尼特
答案 3 :(得分:0)
感谢您的回复。我试过3个解决方案来存储和检查会话数据。
def render = {
// define some variables to put our values into
var userid = ""
// process the form
def process() {
CustomerDbFactory.default.findByCustomerId(userid) match {
//customer found. redirecting to customer home
case Some(x) =>
S.set("USER_ID", userid)
S.redirectTo("/epc-home")
//user not found redirect to index page
case None =>
S.unset(USER_ID)
S.redirectTo("/")
}
}
"name=userid" #> SHtml.onSubmit(epcid = _) & // set the name
"type=submit" #> SHtml.onSubmitUnit(process)
}
def loggedIn_? : Boolean = id match {
case "" => false
case _ => true
}
def id = {
S.get(EPC_ID) openOr ""
}
def render = {
// define some variables to put our values into
var userid = ""
// process the form
def process() {
CustomerDbFactory.default.findByCustomerId(epcid) match {
//customer found. redirecting to customer home
case Some(x) =>
S.setSessionAttribute("USER_ID", userid)
S.redirectTo("/epc-home")
//user not found redirect to index page
case None =>
S.unsetSessionAttribute(USER_ID)
S.redirectTo("/")
}
}
"name=userid" #> SHtml.onSubmit(epcid = _) & // set the name
"type=submit" #> SHtml.onSubmitUnit(process)
}
def loggedIn_? : Boolean = id match {
case "" => false
case _ => true
}
def id = {
S.getSessionAttribute(EPC_ID) openOr ""
}
object Home{
def render = {
// define some variables to put our values into
var userid = ""
// process the form
def process() {
CustomerDbFactory.default.findByCustomerId(epcid) match {
//customer found. redirecting to customer home
case Some(x) =>
userId(userid)
S.redirectTo("/epc-home")
//user not found redirect to index page
case None =>
userId("")
S.redirectTo("/")
}
}
"name=userid" #> SHtml.onSubmit(epcid = _) & // set the name
"type=submit" #> SHtml.onSubmitUnit(process)
}
def loggedIn_? : Boolean = id match {
case "" => false
case _ => true
}
def id = {
userId.is
}
}
object userId extends SessionVar[String]("")
我无法理解我犯错误的地方。另外,我使用嵌入式Jetty作为容器,我使用ServletContextHandler来注册LiftFilter。
ServletContextHandler的初始化如下:
val ctx = new ServletContextHandler(this, "/", true, false)
ctx.addFilter(classOf[LiftFilter],"/*", 1)
分析完成:
我在处理请求时尝试打印会话ID。我发现即使会话记录器打印出特定会话已过期,我的请求也会被一个具有相同会话ID的会话处理。
请给我一些指针来解决这个问题。
非常感谢, 普尼特