在LDAP处理期间发生了未分类的异常;嵌套异常是javax.naming.NamingException [英] Uncategorized exception occured during LDAP processing; nested exception is javax.naming.NamingException

查看:2413
本文介绍了在LDAP处理期间发生了未分类的异常;嵌套异常是javax.naming.NamingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to authenticate using LDAP in oauth2 with spring boot security. My configuration is as given below

@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);
        }
    }
}

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

When I tried to login to http://localhost:9000/api/oauth/token with the required LDAP userid and password I am getting the following exception

{
    "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"
}

任何人都可以帮助我在此

Can anyone please help me on this

更新1

我为LDAP创建了一个java独立应用程序真实使用以下代码 authenticateUser 函数。在那里我能够成功登录

I have created a java standalone application for LDAP authentication using the below code authenticateUser function. There I am able to login successfully

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");
        }
}


推荐答案

你必须定义 managerDn ,它用于绑定到您的LDAP。

You have to define a managerDn which is used to bind to your LDAP.

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

这篇关于在LDAP处理期间发生了未分类的异常;嵌套异常是javax.naming.NamingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆