Play Framework:如何阅读由未知密钥组成的整个配置部分

时间:2014-03-25 12:57:38

标签: scala playframework typesafe-config

以下是我要为Play应用配置安全配置文件的方式 - auth.securityProfiles中的每个条目都包含Operation => Roles对:

auth {
    securityProfiles {
        myOperation1 = "author, auditor"
        myOperation2 = "admin"
        myOperationN = "auditor, default"
    }
}

如何阅读auth.securityProfiles部分中的所有条目以生成Map这样的内容?

val securityProfiles = Map(
  "myOperation1" -> "author, auditor",
  "myOperation2" -> "admin",
  "myOperationN" -> "auditor, default"
)

感谢。

1 个答案:

答案 0 :(得分:0)

这是我的解决方案......我刚刚修改了这样的配置......

auth {
    securityProfiles = [
        {
            operation = "myOperation1"
            roles = ["author", "auditor"]
        }
        {
            operation = "myOperation2"
            roles = ["admin"]
        }
        {
            operation = "myOperationN"
            roles = ["auditor", "default"]
        }
    ]
}

...然后使用以下代码片段阅读它:

import scala.collection.mutable.Map

var securityProfiles = Map[String, List[String]]().withDefaultValue(List.empty)
  configuration.getConfigList("auth.securityProfiles").map { _.toList.map { config =>
    config.getString("operation").map { op =>
      securityProfiles += (op -> config.getStringList("roles").map(_.toList).getOrElse(List.empty))
  }
}}

我希望有所帮助。