如何在 Play Scala 应用程序中使用密钥

How to use secret key in Play Scala application

我在 Play Scala 项目的 application.conf 中有 secret key

# See http://www.playframework.com/documentation/latest/ApplicationSecret for more details.
application.secret="........"

如何在我的控制器或服务中使用它?

我尝试使用 application.secret,它给我错误 value secret is not a member of controllers.Application。 WIth play.crypto.secret 它给出 object crypto is not a member of package play

Play 提供 Configuration 对象以从 application.conf 键访问配置键。

以下是您如何获得播放 Configuration 对象的权限。

class HomeController @Inject() (configuration: play.api.Configuration) extends Controller {
  def foo = Action {

    val appSecretString = configuration.underlying.getString("application.secret")

    //do something with app secret
    Ok(appSecretString) //app secret should not be exposed, just trying to show it on browser, but do not do this in production
  }
}