Spring security ldap baseDN

时间:2015-10-08 21:43:10

标签: spring-security spring-security-ldap

我试图在我的Spring ldap上下文源上配置baseDN,但它不断抛出异常:

配置如下:

<beans:bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
        <beans:constructor-arg index="0" value="${ldap_server}"/>
        <beans:constructor-arg index="1" value="${ldap_searchbase}"/>
    </beans:bean>

我的ldap_searchbase中有一个空格,我查看了Spring代码:导致问题:

public DefaultSpringSecurityContextSource(String providerUrl) {
        Assert.hasLength(providerUrl, "An LDAP connection URL must be supplied.");

        StringTokenizer st = new StringTokenizer(providerUrl);

        ArrayList<String> urls = new ArrayList<String>();

        // Work out rootDn from the first URL and check that the other URLs (if any) match
        while (st.hasMoreTokens()) {
            String url = st.nextToken();
            String urlRootDn = LdapUtils.parseRootDnFromUrl(url);

            urls.add(url.substring(0, url.lastIndexOf(urlRootDn)));

            logger.info(" URL '" + url + "', root DN is '" + urlRootDn + "'");

            if (rootDn == null) {
                rootDn = urlRootDn;
            } else if (!rootDn.equals(urlRootDn)) {
                throw new IllegalArgumentException("Root DNs must be the same when using multiple URLs");
            }
        }

我得到&#34;使用多个网址时,根DN必须相同&#34;错误,我注意到字符串标记符按空格标记,因此它会扼杀我的baseDN并使其成为一个单独的LDAP服务器URL。是什么赋予了?我在这里做错了什么?

如果我这样配置,我会得到同样的问题(显然):

<beans:bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
        <beans:constructor-arg index="0" value="${ldap_server}/${ldap_searchbase}"/>
    </beans:bean>

2 个答案:

答案 0 :(得分:0)

好吧 - 所以我还没弄明白我如何为安全上下文源提供搜索基础,但通过这样做:

<beans:bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
        <beans:constructor-arg index="0" value="${ldap_server}"/>
    </beans:bean>

<beans:bean id="ldapUserSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
        <beans:constructor-arg index="0" value="${ldap_searchbase}"/>
        <beans:constructor-arg index="1" value="${ldap_auth_search_filter}"/>
        <beans:constructor-arg index="2" ref="contextSource" />
    </beans:bean>

它有效。

答案 1 :(得分:0)

DefaultSpringSecurityContextSource的编写方式使其本身无法处理空格。

这首先被报告为ISSUE-2264中的错误。 但是根据错误的注释,这似乎是一个已知问题,解决方案建议使用转义符(即%20) 有关LDAP URL的更多示例,请参见:Section-3

因此,当您定义LDAP DN时,请执行以下操作:

真实网址:

ldap://ldap.itd.umich.edu/o=University of Michigan,c=US

用于DefaultSpringSecurityContextSource参数的更正URL:

ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US