spring启动ldap身份验证获取错误凭据错误

a9wyjsp7  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(247)

我正在尝试使用SpringBootLDAP应用程序通过ldap服务器进行身份验证并连接到本地ldap服务器,但仍然收到错误的凭据,下面有一些代码
ldapwebsecurityconfig.java

@EnableWebSecurity
public class LdapWebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.authorizeRequests()
        .anyRequest()
        .authenticated()
        .and()
        .formLogin()
        .loginProcessingUrl("/login")
        .usernameParameter("username")
        .passwordParameter("password")        
        .successHandler( new DemoAuthenticationSuccessHandler() )
        .failureHandler( new DemoAuthenticationFailureHandler() )
        .and()
        .exceptionHandling()
        .authenticationEntryPoint( new DemoAuthenticationEntryPoint() )
        .and()
        .csrf()
        .ignoringAntMatchers("/login");
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception
    {           
        auth.ldapAuthentication()
        .userDnPatterns("cn={0},OU=Users")
//        .userSearchFilter("CN={0}")
        .groupSearchBase("OU=Users")
        .contextSource()       
        .url("ldap://localhost:10389/dc=example,dc=com")
        .managerDn("cn=Adan Abrams,OU=Users,dc=example,dc=com")
        .managerPassword("1qaz2wsx")
        .and()
        .passwordCompare()
        .passwordEncoder(newPasswordEncoder())
        .passwordAttribute("userPassword");
    }

    private PasswordEncoder newPasswordEncoder() {
        final BCryptPasswordEncoder crypt = new BCryptPasswordEncoder();
        return new PasswordEncoder() {
          @Override
          public String encode(CharSequence rawPassword) {
            // Prefix so that apache directory understands that bcrypt has been used.
            // Without this, it assumes SSHA and fails during authentication.
            return "{CRYPT}" + crypt.encode(rawPassword);
          }
          @Override
          public boolean matches(CharSequence rawPassword, String encodedPassword) {
            // remove {CRYPT} prefix
            return crypt.matches(rawPassword, encodedPassword.substring(7));
          }
      };
    }

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId></groupId>
    <artifactId>adAuth</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>adAuth</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>      
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-ldap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
<!--         <dependency> -->
<!--             <groupId>com.unboundid</groupId> -->
<!--             <artifactId>unboundid-ldapsdk</artifactId> -->
<!--         </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>        
    </dependencies>

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

</project>

democontroller.java

@RestController
public class DemoController {
    @GetMapping("/")
    public String index() {
        return "Welcome to the home page!";
    }
}

lcdp服务器结构
测试帐户密码已验证
如果配置缺少什么,任何帮助都会很好。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题