当前位置: 首页 > 知识库问答 >
问题:

如何正确使用configurer类实现Spring Security Ldap身份验证?

李谦
2023-03-14

您好,我正在尝试使用WebSecurityConfigureAdapter类实现spring的ldap身份验证。

到目前为止,我可以通过内存中的方法进行身份验证,甚至可以通过我公司的ldap服务器进行身份验证,但是,如果我在创建新上下文时传递了硬编码的userDN和密码,或者我没有创建新上下文,或者我没有输入userDN和密码,jvm就会抛出我:

Caused by: javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C0906E8, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v1db1\u0000]; Remaining name: '/'

我的问题是,如何从登录表单中获取用户密码和userDN,以便将其置于上下文中?如果不可能,如何获取密码和userDn的上下文?

这是我的代码:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

        auth.ldapAuthentication().userSearchFilter("(&(objectClass=user)(sAMAccountName={0}))")
          .groupSearchFilter("(&(memberOf:1.2.840.113556.1.4.1941:=CN=DL - DC859 - MIDDLEWARE,OU=Dyn,OU=Dist,OU=Security Groups,OU=POP,DC=pop,DC=corp,DC=local))")
          .contextSource(getLdapContextSource());
    }

    private LdapContextSource getLdapContextSource() throws Exception {
        LdapContextSource cs = new LdapContextSource();
        cs.setUrl("ldap://tcp-prd.pop.corp.local:389");
        cs.setBase("DC=pop,DC=corp,DC=local");
        cs.setUserDn("t8951435@pop.corp.local");
        cs.setPassword("mypassword");
        cs.afterPropertiesSet();
        return cs;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/resources/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll();     
    }

}

非常感谢。

共有1个答案

澹台庆
2023-03-14

我终于在这篇文章中找到了答案。我仍然不知道如何设置组筛选器,但至少现在我可以绑定到服务器。

 @Bean
 public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
     ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider("pop.corp.local", 
             "ldap://tcp-prd.pop.corp.local:389");
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);
        return provider;
 }

@Bean
public LoggerListener loggerListener() {
    return new LoggerListener();
}


@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
}

    @Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/resources/**").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll();     
}

编辑:我终于找到了如何按组过滤。事实证明,他们在ActiveDirectoryLdapAuthenticationProvider类v3中添加了一个setSearchFilter()方法。2.6。因为我使用的是旧版本,所以我从来不知道这一点。因此,我用该方法复制了该类,并创建了一个buildFilter方法来创建传递给setSearchFilter的过滤器字符串。

 类似资料:
  • JAAS 的配置 Kafka 使用Java验证和授权API(JAAS)来完成 SASL 配置. Kafka brokers 的 JAAS 的配置 KafkaServer 是每一个 KafkaServer/Broker 的 JASS 文件里面的节点名称。 在这个节点中,提供了用于所有 brokers 之间通信的 SASL 客户端连接的 SASL 配置选项。 Client 节点是用于认证 SASL 与

  • 问题内容: 我的项目使用Node.js和Express,但问题在于通用方法。 我们的用户全部来自FB,除FB外,我们没有其他任何身份验证。我们需要将某些操作与特定的FB用户相关联,还需要他们的令牌才能与FB通信。 目前, 我们这样做: 用户来到页面 有一些 看不见的 块:一个带有占位符,用于存储用户的化身和姓名(“已登录”),另一个带有用于触发FB登录的按钮(“已退出”) 使用FB JS SDK,

  • 我正在尝试从基于Spring XML的配置迁移到纯Java配置,我已经配置了所有配置文件,但当我尝试在登录页面中输入用户名和密码时,它会再次将我重定向回登录页面。我相信userServiceImpl方法没有被调用,因为控件没有被调用。在这里,我有一个自动连接的userServiceImpl方法,它实现了Spring Security核心凭据的UserDetailsService。 自定义身份验证成

  • 一般来说,我对微服务和api网关的概念相当陌生。我试图理解api网关在使用许多微服务的现代web应用程序中所起的作用。我一直在阅读express gateway的文档和教程,但对于web应用程序如何使用像express gateway这样设置的api网关执行身份验证,我有点困惑。 我的网络应用程序将有多个微服务可以与之对话。我认为,在我所有的微服务前面放置一个API网关可以使每个微服务不必担心用户

  • 我希望我的REST API服务器只能与我的iOS应用程序通信。用户群将不超过1000人,市场规模相当小,总体上不受欢迎。这就是为什么我认为,除了简单的质询-响应身份验证(HTTP、OAuth 2.0、SSL)之外的任何东西都是过火的。但我不确定这个认证应该如何进行。以下是我的想法: 客户端应用程序(用户)发送一个请求:api。实例com/auth?用户名=约翰 我的想法对吗?还是我完全错了?请记住

  • 问题内容: 我正在运行此节点服务器: 我想为此服务器添加登录表单。因此,当用户通过身份验证时,它将显示。 进程中查询功能,如果未提供任何命令,则将文件返回给客户端,因此我可以将登录命令从客户端发送到服务器,但是当服务器接收到带有用户名密码的登录命令时,服务器应如何处理,应如何处理登录请求并返回登录成功或失败的信息,编写此部分需要帮助。 我试过了。 请建议我该如何添加会话,我尝试在登录功能中添加,请