【发布时间】:2016-06-25 16:18:14
【问题描述】:
我正致力于在 Grails 3.x 中外部化 .YML 文件。完成这项工作的代码如下:
在我的 Application.groovy 中,我正在从 EnvironmentAware 接口实现 setEnviroment 方法。
@Override
void setEnvironment(Environment environment) {
try {
String configPath = System.properties["local.config.location"]
def ymlConfig = new File(configPath)
Resource resourceConfig = new FileSystemResource(ymlConfig)
YamlPropertiesFactoryBean ypfb = new YamlPropertiesFactoryBean()
ypfb.setResources(resourceConfig)
ypfb.afterPropertiesSet()
Properties properties = ypfb.getObject()
environment.propertySources.addFirst(new PropertiesPropertySource("local.config.location", properties))
} catch (Exception e) {
log.error("unable to load the external configuration file", e)
}
}
我已将构建中的 bootRun 任务编辑为:
bootRun {
jvmArgs = ['-Dlocal.config.location=external-config.yml']
}
当打印出 setEnvironment 方法中的值时,确实是从加载的对象中读取和添加属性。
现在,有趣的部分。当我将此代码添加到我的原始 application.yml 文件时:
---
grails:
plugin:
springsecurity:
securityConfigType: 'InterceptUrlMap'
interceptUrlMap: [
{pattern: '/**', access: ['permitAll']}
]
providerNames: ['ldapAuthProvider', 'anonymousAuthenticationProvider']
ldap:
context:
managerDn: 'uid=admin,ou=system'
managerPassword: 'secret'
server: 'ldap://localhost:10389'
authorities:
groupSearchBase: 'ou=Groups,dc=aye,dc=com'
retreiveGroupRoles: true
retreiveDatabaseRoles: false
groupSearchFilter: 'member={0}'
search:
base: 'ou=Users,dc=aye,dc=com'
password:
algoritham: 'SHA-256'
---
一切正常。当我将它剪切并粘贴到外部 yml 文件中时,我在 Firefox 中得到了这个漂亮的错误。
我可以看出提供的代码中的配置是正确的,因为我可以添加更多角色和过滤器,并且在原始 application.yml 文件中一切正常。只有从外部文件读取时才会失败。如果我从两个 .yml 文件 .ofc 中删除安全代码,我的页面看起来很奇怪,但 firefox 错误消失了。
有人知道为什么会这样吗?
【问题讨论】:
-
我们做同样的事情,但使用 Groovy 外部文件而不是 YML,它可以正常工作。我不是 YML 方面的专家,但可能有些东西被错误地合并到那里的配置中。
-
是的,我真的不敢说实话。也许有什么东西触发它不被解释为可以使用的资源
-
@droggo 你的文件名字也一样吗?
-
不,groovy 文件不需要同名
-
那就更奇怪了