我有一个外部YML文件,其中包含一些grails配置。在此文件中,添加的配置之一是用于 grails spring 安全 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 快速配置提供)应用程序 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"中。请注意,这两个都存在,但我猜测默认值会覆盖自定义属性。
我是否需要在本地(非圣杯嵌入)Tomcat服务器上更改其他内容以允许外部属性工作?我尝试更改应用程序.yml(外部)的位置,但无济于事。
我在这里注意到的奇怪之处在于,拦截 UrlMap 是唯一无法从外部 YML 文件加载的调用。由于这是当时文档中唯一提供的用于静态路由的方法,因此我采用了不同的方法。(使用外部时髦配置,而不是 YML 配置)
以下是我为使用 LDAP 插件进行外部配置所做的工作列表。首先,我确保我的应用程序启动运行类(Application.groovy)实现了环境感知。我将 setEnvironemnt 方法覆盖如下:
@Override
void setEnvironment(Environment environment) {
try {
String configPath = System.getenv("local.config.location")
def configFile = new File(configPath)
def config = new ConfigSlurper().parse(configFile.toURI().toURL())
environment.propertySources.addFirst(new MapPropertySource("externalGroovyConfig", config))
} catch (Exception e) {
log.error("unable to load the external configuration file", e)
}
}
接下来,我创建了一个 application.groovy 文件,并将其放在另一个位置(不在我的项目中)我的应用程序.groovy 文件现在如下所示:
grails.plugin.springsecurity.ldap.context.managerDn = 'uid=admin,ou=system'
grails.plugin.springsecurity.ldap.context.managerPassword = 'secret'
grails.plugin.springsecurity.ldap.context.server = 'ldap://localhost:10389/'
grails.plugin.springsecurity.ldap.authorities.groupSearchBase = 'ou=Groups,dc=c3cen,dc=com'
grails.plugin.springsecurity.ldap.authorities.retreiveGroupRoles = true
grails.plugin.springsecurity.ldap.authorities.retreiveDatabaseRoles = false
grails.plugin.springsecurity.ldap.authorities.groupSearchFilter = 'member={0}'
grails.plugin.springsecurity.ldap.search.base = 'ou=Users,dc=c3cen,dc=com'
grails.plugin.springsecurity.password.algoritham = 'SHA-256'
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
[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']]
]
grails.plugin.springsecurity.filterChain.chainMap = [
[pattern: '/assets/**', filters: 'none'],
[pattern: '/**/js/**', filters: 'none'],
[pattern: '/**/css/**', filters: 'none'],
[pattern: '/**/images/**', filters: 'none'],
[pattern: '/**/favicon.ico', filters: 'none'],
[pattern: '/**', filters: 'JOINED_FILTERS']
]