我有一个外部YML文件,其中包含一些grails配置 . 在此文件中,添加的配置之一是grails spring security ldap插件 . 我的配置如下:
---
grails:
plugin:
springsecurity:
ldap:
context:
managerDn: 'uid=admin,ou=system'
managerPassword: 'secret'
server: 'ldap://localhost:10389'
authorities:
groupSearchBase: 'ou=Groups,dc=c3cen,dc=com'
retreiveGroupRoles: true
retreiveDatabaseRoles: false
groupSearchFilter: 'member={0}'
search:
base: 'ou=Users,dc=c3cen,dc=com'
password:
algoritham: 'SHA-256'
interceptUrlMap: [
{pattern: '/', access: ['permitAll']},
{pattern: '/error', access: ['permitAll']},
{pattern: '/index', access: ['permitAll']},
{pattern: '/index.gsp', access: ['permitAll']},
{pattern: '/shutdown', access: ['permitAll']},
{pattern: '/assets/**', access: ['permitAll']},
{pattern: '/**/js/**', access: ['permitAll']},
{pattern: '/**/css/**', access: ['permitAll']},
{pattern: '/**/images/**', access: ['permitAll']},
{pattern: '/**/favicon.ico', access: ['permitAll']},
{pattern: '/login/**', access: ['permitAll']},
{pattern: '/logout/**', access: ['permitAll']}
]
---
我还有常规(由grails quick config提供)应用程序yml文件中的一些属性 . 该文件仅包含:
grails:
plugin:
springsecurity:
securityConfigType: 'InterceptUrlMap'
providerNames: ['ldapAuthProvider', 'anonymousAuthenticationProvider']
我通过覆盖Application.groovy类中的setEnvironment方法在grails中加载外部配置 . 它看起来如下:
@Override
void setEnvironment(Environment environment) {
try {
String configPath = System.getenv("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)
}
}
当我在grails中发出run-app命令并部署到我的嵌入式tocat时,一切都按预期工作 . 当我手动部署到我的本地tomcat时,我在firefox中得到“页面没有正确重定向”错误 .
注意:我已使用日志语句确认两个tomcat服务器正在读取外部文件 . 奇怪的是,属性正在被注入,但它们被默认提供的字符串覆盖 . 例如:dc = example显示在search.base中,但在上面的代码中,您可以清楚地看到它位于'ou = Users,dc = c3cen,dc = com'中 . 请注意,这两个都存在,但我猜我的默认是覆盖自定义属性 .
我需要在我的本地(非grails嵌入式)Tomcat服务器上进行更改以允许外部属性工作吗?我试过改变application.yml(外部的)的位置无济于事 .
1 回答
我在这里注意到的奇怪部分是interceptUrlMap是唯一无法从外部YML文件加载的调用 . 由于这是当时用于静态路由的文档中唯一提供的方法,我采取了不同的路线 . (使用外部groovy配置而不是yml配置)
以下是我使用LDAP插件进行外部配置所做的事情列表 . 首先,我确保我的应用程序启动运行类(Application.groovy)实现了EnvironmentAware . 我重写了setEnvironemnt方法,如下所示:
接下来,我创建了一个application.groovy文件,并将其放在备用位置(不在我的项目中)我的application.groovy文件,现在看起来如下: