4

我有一个使用 REST 服务进行 LDAP 身份验证的项目。我的 LDAP 配置具有 Salted SHA (SSHA) 密码哈希方法。在 Spring 的 LDAP 身份验证最佳实践指南中,当我使用支持 SHA 方法时,我得到了错误的凭据,而凭据还可以。

我的配置类参考:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
                .userSearchFilter("uid={0}")
                .contextSource(contextSource())
                .passwordCompare()
                    .passwordEncoder(new LdapShaPasswordEncoder())
                    .passwordAttribute("userPassword");
    }

    @Bean
    public DefaultSpringSecurityContextSource contextSource() {
        return new DefaultSpringSecurityContextSource(Arrays.asList("ldap://localhost:8389/"), "dc=springframework,dc=org");
    }
}

我的ldif配置;

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: {SSHA}pcFdFhO/NS98EhTRup60PMkHMWFRDkJ3jUu1Zg==

我原来的密码是Test1234。我的pom.xml档案;

   <dependency>
       <groupId>org.springframework.ldap</groupId>
       <artifactId>spring-ldap-core</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.security</groupId>
       <artifactId>spring-security-ldap</artifactId>
   </dependency>
   <dependency>
       <groupId>com.unboundid</groupId>
       <artifactId>unboundid-ldapsdk</artifactId>
   </dependency>

如何使用我的用户名/密码通过 SSHA 密码加密向 ldap 服务器进行身份验证?

4

3 回答 3

3

试试这个方法

auth
        .ldapAuthentication()
            .userSearchFilter("uid={0}")
            .contextSource(contextSource())
            .passwordCompare().passwordAttribute("userPassword")
            .and()
            .passwordEncoder(passwordEncoder());

并创建自定义密码编码器

private PasswordEncoder passwordEncoder() {
    final LdapShaPasswordEncoder sha = new LdapShaPasswordEncoder();
    return new PasswordEncoder() {
        @Override
        public String encode(CharSequence rawPassword) {
            return sha.encodePassword(rawPassword.toString(), null);
        }
        @Override
        public boolean matches(CharSequence rawPassword, String encodedPassword) {
            return sha.isPasswordValid(encodedPassword, rawPassword.toString(), null);
        }
    };
}

我有同样的问题,它对我有用。

希望对你有帮助!

于 2017-11-14T17:05:07.647 回答
1

我在使用由 apache 目录支持的 bcrypt 时遇到了类似的问题。感谢@jrdalpra 建议的解决方案,我解决了这个问题。这是我对处于相同情况的任何人的解决方案:

    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));
        }
    };
  }
于 2019-02-18T23:54:20.650 回答
0

坚持使用您的初始代码,但这次尝试 make .userSearchFilter("uid={0}")into .userSearchFilter("uid={0},ou=people")or .userDnPatterns("uid={0},ou=people")

于 2018-03-27T14:17:09.137 回答