Spring security springldap-bind用于成功连接
我正在尝试使用SpringLDAP和SpringSecurity进行身份验证,然后查询我们的公司LDAP。我设法使身份验证工作,但当我试图运行搜索,我总是得到以下异常 要执行此操作,必须在连接上成功完成绑定 经过大量研究,我有了一个理论,即在我进行身份验证之后,在我可以查询之前,我需要绑定到连接。我只是不知道该怎么做 我可以使用JXplorer成功地浏览和搜索我们的LDAP,因此我的参数是正确的 下面是我的securityContext.xml的一部分Spring security springldap-bind用于成功连接,spring-security,ldap,bind,spring-ldap,Spring Security,Ldap,Bind,Spring Ldap,我正在尝试使用SpringLDAP和SpringSecurity进行身份验证,然后查询我们的公司LDAP。我设法使身份验证工作,但当我试图运行搜索,我总是得到以下异常 要执行此操作,必须在连接上成功完成绑定 经过大量研究,我有了一个理论,即在我进行身份验证之后,在我可以查询之前,我需要绑定到连接。我只是不知道该怎么做 我可以使用JXplorer成功地浏览和搜索我们的LDAP,因此我的参数是正确的 下面是我的securityContext.xml的一部分 <security:http aut
<security:http auto-config='true'>
<security:intercept-url pattern="/reports/goodbye.html"
access="ROLE_LOGOUT" />
<security:intercept-url pattern="/reports/**" access="ROLE_USER" />
<security:http-basic />
<security:logout logout-url="/reports/logout"
logout-success-url="/reports/goodbye.html" />
</security:http>
<security:ldap-server url="ldap://s140.foo.com:1389/dc=td,dc=foo,dc=com" />
<security:authentication-manager>
<security:authentication-provider ref="ldapAuthProvider">
</security:authentication-provider>
</security:authentication-manager>
<!-- Security beans -->
<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldap://s140.foo.com:1389/dc=td,dc=foo,dc=com" />
</bean>
<bean id="ldapAuthProvider"
class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
<constructor-arg>
<bean class="foo.bar.reporting.server.security.ldap.LdapAuthenticatorImpl">
<property name="contextFactory" ref="contextSource" />
<property name="principalPrefix" value="TD\" />
<property name="employee" ref="employee"></property>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="foo.bar.reporting.server.security.ldap.LdapAuthoritiesPopulator" />
</constructor-arg>
</bean>
<!-- DAOs -->
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
下面是另一段来自EmployeeDao
的代码片段,我徒劳地试图查询:
public List<Employee> queryEmployeesByName(String query)
throws BARServerException {
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person"));
filter.and(new WhitespaceWildcardsFilter("cn", query));
try {
// the following line throws bind exception
List result = ldapTemplate.search(BASE, filter.encode(),
new AttributesMapper() {
@Override
public Employee mapFromAttributes(Attributes attrs)
throws NamingException {
Employee emp = new Employee((String) attrs.get("cn").get(),
(String) attrs.get("cn").get(),
(String) attrs.get("cn").get());
return emp;
}
});
return result;
} catch (Exception e) {
throw new BarServerException("Failed to query LDAP", e);
}
}
看起来您的LDAP配置为不允许未绑定到它的搜索(无匿名绑定)。此外,您还将
PasswordComparisonAuthenticator
而不是bindeauthenticator
实现到LDAP
您可以尝试修改您的
queryEmployeesByName()
方法来绑定然后搜索,查看中的一些示例。我将接受@Raghuram答案,主要是因为它让我朝着正确的方向思考
为什么我的代码失败了?结果是——按照我的接线方式,我试图执行系统禁止的匿名搜索——因此出现了错误
如何将上述示例重新布线以使其正常工作?首先,你需要提供用户的用户名和密码,这些用户名和密码将用于访问系统。即使您登录并通过身份验证,即使您使用的是BindAuthenticator
系统也不会尝试重用您的凭据,这是非常违反直觉的。真倒霉因此,您需要将两个参数粘贴到contextSource
定义中,如下所示:
<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldap://foo.com:389/dc=td,dc=foo,dc=com" />
<!-- TODO - need to hide this or encrypt a password -->
<property name="userDn" value="CN=admin,OU=Application,DC=TD,DC=FOO,DC=COM" />
<property name="password" value="blah" />
</bean>
这样做使我可以用generic
BindAuthenticator
替换authenticator的自定义实现,然后我的Java搜索开始工作我遇到了同样的错误,找不到解决方案。
最后,我将应用程序池标识改为网络服务,一切都很顺利。
(我在我的网站上启用了windows身份验证和匿名)这看起来像是一条评论,你有什么建议?我知道这是可能的,因为我可以使用JXplorer(java应用程序)来连接和搜索LDAP,我只是不知道如何绑定。我想我以前试过BindAuthenticator,结果也是一样exception@Droidln.net. 用明确的建议编辑了答案。这是我用来编码身份验证的非常好的文档。当他们谈论绑定/取消绑定时,他们的意思是插入/删除特定示例中的记录。我知道这很旧,但@Bostone您能帮我解决这个问题吗。我得到了完全相同的异常,但是我在用户第一次输入凭据的登录页面上得到了这个错误。当输入正确的用户名和密码时,ldap将成功返回,但出现以下错误:[ldap:错误代码1-00000000:LdapErr:DSID-0C090627,注释:为了执行此操作,必须在连接上完成成功绑定,数据0,vece];还押姓名“”@user1647708请参阅下面我的答案。它适用于meDid您是否将
userDn
和password
的值设置为真实值?我的意思是,如果我这样做,每个人都会看到它的纯文本。例如,我如何从登录表单中读取它?
org.springframework.ldap.UncategorizedLdapException:
Uncategorized exception occured during LDAP processing; nested exception is
javax.naming.NamingException: [LDAP: error code 1 - 00000000: LdapErr:
DSID-0C090627, comment: In order to perform this operation a successful bind
must be completed on the connection., data 0, vece]; remaining name
'DC=TD,DC=FOO,DC=COM'
<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldap://foo.com:389/dc=td,dc=foo,dc=com" />
<!-- TODO - need to hide this or encrypt a password -->
<property name="userDn" value="CN=admin,OU=Application,DC=TD,DC=FOO,DC=COM" />
<property name="password" value="blah" />
</bean>