Spring Security 返回“Bad Credentials”,基于 LDAP 的身份验证无一例外

问题描述 投票:0回答:1

我正在尝试创建基于 Sprint Boot、Spring Security 6、LDAP 服务器(外部而非嵌入式)的身份验证应用程序。当我启动应用程序并在登录表单上提供用户名 (uid) 和密码时,我在 UI 上收到一条“Bad Credentials”消息。应用程序日志中没有报告异常。我不明白是什么导致显示“Bad Credentials”消息。非常感谢任何指针。 这是我的配置文件的样子

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeHttpRequests().anyRequest().fullyAuthenticated()
                .and()
                .formLogin();
        httpSecurity.authenticationProvider(ldapAuthenticationProvider());
        return httpSecurity.build();
    }

    @Bean
    LdapAuthenticationProvider ldapAuthenticationProvider() {
        return new LdapAuthenticationProvider(authenticator());
    }

    @Bean
    BindAuthenticator authenticator() {
        FilterBasedLdapUserSearch search = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", contextSource());
        BindAuthenticator authenticator = new BindAuthenticator(contextSource());
        authenticator.setUserSearch(search);
        return authenticator;
    }

    @Bean
    public DefaultSpringSecurityContextSource contextSource() {
        DefaultSpringSecurityContextSource dsCtx = new DefaultSpringSecurityContextSource("ldap://localhost:389/dc=example,dc=com");
        dsCtx.setUserDn("cn=admin,dc=example,dc=com");
        dsCtx.setPassword("password");

        return dsCtx;
    }
}

当我尝试使用 ldapsearch 命令查找用户时,我确实获得了用户信息

MacBook-Pro:springsecuritywithldapdemo$ ldapsearch -LLL -x -H ldap:// -t -b "dc=example,dc=com" "uid=jsmith1"
dn: uid=jsmith1,ou=people,dc=example,dc=com
objectClass: inetOrgPerson
description: John Smith from Accounting.  John is the project manager of the b
 uilding project, so contact him with any questions.
cn: John Smith
sn: Smith
uid: jsmith1
userPassword:: anNtaXRoMTIz

我浏览了谷歌在不同搜索中返回的许多搜索结果,其中大多数使用了旧版本的 Spring Security 或使用了 Spring Security 6 的 JDBC 身份验证。 我已经参考了 youtube 教程,看看我是否做错了什么但看起来不像。

spring-security openldap spring-security-ldap
1个回答
0
投票

首先我要感谢你,因为基于你的解决方案,我能够解决我应用程序中的相同问题。与您的解决方案相比,我唯一改变的是:

FilterBasedLdapUserSearch search = new FilterBasedLdapUserSearch(
  "cn=users,cn=accounts,dc=example,dc=com",
  "(uid={0})",
  contextSource()
);

所以在你的情况下你应该尝试:

FilterBasedLdapUserSearch search = new FilterBasedLdapUserSearch(
  "ou=people,dc=example,dc=com",
  "(uid={0})", contextSource()
);

然后我还必须进行以下更改:

DefaultSpringSecurityContextSource dsCtx = new DefaultSpringSecurityContextSource("ldap://localhost:389");
dsCtx.setUserDn("uid=admin,cn=users,cn=accounts,dc=example,dc=com");

在你的情况下,我认为这几乎是相同的,因为我认为你也有一个

uid=admin
的用户。我希望它能解决你的问题,就像它对我一样。

© www.soinside.com 2019 - 2024. All rights reserved.