2

這是我唯一的配置類:LDAP:錯誤創建springSecurityFilterChain:AlreadyBuiltException:該對象已建成

@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled=true, jsr250Enabled=true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .ldapAuthentication() 
       .userSearchBase("cn=users,cn=accounts") 
       .userSearchFilter("uid={0}") 
       .contextSource() 
        .url("ldap://1.2.3.4/dc=dev"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // TODO: fix frontend 
     http.csrf().disable(); 
    } 
} 

我沒有其他的配置類或註釋。我的application.properties只是設置了server.port和一些日誌級別。如果我註釋掉ldap代碼並使用,例如,inMemoryAuthentication,那麼一切正常。

我已經嘗試了使用.ldapAuthentication()在線的每個示例,我可以在其中找到包括這裏的一些相關問題,它們都會導致此錯誤。哪裏不對?


我相信所有的相關依賴來自這些行:

compile 'org.springframework.boot:spring-boot-starter-security' 
compile 'org.springframework.boot:spring-boot-starter-web' 

堆棧跟蹤的一小部分:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.security.config.annotation.AlreadyBuiltException: This object has already been built 
    ... 
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.security.config.annotation.AlreadyBuiltException: This object has already been built 
    ... 
Caused by: org.springframework.security.config.annotation.AlreadyBuiltException: This object has already been built 
    at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:44) 
    ... 
+0

您是否添加了'compile「org.springframework.security:spring-security-ldap」'依賴項? –

+1

不,那是問題所在。謝謝!我最終發現它構建了兩次,第一次因爲NoClassDefFound org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry $ RequestMatchers異常而失敗。 – Philip

回答

0

添加compile org.springframework.security:spring-security-ldap會解決它通過阿里的建議。