java – SpringBoot,如何在不使用ldif的情况下使用LDAP进行身份验证?

前端之家收集整理的这篇文章主要介绍了java – SpringBoot,如何在不使用ldif的情况下使用LDAP进行身份验证?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试 SpringBoot here中的LDAP身份验证示例

它使用的是我认为不适用于我的要求的ldif方法,因为我们的ldap管理员不会告诉我在哪里可以找到我需要的ldif.
在springboot之前,我曾经使用过自己的ldap实现而不是使用ldif.有没有办法验证不使用ldif只是SECURITY_AUTHENTICATION.simple?
下面是我如何在基本Java没有弹簧的情况下执行ldap安全性.如何在不使用ldif基本用户名密码的情况下在春天这样做.

boolean isLdapRegistred(String username,String password) {
    boolean result = false;
    try {

        Hashtable<String,String> env = new Hashtable<String,String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL,"ldap://10.x.x.x:389");           
        env.put(Context.SECURITY_AUTHENTICATION,"simple");         
        env.put(Context.SECURITY_PRINCIPAL,"OUR-DOMAIN\\" + username);
        env.put(Context.SECURITY_CREDENTIALS,password);

        // Create the initial context
        DirContext ctx = new InitialDirContext(env);
        result = ctx != null;
        if (ctx != null)
        ctx.close();
        System.out.println(result);
        return result;
    } catch (Exception e) {
        System.out.println("oops");
        return result;
    }

}

下面是SpringBoots示例需要使用我的凭据而不是ldif.

@Configuration
protected static class AuthenticationConfiguration extends
        GlobalAuthenticationConfigurerAdapter {

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
                .userDnPatterns("uid={0},ou=people")
                .groupSearchBase("ou=groups")
                .contextSource().ldif("classpath:test-server.ldif");
    }
}

解决方法

没有LDIF,使用Spring,你可以做类似的事情:

@Configuration
@EnableWebSecurity
public class HttpSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(ldapAuthenticationProvider());
    }

    @Bean
    public AuthenticationProvider ldapAuthenticationProvider() throws Exception {
        DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(ldapServerUrl);
        contextSource.setUserDn(ldapManagerDn);
        contextSource.setPassword(ldapManagerPassword);
        contextSource.afterPropertiesSet();
        LdapUserSearch ldapUserSearch = new FilterBasedLdapUserSearch(ldapUserSearchBase,ldapUserSearchFilter,contextSource);
        BindAuthenticator bindAuthenticator = new BindAuthenticator(contextSource);
        bindAuthenticator.setUserSearch(ldapUserSearch);
        LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(bindAuthenticator,new DefaultLdapAuthoritiesPopulator(contextSource,ldapGroupSearchBase));
        return ldapAuthenticationProvider;
    }
}

猜你在找的Springboot相关文章