Spring安全LDAP-登录问题(ProviderNotFoundException)

Spring安全LDAP-登录问题(ProviderNotFoundException),spring,spring-security,Spring,Spring Security,我对LDAP Spring安全性有问题,我正在尝试针对LDAP服务器进行授权。我有如下spring配置xml文件(security config.xml): <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sec

我对LDAP Spring安全性有问题,我正在尝试针对LDAP服务器进行授权。我有如下spring配置xml文件(security config.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sec="http://www.springframework.org/schema/security"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">

 <bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
  <constructor-arg value="ldap://111.111.111.111"/>
  <property name="userDn" value="cn=auth-user,ou=System,dc=foo,dc=com"/>
  <property name="password" value="fooPwd"/>
 </bean>

 <bean id="ldapAuthProvider" class="org.springframework.security.providers.ldap.LdapAuthenticationProvider">
  <constructor-arg>
   <bean class="org.springframework.security.providers.ldap.authenticator.BindAuthenticator">
    <constructor-arg ref="contextSource"/>
    <property name="userSearch">
     <bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
       <constructor-arg index="0" value="ou=people"/>
       <constructor-arg index="1" value="(uid={0})"/>
       <constructor-arg index="2" ref="contextSource" />
     </bean>
    </property>
   </bean>
  </constructor-arg>
  <constructor-arg>
   <bean class="com.company.name.services.UserAuthoritiesPopulator" />
  </constructor-arg>
 </bean>
</beans>
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String loginPPost(String username, String password, Model model, HttpServletRequest req, HttpServletResponse res) throws SQLException {

 UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
 Authentication authentication = authenticationManager.authenticate(authRequest);
 SecurityContextHolder.getContext().setAuthentication(authentication);
 ...
}
“authenticationManager.authenticate(authRequest)”方法引发此异常:

org.springframework.security.providers.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.providers.UsernamePasswordAuthenticationToken
    at org.springframework.security.providers.ProviderManager.doAuthentication(ProviderManager.java:214)
    at org.springframework.security.AbstractAuthenticationManager.authenticate(AbstractAuthenticationManager.java:46)
有人知道如何解决这个问题吗?我应该使用不同的授权方法吗?还是我的配置不好

谢谢你的帮助


Mateo

您必须在身份验证提供程序bean中添加标记“sec:custom authentication provider”:

<bean id="ldapAuthProvider" class="org.springframework.security.providers.ldap.LdapAuthenticationProvider">
    <sec:custom-authentication-provider/>
    ...
</bean>

...
您可以在我的博客上找到一个使用群组而不是LDAP的示例:

非常感谢!这有帮助。我只是想知道我在春季安全的任何地方都找不到它-/