2016-03-10 79 views
1

我正在外化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中遇到了這個漂亮的錯誤。

enter image description here

我可以告訴大家,在配置中提供的代碼是正確的,因爲我可以添加更多的角色和過濾器,和一切工作就好了原application.yml文件時。只有在從外部文件讀取時纔會失敗。如果我從兩個.yml文件中刪除安全代碼,.ofc,我的頁面看起來很奇怪,但firefox錯誤消失。

有沒有人有一個想法,爲什麼這可能是這種情況?

+0

我們做同樣的事情,但使用Groovy外部文件,而不是的YML,它的工作沒有問題。我不是YML的專家,但也許有些東西被錯誤地合併到了那裏的配置中。 – droggo

+0

是啊,我真的不確定是誠實的。也許是引發它不被解釋爲可以使用的資源 – angryip

+0

@droggo是你的文件也被命名爲相同? – angryip

回答

3

你可以嘗試測試它是否可以工作的一件事是將你的external-config.yml文件重命名爲application.yml。除非另有說明,我相信默認名稱應該是應用程序。

This article here shows a good example of its correct use

當你這樣做,嘗試在你的類之一讀取性能,以確保該YML文件合併。您可以使用如下命令讀取這些屬性:

grailsApplication.config.getProperty("grails.plugin.springsecurity.securityConfigType") 

或者,你可以打印出所有的人,用持有人實用

def config = Holders.config 
+1

似乎解決我的合併問題。嗯,有趣。 – angryip

+0

非常好!有時候,它只需要來自外部的一點幫助。 –