目次

Version 3, last updated by Peter Robinett at Nov 02 07:28 UTC

If you want your user to stay logged across client and server restarts then you need to use extended sessions. Luckily if you’re using Mapper Lift already has you covered in the form of ProtoExtendedSession. The following simple example, based upon the Apache ESME implementation and discussion on the mailing list, will walk you through the process.

First, you will need to create a new model. Here’s ESME’s ExtSession model:

package org.apache.esme.model
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import net.liftweb._
import mapper._
import util._
import common._

object ExtSession extends ExtSession with MetaProtoExtendedSession[ExtSession] {
  override def dbTableName = "ext_session" // define the DB table name

  def logUserIdIn(uid: String): Unit = User.logUserIdIn(uid)

  def recoverUserId: Box[String] = User.currentUserId

  type UserType = User
}

class ExtSession extends ProtoExtendedSession[ExtSession] {
  def getSingleton = ExtSession // what's the "meta" server
}

As you can see, there are some simple methods to log the user in again if they aren’t already, and it is told what model represents the user, in this case User.

Next you need to make sure that Lift actually uses your extended session system. First you need to tell User about the extended session. This is very simple, all you do is add a function to the User companion object’s onLogIn var:

onLogIn = List({ ExtSession.userDidLogin _ })

Then in Boot.scala add:

S.addAround(ExtSession.requestLoans)

By default this means that every stateful request will try to restore the user session – this includes static files served by your Lift app. You can prevent Lift from serving static files like so:

LiftRules.liftRequest.append { 
  case Req("favicon" :: Nil, "ico", GetRequest) => false
  case Req(_, "css", GetRequest) => false 
  case Req(_, "js", GetRequest) => false 
}

If you’re using the included Javascript files that Lift produces for you, make sure to allow Lift to serve them:

LiftRules.liftRequest.append { 
  case Req("classpath" :: _, _, _) => true
  case Req("favicon" :: Nil, "ico", GetRequest) => false
  case Req(_, "css", GetRequest) => false 
  case Req(_, "js", GetRequest) => false 
}