加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 04:33:52 所属栏目:Java 来源:网络整理
导读:我正在尝试 SpringBoot here中的LDAP身份验证示例 它使用的是我认为不适用于我的要求的ldif方法,因为我们的ldap管理员不会告诉我在哪里可以找到我需要的ldif. 在springboot之前,我曾经使用过自己的ldap实现而不是使用ldif.有没有办法验证不使用ldif只是SECUR
我正在尝试 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;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读