繁体   English   中英

在Spring Boot中使用LDAP进行身份验证时出错,原因:[LDAP:错误代码50-访问权限不足]

[英]Error authenticating with LDAP in Spring Boot Reason: [LDAP: error code 50 - Insufficient Access Rights]

我正在尝试使用Spring Boot创建一个验证页面,以验证LDAP服务器中存在的用户。 我的代码是这样的:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.encoding.LdapShaPasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;

import java.util.Arrays;

@Configuration
@EnableGlobalMethodSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .ldapAuthentication()
        .userSearchBase("ou=people")
        .userSearchFilter("(cn={0})")
        .contextSource(contextSource())
        .passwordCompare()
        .passwordAttribute("userPassword");
}

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

}

问题是我收到以下错误,而且我似乎不明白它的来源。

原因:[LDAP:错误代码50-访问权限不足]; 嵌套的异常是javax.naming.NoPermissionException:[LDAP:错误代码50-访问权限不足]; 剩余名称'cn = nameEntered,ou = people'

任何帮助都会有所帮助。

当您尝试将userPassword作为LDAP搜索查询的一部分时,很可能出现此问题。 大多数服务器如RFC中所述保护此属性。

因此,针对此问题可能有两种不同的解决方案:

  • 与您的LDAP服务器管理员联系,并创建一个能够读取该属性的进程帐户( 此处为OpenLDAP的手册 )。 假设您的userPassword属性是明文密码,否则请使用此处所述的密码编码器。

  • 切换到绑定身份验证。 无需使用特权帐户从用户那里检索密码,而是将它绑定到LDAP,而您要进行身份验证的用户不需要任何特殊权限。 但是,它仅使您可以访问用户有权访问的属性。 在这里描述

在这两种情况下,您都应该切换到ldaps://而不是ldap://,因为密码(或哈希)将通过网络传输。

我对Spring API不熟悉。

但是我阅读了API调用,因为它可以完成两件事:

  1. 发送搜索操作以获取用户LDAP条目的专有名称(DN)。
  2. 使用断言类型userPassord和输入密码作为断言值发送对此DN的比较操作。

此操作之一失败。 可以在异常回溯中看到哪一个。

无论如何,使用针对OpenLDAP的比较操作来验证密码是没有意义的,因为它仅在存储明文密码时才起作用。 这是熟练的LDAP管理员可能不会做的事情。

因此,请尝试查找用于使用绑定操作来检查所定位用户DN的密码的API方法。 请注意,这可能需要在客户端应用程序中使用系统帐户身份验证才能获取搜索结果。

在某些情况下,您可以直接从用户名获得用户DN。 在这种情况下,您可以发送绑定操作而无需事先进行系统绑定。 但这需要固定的目录信息树(DIT)布局。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM