有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring LDAP身份验证Salted SHA类型中的错误凭据密码

我有一个使用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服务器进行身份验证


共 (3) 个答案

  1. # 1 楼答案

    坚持使用初始代码,但这次尝试将.userSearchFilter("uid={0}")变成.userSearchFilter("uid={0},ou=people").userDnPatterns("uid={0},ou=people")

  2. # 2 楼答案

    试试这个方法

    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);
            }
        };
    }
    

    我也有同样的问题,这对我很有效

    希望它能帮助你

  3. # 3 楼答案

    我在使用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));
            }
        };
      }