Spring Security Ldap验证userDn和登录表单中的密码

问题描述 投票:4回答:1

我正在尝试使用WebSecurityConfigurerAdapter实现Spring Security LDAP身份验证。

到目前为止它工作正常,但我的问题是我不希望上下文的用户名和密码被硬编码。它必须是用户的登录名和密码,所以我的问题是如何从登录表单构建上下文和设置用户名和密码?

这是我正在使用的代码:

@Configuration
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()
                .userSearchFilter("(sAMAccountName={0})")
                .contextSource(contextSource());
    }

    @Bean
    public BaseLdapPathContextSource contextSource() {
        LdapContextSource bean = new LdapContextSource();
        bean.setUrl("ldap://10.10.10.10:389");
        bean.setBase("DC=myDomaine,DC=com");
        //instead of this i want to put here the username and password provided by the user
        bean.setUserDn("myDomaine\\username");
        bean.setPassword("password");
        bean.setPooled(true);
        bean.setReferral("follow");
        bean.afterPropertiesSet();
        return bean;
    }
}

谢谢!

java spring-boot spring-security active-directory ldap
1个回答
1
投票

你的代码应该完全正常。硬编码的用户名和密码仅用于创建与ldap服务器的绑定。登录表单中提供的用户名和密码仅使用您的代码进行身份验证。

我使用以下代码来执行ldap身份验证。

public void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication().userSearchFilter("sAMAccountName={0}").contextSource().url(this.ldapUrl).managerDn(this.managerDn)
    .managerPassword(this.managerPassword);
}

管理器是用于与服务器创建绑定的ldap帐户。

© www.soinside.com 2019 - 2024. All rights reserved.