AlreadyBuiltException运行具有Spring安全性的Spring Boot应用程序

时间:2016-04-15 19:41:54

标签: java spring-security spring-boot

我正在按照确切的tutorial构建安全的Spring Boot应用程序。

这是我的安全配置类:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Configuration
    protected static class AuthenticationConfiguration extends
            GlobalAuthenticationConfigurerAdapter {

        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            auth
                .ldapAuthentication()
                    .userDnPatterns("uid={0},ou=people")
                    .groupSearchBase("ou=groups")
                    .contextSource().ldif("classpath:test-server.ldif");            
        }
    }   

}

但是,当我在Spring Tool Suite中运行应用程序时,出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain'
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]
Caused by: org.springframework.security.config.annotation.AlreadyBuiltException: This object has already been built

如果我在@Configuration类def之前注释掉AuthenticationConfiguration,则此错误将消失。但是,如果删除了@Configuration注释,我认为没有配置ldap auth。

为什么说对象已经构建好了?它指的是什么“对象”?下面是我的pom.xml中的依赖项

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-ldap</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.directory.server</groupId>
        <artifactId>apacheds-all</artifactId>
        <version>2.0.0-M20</version>
    </dependency>
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc7</artifactId>
      <version>12.1.0.1</version>
    </dependency>
  </dependencies>

1 个答案:

答案 0 :(得分:1)

您正在尝试使用ApacheDS 2.0,但由于2.0和更新的1.5.x版本中的API更改中断,Spring Security仅支持1.5.5。更新你的pom以使用1.5.5应该可以解决问题。