【问题标题】:spring.io Authenticating with LDAP guide returns Bad Credentialsspring.io Authenticating with LDAP guide 返回 Bad Credentials
【发布时间】:2020-02-17 21:50:09
【问题描述】:

我正在遵循指南 (https://spring.io/guides/gs/authenticating-ldap/) 并有以下课程:

网页配置类:

@Configuration
public class WebSecurityConfig {

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

public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .ldapAuthentication()
            .userDnPatterns("uid={0},ou=people")
            .groupSearchBase("ou=groups")
            .contextSource()
                .url("ldap://localhost:8389/dc=springframework,dc=org")
                .and()
            .passwordCompare()
                .passwordEncoder(new LdapShaPasswordEncoder())
                .passwordAttribute("userPassword");
    }
}

控制器类

@RestController
public class SpringLdapController {

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

LDAP 配置文件 (.ldif)

dn: dc=springframework,dc=org
objectclass: top
objectclass: domain
objectclass: extensibleObject
dc: springframework

dn: ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: groups

dn: ou=subgroups,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: subgroups

dn: ou=people,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: people

dn: ou=space cadets,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: space cadets

dn: ou=\"quoted people\",dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: "quoted people"

dn: ou=otherpeople,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: otherpeople

dn: uid=ben,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Ben Alex
sn: Alex
uid: ben
userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=

dn: uid=bob,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Bob Hamilton
sn: Hamilton
uid: bob
userPassword: bobspassword

dn: uid=joe,ou=otherpeople,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Joe Smeth
sn: Smeth
uid: joe
userPassword: joespassword

dn: cn=mouse\, jerry,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Mouse, Jerry
sn: Mouse
uid: jerry
userPassword: jerryspassword

dn: cn=slash/guy,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: slash/guy
sn: Slash
uid: slashguy
userPassword: slashguyspassword

dn: cn=quote\"guy,ou=\"quoted people\",dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: quote\"guy
sn: Quote
uid: quoteguy
userPassword: quoteguyspassword

dn: uid=space cadet,ou=space cadets,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Space Cadet
sn: Cadet
uid: space cadet
userPassword: spacecadetspassword



dn: cn=developers,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: groupOfUniqueNames
cn: developers
ou: developer
uniqueMember: uid=ben,ou=people,dc=springframework,dc=org
uniqueMember: uid=bob,ou=people,dc=springframework,dc=org

dn: cn=managers,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: groupOfUniqueNames
cn: managers
ou: manager
uniqueMember: uid=ben,ou=people,dc=springframework,dc=org
uniqueMember: cn=mouse\, jerry,ou=people,dc=springframework,dc=org

dn: cn=submanagers,ou=subgroups,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: groupOfUniqueNames
cn: submanagers
ou: submanager
uniqueMember: uid=ben,ou=people,dc=springframework,dc=org

我尝试了我认为正确的登录组合,例如:

"ben", "benspassword"|| "太空学员","太空学员密码"|| “杰瑞”,“杰瑞密码”

但我的屏幕上不断显示“错误凭据”,拒绝我的登录。有什么我遗漏的吗?

【问题讨论】:

标签: java spring rest spring-ldap spring-security-ldap


【解决方案1】:

您应该错过了应用程序密钥 LDAP 属性配置,这可能在上述教程中是看不到的。

src/main/resources/application.properties 文件应该包含嵌入式 Spring LDAP 服务器相关配置:

spring.ldap.embedded.ldif=classpath:<YOUR_LDIF_FILE_NAME>
spring.ldap.embedded.base-dn=dc=springframework,dc=org
spring.ldap.embedded.port=8389

请注意,按照此示例配置,YOUR_LDIF_FILE_NAME 应放在 src/main/resources 下。

【讨论】:

  • 抱歉重播晚了。我已将上述配置添加到 application.properties 并且 ldif 在 src/main/resources 中,但我仍然收到错误消息。 ://
  • 能否将错误堆栈跟踪添加到主帖?
【解决方案2】:

你的 ldif 文件中的密码是明文的,同时你使用 passwordEncoder 进行密码比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-30
    • 2012-04-25
    • 2018-09-10
    • 2021-12-03
    • 2020-03-24
    • 1970-01-01
    • 2015-06-05
    • 2019-07-18
    相关资源
    最近更新 更多