Play 2.5 类型不匹配 Session 类型

Play 2.5 type mismatch Session type

使用 deadbolt2 我有以下控制器功能实现:

def restricted: Action = deadbolt.Restrict(List(Array(USER_ROLE)))() { request =>
  Future {
    val localUser = userProvider.getUser(request.session) // <<< expects a play.mvc.Http.Session
    Ok(views.html.restricted(userProvider, localUser))
  }
}

但它会导致以下编译器错误:

[error] /home/bravegag/code/play-authenticate-usage-scala/app/controllers/Application.scala:26: type mismatch;
[error]  found   : play.api.mvc.Session
[error]  required: play.mvc.Http.Session
[error]       val localUser = userProvider.getUser(request.session)
[error]                                                    ^

基本上当前的 request 给我 play.api.mvc.Session 但我依赖的库 (play-authenticate) 期望 play.mvc.Http.Session。有没有办法在两者之间进行转换?或另一种获取所需 Http 的方法?

我找到了如何从 play.api.mvc.Session 转换为 play.mvc.Http.Session:

import scala.collection.JavaConversions

val session : Http.Session = 
    new Http.Session(JavaConversions.mapAsJavaMap(request.session.data))

虽然我将不得不在所有地方重做......或者编写一个隐式转换助手对象。

UPDATE 定义了我自己的隐式转换助手:

package utils

import scala.collection.JavaConversions

object PlayConversions {
  /**
    * Returns the result conversion from a play.api.mvc.Session to a play.mvc.Http.Session
    * @param session play.api.mvc.Session instance
    * @return the result conversion from a play.api.mvc.Session to a play.mvc.Http.Session
    */
  implicit def toHttpSession(session: play.api.mvc.Session) = new play.mvc.Http.Session(JavaConversions.mapAsJavaMap(session.data))
}

UPDATE 实际上这是 Play institutional/preferred 的方式:

import play.core.j.JavaHelpers

val context = JavaHelpers.createJavaContext(request)
// and now access the Java Http.Session
context.session