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

通过Active Directory LDAP使用Spring Security性进行身份验证

李星辰
2023-03-14

我无法使用真正的active directory进行身份验证,让我更好地解释一下,我尝试使用spring提出的示例进行身份验证。io无问题—内部服务启动时没有任何问题。参考https://spring.io/guides/gs/authenticating-ldap/

我试图通过插入active directory的配置来修改下面的代码,但没有成功。你能不能给我一个真实的例子,让我看看在不使用内部服务的情况下,如何建立真正的连接?我在网上查了一下,但发现一切都与官方的例子相似,没有任何真实的案例

@Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .ldapAuthentication()
                .userDnPatterns("uid={0},ou=people")
                .groupSearchBase("ou=groups")
                .contextSource()
                    .url("ldap://localhost:8389/dc=springframework,dc=org")
                    .and()
                .passwordCompare()
                    .passwordEncoder(new LdapShaPasswordEncoder())
                    .passwordAttribute("userPassword");
    }

错误显示:LDAP处理期间发生未分类异常;嵌套的异常是javax。命名。NamingException:[LDAP:错误代码1-000004DC:LDAPPER:DSID-0C0907C2,注释:为了执行此操作,必须在连接上完成成功绑定。数据0,v2580

共有2个答案

高夜洛
2023-03-14

我在这里找到了一个样本,很有用:

https://github.com/sachin-awati/Mojito/tree/master/webapp/src/main/java/com/box/l10n/mojito/security

您可以选择性地实现UserDetailsContextMapperImpl,如果在Active Directory查找过程中未找到用户,则它将覆盖mapUserFromContext以创建UserDetails对象。

@Component
public class UserDetailsContextMapperImpl implements UserDetailsContextMapper {

    @Override
    public UserDetails mapUserFromContext(DirContextOperations dirContextOperations, String username, Collection<? extends GrantedAuthority> authorities) {

        UserDetails userDetails = null;

        try {
            userDetails = userDetailsServiceImpl.loadUserByUsername(username);

        } catch (UsernameNotFoundException e) {
            String givenName = dirContextOperations.getStringAttribute("givenname");
            String surname = dirContextOperations.getStringAttribute("sn");
            String commonName = dirContextOperations.getStringAttribute("cn");

            userDetails = userDetailsServiceImpl.createBasicUser(username, givenName, surname, commonName);
        }

        return userDetails;
    }

确保您使用的是ActiveDirectoryLdapAuthentiationProviderSpring Security类,因为与其他LDAP服务器相比,Active Directory具有自己的细微差别。您可能需要在您的安全配置类中使用@EnableGlobal身份验证注释,因为您可以使用多个身份验证管理器构建器,这会让事情变得很混乱。

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

        ActiveDirectoryLdapAuthenticationProvider adProvider =
                new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://primarydc.domain.com:389");
        adProvider.setConvertSubErrorCodesToExceptions(true);
        adProvider.setUseAuthenticationRequestCredentials(true);
        auth.authenticationProvider(adProvider);
}

详情如下:https://github.com/spring-projects/spring-security/issues/4324 https://github.com/spring-projects/spring-security/issues/4571

亢保赫
2023-03-14

是的,通过LDAP进行身份验证太痛苦了。为了能够对AD执行身份验证,您需要使用ActiveDirectoryLdapAuthenticationProvider。以下是工作示例:

@Override
protected void configure(AuthenticationManagerBuilder auth) {
    ActiveDirectoryLdapAuthenticationProvider adProvider =
            new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://localhost:8389");
    adProvider.setConvertSubErrorCodesToExceptions(true);
    adProvider.setUseAuthenticationRequestCredentials(true);
    auth.authenticationProvider(adProvider);
}

为了节省您的时间,请阅读以下内容,这非常重要: AD身份验证文档

 类似资料:
  • 问题: 我们有一个spring的基于MVC的RESTful API,它包含敏感信息。API应该是安全的,但是不希望在每个请求中发送用户的凭据(User/Pass组合)。根据REST指南(和内部业务需求),服务器必须保持无状态。API将由另一台服务器以混搭方式使用。 要求: > 客户端请求使用凭据(不受保护的URL);服务器返回一个安全令牌,该令牌包含足够的信息,供服务器验证未来的请求并保持无状态。

  • 在我配置了下面的配置之后,它不会连接到Active Directory。我无法使用Active Directory的帐户登录。会有什么问题? 我有一个Ubuntu服务器18.04,带有ApacheGuacamoleV1。0.0. 安装。我想使用LDAP身份验证来验证用户。我已经下载了鳄梨酱-auth-ldap-1.0.0。jar和jldap-4.3。jar扩展。 10.10.10.21,10.10

  • 我正在尝试通过连接到LDAP使用Spring Security进行我的第一个演示。 我使用的Sping版本是:3.1.0.RELEASE 以下是我的security-integration.xml: 然而,每当我部署我的战争时,我都会遇到这个例外: HTTP状态500 - 类型异常报告 消息 描述服务器遇到一个内部错误(),该错误阻止它完成此请求。 例外情况 javax.servlet。Servl

  • 问题内容: 在(带有)中,我尝试使用以下语句通过基本身份验证访问我的网页: 但是Google Chrome浏览器在控制台中向我发出以下警告: [弃用]其URL包含嵌入式凭据(例如https://user:pass@host/)的子资源请求被阻止。有关更多详细信息,请参见https://www.chromestatus.com/feature/5669008342777856。 在标记的链接中提到该

  • (我曾在SO上回顾过类似的问题,但没有一个建议对我有效) 我有一个API服务,运行在Google App Engine上(在Google云平台上)。我需要通过谷歌管理目录API的身份验证,以便我可以在我们的GSuite域上创建组。 我最初尝试了隐式授权,遵循指南,以手动获取和提供服务帐户凭据,但我认为这可能仅限于在Google App Engine上运行的应用程序之间的通信。我的理解是,在App

  • 问题内容: 注销HTTP身份验证受保护的文件夹的 正确 方法是什么? 有一些解决方法可以实现这一目标,但是它们可能会带来危险,因为它们可能有故障或在某些情况下/浏览器中无法使用。这就是为什么我要寻找正确和清洁的解决方案。 问题答案: 亩。 没有正确的方法 ,甚至没有跨浏览器一致的方法。 这是来自HTTP规范(第15.6节)的问题: 现有的HTTP客户端和用户代理通常会无限期地保留身份验证信息。HT