2017-06-01 74 views
0

我試圖使用春季安全與LDAP連接,但它總是顯示錯誤的憑據問題。
我想,也許有什麼錯我的代碼:春季安全LDAP登錄錯誤憑證

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.debug(true); 
    } 
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 

     auth.ldapAuthentication().userDnPatterns("sAMAccountName={0},OU=SupportUsers,OU=Users,OU=company,DC=ad,DC=company,DC=com,DC=pl") 
      .contextSource(contextSource()).passwordCompare().passwordAttribute("userPassword"); 
    } 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .anyRequest().fullyAuthenticated() 
       .and() 
      .formLogin(); 
     http.csrf().disable(); //Vaadin already have built in csrf 
    } 
    @Bean 
    public LdapContextSource contextSource() { 
     LdapContextSource contextSource= new LdapContextSource(); 
     contextSource.setUrl("ldap://192.168.2.2:389"); 
     contextSource.setBase("dc=ad,dc=company,dc=com,dc=pl"); 
     contextSource.setUserDn("CN=lister,OU=SupportUsers,OU=Users,OU=company,DC=ad,DC=company,DC=com,DC=pl"); 
     contextSource.setPassword("examplePassword"); 
     contextSource.setAnonymousReadOnly(false); 
     contextSource.setPooled(true); 
     contextSource.afterPropertiesSet(); 
     return contextSource; 
    } 
} 

我不能在代碼中發現錯誤,也許我在做錯誤的方式的東西。這是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>pl.com.company</groupId> 
    <artifactId>LDAPSpringInitializr</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>LDAPSpringInitializr</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.3.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
     <vaadin.version>8.0.5</vaadin.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-data-ldap</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.vaadin</groupId> 
      <artifactId>vaadin-spring-boot-starter</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-ldap</artifactId> 
     </dependency> 
    </dependencies> 

    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>com.vaadin</groupId> 
       <artifactId>vaadin-bom</artifactId> 
       <version>${vaadin.version}</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

我做錯了什麼?也許有密碼編碼的問題?問候,Rafał

回答

0
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 

    auth.ldapAuthentication().userSearchFilter("(sAMAccountName={0})") 
    .contextSource(contextSource()); 
} 

它的工作原理,我剛剛改變了這種方法。