Spring3 LDAP
这里介绍使用LDAP来校验用户
1.准备阶段
下载相关的jar
spring-ldap-1.3.0.RELEASE-all.jar
2.配置相关
<bean id="contextSource"
class="org.springframework.ldap.test.TestContextSourceFactoryBean">
<property name="defaultPartitionSuffix" value="dc=jayway,dc=se" />
<property name="defaultPartitionName" value="jayway" />
<property name="principal" value="${userDn}" />
<property name="password" value="${password}" />
<property name="ldifFile" value="classpath:/setup_data.ldif" />
<property name="port" value="3900" />
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
3.访问LDAP服务器
import javax.inject.Inject;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.stereotype.Service;
@Service
public class SpringLdapDaoImpl implements SpringLdapDao {
@Inject
LdapTemplate ldapTemplate;
@Override
public boolean authUser(String userId, String password) {
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("CN", userId));
return ldapTemplate.authenticate("OU=Org" , filter.toString(), password);
}
}
然后在server端调用即可验证用户。