从控制台使用 play.api.libs.Crypto

Using play.api.libs.Crypto from the console

scala> play.api.libs.Crypto.encryptAES("test")
com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'play.crypto.secret'

我已经在 application.conf 中设置了变量,但我不确定如何生成全局状态以便 Crypto 选取变量。

The documentation 不正确:

scala> import play.api._
import play.api._

scala> val application = new DefaultApplication(new File("."), this.getClass.getClassloader, None, Play.Mode.Dev)
<console>:14: error: not enough arguments for constructor DefaultApplication: (environment: play.api.Environment, applicationLifecycle: play.api.inject.DefaultApplicationLifecycle, injector: play.api.inject.Injector, configuration: play.api.Configuration, requestHandler: play.api.http.HttpRequestHandler, errorHandler: play.api.http.HttpErrorHandler, actorSystem: akka.actor.ActorSystem, plugins: play.api.Plugins)play.api.DefaultApplication.
Unspecified value parameters requestHandler, errorHandler, actorSystem...
      val application = new DefaultApplication(new File("."), this.getClass.getClassloader, None, Play.Mode.Dev)

我尝试加载到 test:console 并通过

加载
val application = new play.api.test.FakeApplication(additionalConfiguration = Map("play.application.secret" -> "foobar"))

但这并没有将它加载到全局配置对象中。

一种蛮力方式是从控制台启动应用程序,这取决于您是否自定义 ApplicationLoader。就我而言,我这样做了(这样我就可以将 macwire 用于 DI)。这是它对我的工作方式,我在 build.sbt:

中有这个
initialCommands in console := """
  import play.api.{ApplicationLoader, Environment, Mode}
  import com.projectname.apiserver.global.MacwireApplicationLoader
  import com.projectname.apiserver.model._
  val env = Environment(new java.io.File("."), this.getClass.getClassLoader, Mode.Dev)
  val context = ApplicationLoader.createContext(env)
  val loader = new MacwireApplicationLoader
  val registry = loader.loadRegistry(context)
  import registry._
"""

和自定义应用程序加载器:

/**
 * MacwireApplicationLoader replaces the default Play application loader with a
 * compile time DI system (using macwire).
 *
 * More info: https://www.playframework.com/documentation/2.5.x/ScalaCompileTimeDependencyInjection
 */
class MacwireApplicationLoader extends ApplicationLoader {
  def load(context: Context) = loadRegistry(context).application

  def loadRegistry(context: Context): BuiltInComponentsFromContext with Registry =
    new BuiltInComponentsFromContext(context)
      with AppComponents
      with ApplyEvolutions
}

trait AppComponents
    extends BuiltInComponents
    with NingWSComponents
    with Registry {

  lazy val assets: Assets = wire[Assets]
  lazy val config = configuration
  lazy val app = application
  lazy val ws = wsClient

  lazy val router: Router = {
    lazy val prefix = "/"
    wire[Routes]
  }

}

trait ApplyEvolutions extends EvolutionsComponents {
  applicationEvolutions
  override def dynamicEvolutions = new DynamicEvolutions
}

然后当我进入控制台时,我可以这样做:

[api-server] $ console
[info] Starting scala interpreter...
[info]
2016-07-23 11:11:01,106 [INFO] [p.a.d.DefaultDBApi] - Database [default] connected at jdbc:postgresql://localhost:5432/databasename
import play.api.{ApplicationLoader, Environment, Mode}
import com.projectname.apiserver.global.MacwireApplicationLoader
import com.projectname.apiserver.model._
env: play.api.Environment = Environment(.,scala.tools.nsc.interpreter.IMain$TranslatingClassLoader@5b0c7029,Dev)
context: play.api.ApplicationLoader.Context = Context(Environment(.,scala.tools.nsc.interpreter.IMain$TranslatingClassLoader@5b0c7029,Dev),None,play.core.DefaultWebCommands@352ca33b,Configuration(Config(SimpleConfigObject({"akka":{"actor":{"creation-timeout":"20s","debug":{"autoreceive":"off","event-stream":"off","fsm":"off","lifecycle":"off","receive":"off","router-misconfiguration":"off","unhandled":"off"},"default-dispatcher":{"attempt-teamwork":"on","default-executor":{"fallback":"fork-join-executor"},"executor":"defa...Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.

scala> crypto.encryptAES("test")
res0: String = 2-ZLIW79WqQff4SDZ+aWLkf38cZyU=