开发者

Setting cookie in LiftFilter

How do I set a cookie in a LiftFilter.doFilter method?

I tried to set the cookie as follows (stripped some code):

class AuthenticationFilter extends LiftFilter {

  override def  doFilter(request: ServletReque开发者_JAVA技巧st, response: ServletResponse , chain: FilterChain) {

      val cookie = new HTTPCookie("SomeCookie", Full("" + System.nanoTime), Empty,
        Full("/authentication"), Full(60 * 60 * 24 * 14), Empty, Empty)
      cookie.setPath("/somePath")
      S.addCookie(cookie)

      val httpResp = response.asInstanceOf[HttpServletResponse]
      httpResp.sendRedirect("/some/page.html")

  }
}

However, when I check the browsers cookie, no cookie is set (apart from JSESSIONID), and I know the doFilter method is being executed because of logging messages and the fact that the browser is redirected to /some/page.html.

I'm using Scala 2.8, Lift 2.1-SNAPSHOT and the app is running is GAE (1.3.6, only tested on dev_appserver so far).

Any ideas? Thanks,

Gero


I posted the same question to the Lift discussion list and David Pollak pointed me in the right direction.

What I actually wanted to achieve was to be able to use a cookie value (if present) to retrieve some information of the user. There's no need to set a cookie for that in the filter, but the use of the LiftFilter in itself was in appropriate as David pointed out. You should not subclass the LiftFilter, but instead do the following in your Boot.scala:

LiftRules.statelessDispatchTable.prepend {
  case req if !checkReqForCookies(req) => () =>
    Full(RedirectResponse(whereTo, cookie1, cookie2))
}

def checkReqForCookies(in: Req): Boolean {
   ... do your checks ...
}

Works like a charm for me :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜