有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

LDAP处理过程中发生java未分类异常;嵌套的异常是javax。命名。NamingException

我正在尝试使用oauth2中的LDAP和spring引导安全性进行身份验证。我的配置如下所示

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
@EnableWebSecurity
public class LdapConfiguration extends WebSecurityConfigurerAdapter {

    private static String url ="ldap://myldapdomain.com:389/OU=Users,OU=Accounts,DC=myldapdomain,DC=com";

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf()
        .disable()
        .authorizeRequests()
        .anyRequest()
        .authenticated()
        .and()
        .httpBasic();

    }

    @Configuration
    protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            auth
            .ldapAuthentication()
            .userSearchFilter("(uid={0})")
            .contextSource().url(url);
        }
    }
}

当我尝试使用所需的LDAP用户ID和密码登录到http://localhost:9000/api/oauth/token时,我遇到以下异常

{
    "timestamp": 1508848799342,
    "status": 401,
    "error": "Unauthorized",
    "message": "Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C090749, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v2580\u0000]; remaining name '/'",
    "path": "/api/oauth/token"
}

谁能帮我一下吗

更新1

我使用下面的代码authenticateUser函数为LDAP身份验证创建了一个java独立应用程序。在那里我可以成功登录

private String ldapURL = "ldap://myldapdomain:389";

private String ldapDomain = "myldapdomain.com";


public void authenticateUser(String username, String password) throws NamingException {
        Hashtable<String, String> env = new Hashtable<>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.PROVIDER_URL, ldapURL);
        env.put(Context.SECURITY_PRINCIPAL, username + "@" + ldapDomain);
        env.put(Context.SECURITY_CREDENTIALS, password);

        DirContext context = null;
        try {
            context = new InitialDirContext(env);
        } catch (Exception e) {
            if (context != null) {
                context.close();
            }
            System.out.println("LDAP auth Failed:::"+ e.getMessage());
            //throw new LoginFailedException("Invalid User Id orPassword");
        }
}

共 (1) 个答案

  1. # 1 楼答案

    您必须定义一个managerDn,用于绑定到LDAP

    例如ldapAuthentication().contextSource() .url(securityConfigProperties.getUrl()) .port(securityConfigProperties.getPort()) .managerDn(securityConfigProperties.getManagerDn()).managerPassword(securityConfigProperties.getManagerPassword())