Spring LDAP安全性-BeanCreationException

时间:2019-12-22 21:18:27

标签: spring-boot spring-mvc spring-security-ldap

尊敬的Java专业人员, 我正在尝试使用来自以下链接的说明使用LDAP机制保护Spring-boot MVC应用程序:https://spring.io/guides/gs/authenticating-ldap/

但是,我面临下一个问题:

  

org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / security / config / annotation / web / configuration / WebSecurityConfiguration.class]中创建名称为'springSecurityFilterChain'的bean时出错:通过工厂实例化Bean方法失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[javax.servlet.Filter]:工厂方法'springSecurityFilterChain'抛出了异常;嵌套的异常是java.lang.NoClassDefFoundError:org / springframework / security / ldap / DefaultSpringSecurityContextSource

我不知道为什么会出现此错误。我试图在这个论坛上搜索,以为发现了类似的问题,但是没有成功: https://stackoverflow.com/questions/28616569/error-creating-bean-with-name-springsecurityfilterchain

有人能建议我做错了吗? 预先感谢。

这是WebSecurityConfig类的外观:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

 @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
      //  .antMatchers("/", "/home").permitAll()
        .anyRequest().authenticated()
        .and()
      .formLogin()
        .loginPage("/login")
        .permitAll()
        .and()
      .logout()
        .permitAll();
  }


  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
        .ldapAuthentication()
        .userDnPatterns("uid={0},ou=people")
        .groupSearchBase("ou=groups")
        .contextSource()
                .url("ldap://localhost:8389/dc=springframework,dc=org")
                .and()
                .passwordCompare()
                .passwordEncoder(new LdapShaPasswordEncoder())
                .passwordAttribute("userPassword");
    }
}

这是MvcConfig类:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;    

@Configuration
public class MvcConfig implements WebMvcConfigurer {    
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");
  }
}

pom.xml文件的相关部分:

<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>ba.epbih.zastoji</groupId>
    <artifactId>Zastoji_v1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>MyApp</name>
    <description>lalala</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>com.unboundid</groupId>
            <artifactId>unboundid-ldapsdk</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ldap</groupId>
            <artifactId>spring-ldap-core</artifactId>
        </dependency>

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,并尝试了各种方法来消除错误。我想您也遵循此处给出的示例:

https://spring.io/guides/gs/authenticating-ldap/

由于我使用的spring boot版本比示例新,因此没有直接的匹配。

最终,在尝试添加和删除依赖项之后,我发现缺少的依赖项是(用于gradle):

implementation 'org.springframework.security:spring-security-ldap'

因此在Maven中,它应该类似于:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
</dependency>