我正在尝试使用 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");
}
}
问题是我收到以下错误,但我似乎不明白它来自哪里。
Reason: [LDAP: error code 50 - Insufficient Access Rights]; nested exception is javax.naming.NoPermissionException: [LDAP: error code 50 - Insufficient Access Rights]; remaining name 'cn=nameEntered,ou=people'
任何帮助都会有所帮助。
最佳答案
问题很可能是由于您尝试将 userPassword 作为 LDAP 搜索查询的一部分而导致的。大多数服务器都会保护此属性,如 this RFC 中所述。 .
因此这个问题可能有两种不同的解决方案:
与您的 LDAP 服务器管理员联系并创建一个能够读取该属性的进程帐户 ( manual for OpenLDAP here )。这假设您的 userPassword 属性是明文密码,如果不是,您将需要使用密码编码器 described here .
切换到绑定(bind)身份验证。它不是使用特权帐户从用户处检索密码,而是将您尝试验证的用户绑定(bind)到 LDAP,不需要任何特殊权限。然而,它只会让您访问用户有权访问的属性。这是described here 。您可以这样配置:
<ldap-authentication-provider user-dn-pattern="uid={0},ou=people"/>
无论哪种情况,您都应该切换到 ldaps://而不是 ldap://,因为密码(或哈希值)将以明文形式通过网络传输。
关于java - 在 Spring Boot 中使用 LDAP 进行身份验证时出错原因 : [LDAP: error code 50 - Insufficient Access Rights],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51558193/