在 users.ldif 文件的开头添加以下内容(这是包含 sAMAccountName 的 Microsoft 架构的最小片段):
dn: cn=microsoft, ou=schema
objectclass: metaSchema
objectclass: top
cn: microsoft
dn: ou=attributetypes, cn=microsoft, ou=schema
objectclass: organizationalUnit
objectclass: top
ou: attributetypes
dn: m-oid=1.2.840.113556.1.4.221, ou=attributetypes, cn=microsoft, ou=schema
objectclass: metaAttributeType
objectclass: metaTop
objectclass: top
m-oid: 1.2.840.113556.1.4.221
m-name: sAMAccountName
m-equality: caseIgnoreMatch
m-syntax: 1.3.6.1.4.1.1466.115.121.1.15
m-singleValue: TRUE
dn: ou=objectclasses, cn=microsoft, ou=schema
objectclass: organizationalUnit
objectclass: top
ou: objectClasses
dn: m-oid=1.2.840.113556.1.5.6, ou=objectclasses, cn=microsoft, ou=schema
objectclass: metaObjectClass
objectclass: metaTop
objectclass: top
m-oid: 1.2.840.113556.1.5.6
m-name: securityPrincipal
m-supObjectClass: top
m-typeObjectClass: AUXILIARY
m-must: sAMAccountName
[rest of users.ldif]
现在将新的 objectClass 添加到人员条目:
[...]
dn: cn=rod,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
objectclass: securityPrincipal <--- new objectClass
cn: Rod Johnson
sn: Johnson
sAMAccountName: rod
userPassword: koala
[...]
有新条目是不够的。Spring Security 中的 ApacheDS 配置已禁用模式拦截器,因此默认情况下不会创建新的模式条目。我们可以通过创建修复此问题的 BeanPostProcessor 来打开它:
package com.example.test.spring;
import java.util.List;
import org.apache.directory.server.core.interceptor.Interceptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.security.ldap.server.ApacheDSContainer;
import static org.springframework.util.CollectionUtils.isEmpty;
public class ApacheDSContainerConfigurer implements BeanPostProcessor {
private List<Interceptor> interceptors;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof ApacheDSContainer){
ApacheDSContainer dsContainer = ((ApacheDSContainer) bean);
setInterceptorsIfPresent(dsContainer);
}
return bean;
}
private void setInterceptorsIfPresent(ApacheDSContainer container) {
if (!isEmpty(interceptors)) {
container.getService().setInterceptors(interceptors);
}
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
public void setInterceptors(List<Interceptor> interceptors) {
this.interceptors = interceptors;
}
}
我们必须在应用程序上下文中注册和配置 bean:
<bean class="com.example.test.spring.ApacheDSContainerConfigurer">
<property name="interceptors">
<list>
<bean class="org.apache.directory.server.core.normalization.NormalizationInterceptor"/>
<bean class="org.apache.directory.server.core.authn.AuthenticationInterceptor"/>
<bean class="org.apache.directory.server.core.referral.ReferralInterceptor"/>
<!--<bean class="org.apache.directory.server.core.authz.AciAuthorizationInterceptor"/>-->
<!--<bean class="org.apache.directory.server.core.authz.DefaultAuthorizationInterceptor"/>-->
<bean class="org.apache.directory.server.core.exception.ExceptionInterceptor"/>
<!--<bean class="org.apache.directory.server.core.changelog.ChangeLogInterceptor"/>-->
<bean class="org.apache.directory.server.core.operational.OperationalAttributeInterceptor"/>
<bean class="org.apache.directory.server.core.schema.SchemaInterceptor"/>
<bean class="org.apache.directory.server.core.subtree.SubentryInterceptor"/>
<!--<bean class="org.apache.directory.server.core.collective.CollectiveAttributeInterceptor"/>-->
<!--<bean class="org.apache.directory.server.core.event.EventInterceptor"/>-->
<!--<bean class="org.apache.directory.server.core.trigger.TriggerInterceptor"/>-->
<!--<bean class="org.apache.directory.server.core.journal.JournalInterceptor"/>-->
</list>
</property>
</bean>
它现在应该可以工作了。