在应用程序中加密db密码。conf For Play2.0/anorm
-
11-12-2019 - |
题
我不想将未加密的密码放在应用程序配置文件中。
这个问题: 在应用程序中加密db密码。conf 对这个问题有一个很好的解决方案,但它只适用于游戏1。
有谁知道一个适用于Play2.0的解决方案?我正在Scala版本的Play2.0.2中使用anorm。
解决方案 3
讨论了raffaele并遵循我对代码的调查,似乎玩2.0不允许您加密DB密码。
如果我错过了什么,请告诉我。
编辑:可以通过以下方式使用自定义数据库驱动程序来解决问题:
.
然后替换My.com.mydecryptive驱动程序替换application.conf / db..driver。不完美但为我工作... // Just redirect everything to the delegate
class DelegatingDriver(delegate: Driver) extends Driver
{
def connect(url: String, info: Properties) = delegate.connect(url, info)
def acceptsURL(url: String) = delegate.acceptsURL(url)
def getPropertyInfo(url: String, info: Properties) = delegate.getPropertyInfo(url, info)
def getMajorVersion = delegate.getMajorVersion
def getMinorVersion = delegate.getMinorVersion
def jdbcCompliant() = delegate.jdbcCompliant()
}
// Replace password in properties with the decrypted one
class MyDecryptingDriver extends DelegatingDriver(Class.forName("<my.original.Driver>").newInstance().asInstanceOf[Driver])
{
override def connect(url: String, info: Properties)= {
// copy Properties
val overriddenProperties= clone(info)
// override password property with the decrypted value
Option(info.getProperty("password")).foreach(value => overriddenProperties.setProperty("password", decryptPassword(value)))
super.connect(url, overriddenProperties)
}
def clone(orig: Properties)= {
val result= new Properties()
orig.propertyNames().map(_.asInstanceOf[String]).foreach(pName => result.setProperty(pName, orig.getProperty(pName)))
result
}
def decryptPassword(encrypted: String)= ...
}
其他提示
所有努力都是毫无意义的。当我们在数据库中放置哈希密码时,因为人类可以在其大脑中保留密码,而他们的大脑不可读取。它被称为不对称加密。
您正在谈论的事情只是可能与对称加密可能:程序有运行时的键,并使用此键解密DB密码。但是,存储用密钥加密的数据库密码,并且仍然具有此密钥可用的点? (这对Java来源和编译类是如此)。链条只有其最薄弱的环节。
当机器必须连接到DB时,它需要一个密码:我们以纯文本存储此密码,因为程序必须使用它,并且不需要人类输入。我们可以做的一切都要强制执行安全性是限制对此纯文本文件的访问,最终使用仅在admin的思维中存储的密码保护它(btw,更有可能的admin将保留其数据库中的所有密码,也许是主密码)。请注意,如果使用提到的播放插件,事情不会更改。
唯一介于我脑海中的其他事情是播放应用程序,只有当管理员输入DB密码时才连接到DB(但真的这只是一个思考练习)
我知道现在有点晚了,但没有关于这个问题的新讨论。我想分享实际的解决方案(播放v.2.5。X),如建议在 文件, ,现在可以复盖 GuiceApplicationLoader
配置 GuiceApplicationBuilder
处理一些初始配置。
在一个新的类 modules/ApplicationLoaderConfig.scala
:
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import javax.xml.bind.DatatypeConverter
import play.api.inject.guice._
import play.api.{ApplicationLoader, Configuration}
class ApplicationLoaderConfig extends GuiceApplicationLoader() {
override def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
// Decrypt secrets
val decryptedConfig = context.initialConfiguration ++
Configuration("config.to.descrypt.1" -> decryptDES(context.initialConfiguration.getString("config.to.descrypt.1").get)) ++
Configuration("config.to.descrypt.2" -> decryptDES(context.initialConfiguration.getString("config.to.descrypt.2").get))
initialBuilder
.in(context.environment)
.loadConfig(decryptedConfig)
.overrides(overrides(context): _*)
}
private def decryptDES(secret: String): String = {
val key = "12345678"
val skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "DES")
val cipher = Cipher.getInstance("DES/ECB/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, skeySpec)
new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(secret)))
}
}
还添加到 application.config
:
play.application.loader = "modules.ApplicationLoaderConfig"