将X509值传递给LDAP服务器

时间:2013-01-28 23:57:11

标签: java spring spring-security ldap x509

我正在努力将spring security应用到我的项目中。我需要使用Spring Security从证书CN中提取的用户名作为LDAP服务器上的uid。我不确定解决这个问题的正确方法。我不确定如何将CN值从x509传递到LDAP身份验证器。有人以前做过这个或者有任何想法吗?

注意:我不需要将整个证书传递给LDAP服务器,因为它们不存储在那里,只需要存储CN中的用户名。

编辑:以下是我的spring security xml文件中的一些配置:

<http>
    <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <anonymous />
    <x509 subject-principal-regex="CN=(.*?)," /> <!-- user-service-ref needed? -->
</http>

<ldap-server id="ldapServer" url="ldap://localhost:389/dc=example,dc=com" manager-dn="cn=manager,dc=example,dc=com" manager-password="myPassword" />

<authentication-manager>
    <ldap-authentication-provider
       user-dn-pattern="uid={0},ou=people"
       user-search-filter="(uid={0})"
       user-search-base="ou=people,dc=example,dc=com"
       group-search-filter="(member={0})"
       group-search-base="ou=groups"
       group-role-attribute="cn" />        
</authentication-manager>

1 个答案:

答案 0 :(得分:1)

您应该使用ldap-user-service。这是Spring Security测试套件中的an example configuration。然后,Spring Security应自动替换它从证书CN中提取的用户名,而不是LDAP搜索过滤器中的{0}标记。

ldap-authentication-provider元素用于使用用户名和密码对用户进行身份验证,这不是您想要的X.509,其中容器对证书的验证被视为执行身份验证。 / p>

Spring Security的X.509身份验证过滤器需要UserDetailsService来为用户加载信息,因此您需要在配置中使用一个。正如in the manual所解释的那样,如果只有一个属性,则不需要使用user-service-ref属性,因此只需添加ldap-user-service声明即可。