如何将用户名/密码命令行选项传递给Config.groovy中的jndi grails.naming.entries

时间:2011-09-21 16:54:00

标签: grails passwords jndi

我在Config.groovy中有一个jndi条目,如下:

grails.naming.entries = ['jdbc/test_me': [
    type: "javax.sql.DataSource", //required
        auth: "Container", // optional
        description: "Data source for ...", //optional
        //properties for particular type of resource
    url: "jdbc:oracle:thin:@testserver:1521:SID",
    username: "someuser",
    password: "somepassword",
    driverClassName: "oracle.jdbc.driver.OracleDriver",
    maxActive: "8", //and so on
        maxIdle: "4"
    ]
]

这很好但我不想在Config.groovy源中存储用户名/密码。有没有办法将凭证从命令行选项-Duser = someuser -Dpass-somepassword传递给Config.groovy中的grails.naming.entries?

2 个答案:

答案 0 :(得分:0)

您最好的选择是使用外部存储的配置设置。

这允许Grails加载生产(或测试或开发)服务器的唯一设置,这些设置不存储在grails应用程序WAR中。另一个好处是可以在不更换任何代码的情况下更新这些内容,只需重新启动服务器上的应用程序即可。

来自this great article on the subject的示例:

// Put this at the top of your Config.groovy
// Copied from http://blog.zmok.net/articles/2009/04/22/playing-with-grails-application-configuration
if(System.getenv("MY_GREAT_CONFIG")) {
    println( "Including configuration file: " + System.getenv("MY_GREAT_CONFIG"));
    grails.config.locations << "file:" + System.getenv("MY_GREAT_CONFIG")
} else {
    println "No external configuration file defined."
}

现在将环境变量MY_GREAT_CONFIG设置为外部groovy配置的绝对路径。请参阅链接以获取更完整的示例。

答案 1 :(得分:0)

似乎在Config.groovy中没有通过grails.config.locations添加的任何选项。 “$ {System.getProperty('password')}”。toString()是唯一有效的方法。 以下是我的测试结果:

在Config.groovy的开头添加:

if (new File("${userHome}/.grails/${appName}-config.groovy").exists()){
    grails.config.locations = ["file:${userHome}/.grails/${appName}-config.groovy"]
}

在Config.groovy结束时添加:

println "(*) grails.config.locations = ${grails.config.locations}"
def f = new File("${userHome}/.grails/${appName}-config.groovy")
f.eachLine{ line -> println line }
println "test password: ${testPassword}" // same result ([:]) with grails.config.testPassword
println "${System.getProperty('password')}"

grails.naming.entries = ['jdbc/test_mnr': [
    type: "javax.sql.DataSource", //required
    auth: "Container", // optional
    description: "Data source for ...",
    url: "jdbc:oracle:thin:@server:1521:SID",
    username: "username",
    password: "${System.getProperty('password')}".toString(),
    driverClassName: "oracle.jdbc.driver.OracleDriver",
    maxActive: "8",
    maxIdle: "4",
    removeAbandoned: "true",
    removeAbandonedTimeout: "60",
    testOnBorrow: "true",
    logAbandoned: "true",
    factory: "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory",
    validationQuery: "select count(*) from dual",
    maxWait: "-1"
  ]
]

user.home / .grails / mnroad-config.groovy的内容:

testPassword='some_password'

使用-Dpassword = somePassword:

运行时的结果如下
(*) grails.config.locations = [file:C:\Documents and Settings\carr1den/.grails/mnroad-config.groovy]
testPassword=some_password
test password: [:]
somePassword

应用初始化后,grailsApplication.config.testPassword选项可用。