【问题标题】:How to configure ldap with spring 4.0spring 4.0如何配置ldap
【发布时间】:2020-02-15 04:12:36
【问题描述】:

我在使用 openldap 进行身份验证时遇到问题,我不知道如何配置 spring 安全性,使用 Spring 4.0.0 版本的 ldap。请提供示例参考。

<beans:bean id="contextSource"
    class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
    <beans:constructor-arg
        value="ldap://localhost:389/dc=test,dc=com" />
</beans:bean>
<security:ldap-server id="contextSource"
    url="ldap://localhost:389/dc=test,dc=com" />


<beans:bean id="authMgr"
    class="org.springframework.security.authentication.ProviderManager">
    <beans:constructor-arg>
        <beans:list>
            <beans:bean id="ldapAuthProvider"
                class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
                <beans:constructor-arg>
                    <beans:bean
                        class="org.springframework.security.ldap.authentication.BindAuthenticator">
                        <beans:constructor-arg ref="contextSource" />
                        <beans:property name="userDnPatterns">
                            <beans:list>
                                <beans:value>uid={0},ou=users</beans:value>
                            </beans:list>
                        </beans:property>
                    </beans:bean>
                </beans:constructor-arg>
            </beans:bean>
        </beans:list>
    </beans:constructor-arg>
</beans:bean>
<security:authentication-manager>
    <security:ldap-authentication-provider
        server-ref="contextSource" user-search-base="ou=users"
        user-search-filter="(uid={0})" group-search-filter="ou=groups">
        <security:password-compare hash="{sha}"
            password-attribute="userPassword" />
    </security:ldap-authentication-provider>
</security:authentication-manager>

【问题讨论】:

    标签: spring-mvc spring-security spring-security-ldap


    【解决方案1】:

    Spring Security 4.0 LDAP Reference

    LDAP 最佳实践是搜索条目的 DN,然后进行配置

        <bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
          <constructor-arg index="0" value=""/>
          <constructor-arg index="1" value="(uid={0})"/>
          <constructor-arg index="2" ref="contextSource" />
        </bean>
    

    通常 LDAP 静态组条目的命名属性是 cn,因此配置

    <bean class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
        <constructor-arg ref="contextSource"/>
        <constructor-arg value="ou=groups"/>
        <property name="groupRoleAttribute" value="cn"/>
    </bean>
    

    而不是指南中显示的示例

    【讨论】:

    • 我已经编辑了我的问题并粘贴了 xml 配置,如果有任何不正确,请提出建议,虽然我输入了正确的 DN,但我也收到错误为“LDAP:错误代码 34 - 无效 DN”。
    • 虽然我输入了正确的密码,但我遇到了错误的凭据异常。在 ldap 中,我已将密码加密配置为 sha,并且在 spring xml 中相同。
    • 各位大侠帮帮忙!
    • LDAP 最佳实践是让 LDAP 服务器在客户端发送 LDAP BIND 请求时进行密码比较。 docs.spring.io/spring-security/site/docs/4.0.x/reference/html/…
    猜你喜欢
    • 1970-01-01
    • 2015-04-11
    • 2015-05-05
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    相关资源
    最近更新 更多