首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >简单的Spring身份验证示例不适用于ActiveDirectory

简单的Spring身份验证示例不适用于ActiveDirectory
EN

Stack Overflow用户
提问于 2022-09-28 14:28:11
回答 1查看 236关注 0票数 0

我找到了一个非常简单的LDAP身份验证示例,它使用嵌入式LDAP服务器工作得很好:https://github.com/asbnotebook/spring-boot/tree/master/spring-security-embedded-ldap-example。这正是我所需要的-添加了一个配置类,现在所有用户都需要在访问应用程序之前登录。

由于我们的AD (本地服务器,而不是Azure )需要userDN和密码才能访问,所以我将其添加到示例代码中,还修改了url、基本dn等。

当我尝试登录时,我总是会收到“坏凭据”错误消息。然后,我遍历了代码,发现Spring代码成功地从AD检索了一些用户数据(我在"userDetails“对象中找到了用户电子邮件地址,只有在AD中才知道),但是"password”字段设置为null。然后将此空值与用户输入的密码进行比较,该密码失败,并在函数org.springframework.security.authentication.dao.additionalAuthenticationChecks().中抛出一个BadCredentialsException。

现在我有两个问题:

  1. 为什么将“密码”属性设置为null?我的理解是,它应该包含密码哈希。我用ldapsearch检查了AD响应,但是我没有看到任何看起来像密码哈希的东西。然而,userDN确实与其他应用程序一起工作,因此它可能不是userDN AD帐户的问题。请建议如何正确检索密码信息.

  1. 我相信这个示例不处理密码哈希。预加载示例应用程序的嵌入式LDAP服务器的LDIF文件只包含userPassword属性的明文密码。此外,示例代码中的passwordEncoder看起来像一个无操作程序编码器。我应该如何更改它以使它与AD一起工作呢?

这是我的代码:

代码语言:javascript
运行
复制
package com.asbnotebook.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.ldap.DefaultLdapUsernameToDnMapper;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
import org.springframework.security.ldap.userdetails.LdapUserDetailsManager;

@Configuration
public class LdapSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService() {
        
        var cs = new DefaultSpringSecurityContextSource("ldaps://ad.company.local/dc=company,dc=local");
        cs.setUserDn("cn=robot1,ou=robots");
        cs.setPassword("secret");
        cs.afterPropertiesSet();

        var manager = new LdapUserDetailsManager(cs);
        manager.setUsernameMapper(new DefaultLdapUsernameToDnMapper("ou=company_user", "cn"));
        manager.setGroupSearchBase("ou=company_groups");

        return manager;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-04 08:53:07

在考虑了Gabriel Luci's评论之后,我现在找到了一种使用ActiveDirectory进行身份验证的简单方法:

代码语言:javascript
运行
复制
package com.asbnotebook.example.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;

@Configuration
public class LdapSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        ActiveDirectoryLdapAuthenticationProvider adProvider =
                new ActiveDirectoryLdapAuthenticationProvider(
                        "company.de","ldaps://ad.company.local","dc=company,dc=local");
        adProvider.setConvertSubErrorCodesToExceptions(true);
        adProvider.setUseAuthenticationRequestCredentials(true);
        auth.authenticationProvider(adProvider);
        auth.eraseCredentials(false);
    }
}

可以使用电子邮件地址或sAMAccountName登录。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73883238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档