我正在外化Grails 3.x中的.YML文件。使這項工作的代碼如下:外化Grails 3 Spring安全配置
在我的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
是啊,我真的不確定是誠實的。也許是引發它不被解釋爲可以使用的資源 – angryip
@droggo是你的文件也被命名爲相同? – angryip