我正在开发外部化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错误消失了。
有没有人知道为什么会出现这种情况?
答案 0 :(得分:3)
您可以尝试测试它是否有效,将您的external-config.yml文件重命名为application.yml。我认为默认名称应该是application,除非另有说明。
This article here shows a good example of its correct use
执行此操作时,请尝试阅读其中一个类中的属性,以确保yml文件正在合并。您可以使用如下命令读取这些属性:
grailsApplication.config.getProperty("grails.plugin.springsecurity.securityConfigType")
或者,您可以使用“持有人”实用程序
打印出所有这些内容def config = Holders.config