我不确定这是否可行,或者我是否必须找到另一种方法但是......
我有一个使用Spring-Security-Core作为其身份验证系统的Grails应用程序。最终可能会部署一些位置,不同的位置可能会使用不同的身份验证提供程序(例如,一些将使用DAO,一些使用LDAP等)。
我希望能够根据.properties文件'auth.type = LDAP'或'auth.type = DAO'中的一行设置应用。一旦应用知道它将是哪一个,那么需要使用Config.groovy设置更多属性以满足此要求。
我正在考虑需要config.groovy来从.properties文件加载属性,然后在Config.groovy中使用switch语句中的这些属性来确定要设置的其他属性。但是我尝试了这个,但我相信在Config文件中,.properties文件中的属性是在配置文件中的所有属性之后加载的。这是正确的假设吗?
无论如何,如果有人知道实现这种影响的简单方法,将非常感激
答案 0 :(得分:1)
您是正确的,从grails.config.locations
文件(groovy或属性)读取的属性仅在读取主Config.groovy
后应用。您可以使用自定义环境,并使用
environments {
ldapAuth {
foo.bar = 'something'
}
daoAuth {
foo.bar = 'something else'
}
}
但是您必须在构建WAR时指定环境,不能构建一个WAR,然后在运行时为不同的环境配置它。
如果您有某种方法可以为运行WAR的tomcat(或其他)指定系统属性,那么您可以执行类似
的操作def authType = System.getProperty('myapp.auth.type', 'dao')
// store authType as a real config option as well as a local variable
auth.type = authType
switch(authType) {
case 'dao':
foo {
bar = 'something'
}
break
case 'ldap':
foo {
bar = 'something else'
}
break
default:
println "Unrecognised auth type ${authType}"
}
或者自己手动阅读.properties
文件(而不是依赖grails.config.locations
)
def authProps = new Properties()
new File('/etc/myapp/auth.properties').withInputStream(authProps.&load)
def authType = authProps.getProperty('auth.type', 'dao')
auth.type = authType
// switch(authType) as before
一个皱纹 - 在log4j
闭合中你可能会发现你无法访问authProps
变量(我不知道,我还没试过)。但是在该关闭内,您可以以config
的形式访问完整的配置,因此如果您拥有我上面使用的auth.type = authType
行,您只需说config.auth.type