【问题标题】:LDAP Authentication - Spring Security - LdapAuthenticationProviderLDAP 身份验证 - Spring Security - LdapAuthenticationProvider
【发布时间】:2016-09-01 17:10:51
【问题描述】:

我正在努力让 LDAP 安全配置与 xml 配置一起使用。

我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean               with name 'securityConfig': Injection of autowired dependencies failed; nested     exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.ldap.authentication.LdapAuthenticationProvider sgcbmw.security.SecurityConfig.ldapAuthenticationProvider; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.ldap.authentication.LdapAuthenticationProvider] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)

我的安全配置:

<bean id="contextSource"class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
        <constructor-arg value="ldap:/ldapserver"/>
        <property name="userDn" value="user"/>
        <property name="password" value="pass"/>
    </bean>

    <bean id="ldapAuthProvider"
          class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
        <constructor-arg>
            <bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
                <constructor-arg ref="contextSource"/>
                <property name="userSearch">
                <bean id="userSearch"
                      class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
                    <constructor-arg index="0" value=""/>
                    <constructor-arg index="1" value="(&amp;(objectClass=user)(sAMAccountName={0}))"/>
                    <constructor-arg index="2" ref="contextSource" />
                </bean>
                </property>
                <property name="userDnPatterns">
                    <list><value>uid={0},ou=people</value></list>
                </property>
            </bean>
        </constructor-arg>
        <constructor-arg>
            <bean  class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
                <constructor-arg ref="contextSource"/>
                <constructor-arg value="ou=groups"/>
                <property name="groupRoleAttribute" value="memberOf"/>
            </bean>
        </constructor-arg>
    </bean>
    <security:authentication-manager>
        <security:authentication-provider ref="ldapAuthProvider" />
    </security:authentication-manager>

配置适配器:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private LdapAuthenticationProvider ldapAuthenticationProvider;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(ldapAuthenticationProvider);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .csrf().disable()
            .authorizeRequests()
            .anyRequest().fullyAuthenticated()
            .and()
            .formLogin();
}
}

不应该以这种方式注入 LdapAuthenticationProvider 吗?

【问题讨论】:

  • 你的上下文是否被解析了?
  • 你说的解析是什么意思?在 web,xml 我有以下代码: contextConfigLocationclasspath:spring/business-config.xml-param>
  • 试试这个,可能会有所帮助:使用 ldapAuthenticationProvider 的 setter 注入,在 SecurityConfig 类中创建 ldapAuthenticationProvider 的 setter,删除 @Autowired 注释。
  • Ricardo,您确定您的安全 xml 配置已被解析并且 LdapAuthenticationProvider 已由 Spring 实例化吗?为确保,在 LdapAuthenticationProvider 类中为所有构造函数设置断点并在调试模式下运行项目。是否达到任何断点?

标签: java spring spring-security spring-security-ldap


【解决方案1】:

什么解决了我的问题:

我为安全创建了一个上下文 xml 配置,并将以下内容添加到 web.xml:

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/business-config.xml classpath:spring/spring-security-config.xml</param-value>
</context-param>

最后,我在 spring-security-config.xml 上配置了 beans,并在我的类 WebSecurityConfigurerAdapter 上自动装配它们。从 xml 中删除:

<security:authentication-manager>
    <security:authentication-provider ref="ldapAuthProvider" />
</security:authentication-manager>

感谢您的帮助。

【讨论】:

  • 如何在没有 config.xml 而不是 java 代码的情况下实例化 LdapAuthenticationProvider 的对象?
猜你喜欢
  • 2012-02-18
  • 2015-10-20
  • 2016-09-09
  • 2013-01-11
  • 2016-08-05
  • 2015-07-22
  • 2012-10-10
  • 2020-04-08
  • 2011-02-01
相关资源
最近更新 更多