【问题标题】:Using play.api.libs.Crypto from the console从控制台使用 play.api.libs.Crypto
【发布时间】:2016-07-22 07:28:56
【问题描述】:
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"))

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

【问题讨论】:

  • 使用 Crypto 的构造函数并传入 crypto config 怎么样?
  • @rethab 它正在图书馆中以这种方式使用。
  • 不确定我是否明白你在说什么。在标题中我读到“从控制台”
  • @rethab 我正在使用控制台中的库。

标签: scala playframework playframework-2.4


【解决方案1】:

蛮力方法是从控制台内启动应用程序,这取决于您是否自定义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=

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    相关资源
    最近更新 更多