Play Framework Guice Dependency Injection ProvisionError

时间:2017-05-03 22:31:29

标签: scala playframework guice bouncycastle

我有一个像这样构建的类<​​/ p>

class CryptoManager @Inject()(config: Configuration) {

  Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())

  //Static values for Public Key Encryption
  private val pubkf = KeyFactory.getInstance("ECDSA", "BC")
  private val cspec = ECNamedCurveTable.getParameterSpec("secp192r1")
  private val privkeyString = config.getString("crypto.ECDSA.privkey").get
  private val r = new BigInteger(privkeyString, 16)
  private val ec = new ECPrivateKeySpec(r, cspec)
  private val privKey = pubkf.generatePrivate(ec)
...
}

当我在测试中明确地构建它时,它可以正常工作:

   val config = Configuration.from(Map(
      "crypto" -> Map(
        "ECDSA" -> Map(
          "privkey" -> "b005ec459ad9482997757093df4c70732452083529617227",
          "pubkey" -> "04a4ec3b4e144434283a28c129ad5f2c13effc8dca6c79987bb8298c9be41e9c2f2176c9cacb2da156a095e68383a9f053",
          "bitlength" -> 192),
        "AES" -> Map(
          "secretkey" -> "ee21affe373c6e0e324fee2c0a6060e"
        ),
        "AESMAC" -> Map(
          "secretkey" -> "b9d1c0381f0ac08b27dcb945ff48c533"
        ))))

    val cryptoManager = new CryptoManager(config)

但是当我尝试使用Guice将此类的实例注入另一个类的构造函数时,我收到以下错误

play.api.UnexpectedException: Unexpected exception[ProvisionException: Unable to provision, see the following errors:

1) Error injecting constructor, java.security.spec.InvalidKeySpecException: key spec not recognized
  at com.nexkey.managers.CryptoManager.<init>(CryptoManager.scala:117)
  while locating com.nexkey.managers.CryptoManager
    for parameter 1 at com.nexkey.controllers.MessageController.<init>(MessageController.scala:23)
  at com.nexkey.controllers.MessageController.class(MessageController.scala:23)
  while locating com.nexkey.controllers.MessageController
    for parameter 7 at router.Routes.<init>(Routes.scala:51)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application

我确认应用程序的application.conf文件中的值与我在测试文件中构建的配置对象中的值相同。我还确认privkeyString中的值在两种情况下都是相同的。

我注意到如果我提供如下配置而不是注入它,一切正常:

val config = ConfigFactory.defaultApplication()
val conf = Configuration(config)

但我明白这是做错的“错误”方式,我更愿意弄清楚为什么Guice无法建立我的课程。

0 个答案:

没有答案