【问题标题】:How can i get user role from LDAP with spring security如何使用 Spring Security 从 LDAP 获取用户角色
【发布时间】:2019-06-19 16:37:24
【问题描述】:

我能够连接 ldap 并获得响应,但在我的 Principal 对象权限中,我猜想角色详细信息可用的大小为零。 为了获取 ldap 角色详细信息,我需要传递哪些额外的输入?

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.ldapAuthentication()
        .userDnPatterns("uid={0},ou=TestOu")
        .contextSource()
        .url("ldaps://XX:768");
        }

我也尝试使用 DirContextOperations 对象,它包含除角色之外的许多属性,角色在 ldapit 中定义,我能够在运行 ldap 查询时获取角色, 问题只是通过弹簧安全

请帮忙

【问题讨论】:

    标签: spring-security-ldap


    【解决方案1】:

    对于 LDAP 目录服务器来说,“角色”并不真正意味着任何东西。

    LDAPv3 只知道静态组。

    某些 LDAP 目录服务器产品允许从入门级的“动态属性”检索组成员资格。

    您可以将“角色”定义为条目的属性。

    【讨论】:

    • 感谢您的快速回答 Bernhard,它已经在 ldap 中定义了,我可以在运行 ldap 查询时获得角色,问题只能通过 spring security
    • 使用 LDAP 进行身份验证意味着,执行 LDAP SEARCH 操作以检索条目的 DN,然后使用该 DN 和提供的密码执行 LDAP BIND 操作。您想使用特定属性填充 LdapUserDetails 对象还是使用“角色”属性的值来分配 Spring Security 角色?
    • 您是在 LDAP 连接中执行 ldap 查询吗? LDAP 连接被认证为“用户 DN”还是“管理 DN”?
    • 我可以使用 LDAP 配置登录,并且我需要在 httpSession 中设置用户角色(意思是来自 LDAP 的角色属性值),但是角色值不是从 LDAP 中返回的 .response 对象有很多值,如电子邮件、uid、employeNumber ..etc ,用户角色除外。通过 ldapsearch ,直接运行命令我可以看到结果
    • 那么LDAP目录服务器有访问控制限制返回属性。如果您没有指定所谓的“返回属性”,这将是最佳实践,那么所有属性都将返回,绑定 DN 允许查看。您可以将基于 Spring 的查询的访问日志条目与使用 ldapsearch 执行的查询进行比较。我怀疑在进一步的情况下使用了用户 DN。
    【解决方案2】:

    知道了!!!!实现一个自定义的 AuthenticationProvider 和 LdapAuthenticator,它使用了 BindAuthenticator。我们必须使用 BindAuthenticator 设置以下内容

         authenticator.setUserDnPatterns(new String[]{"XX"});
         authenticator.setUserAttributes(new String[]{"nsrole"});
    

    在配置中

    @覆盖 公共无效配置(AuthenticationManagerBuilder auth)抛出异常{

        auth.authenticationProvider(this.customLdapAuthenticationProvider());
    }
    
    @Bean(name = "ldapAuthenticationProvider")
    public AuthenticationProvider customLdapAuthenticationProvider() {
    
        LdapUserDetailsMapper userDetailsMapper = new UserMapper();
    
        CustomLdapAuthenticationProvider provider = new CustomLdapAuthenticationProvider(this.ldapAuthenticator(),
                new NullLdapAuthoritiesPopulator());
        provider.setUserDetailsContextMapper(userDetailsMapper);
    
        return provider;
    
    }
    
    @Bean(name = "ldapAuthenticator")
    public LdapAuthenticator ldapAuthenticator() {
    
        BindAuthenticator authenticator = new BindAuthenticator(this.contextSource());
        authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
        authenticator.setUserAttributes(new String[] { "nsrole" });
        return authenticator;
    }
    
    @Bean(name = "contextSource")
    public DefaultSpringSecurityContextSource contextSource() {
    
        DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(ldapUrl);
        return contextSource;
    }
    

    私有类 UserMapper 扩展了 LdapUserDetailsMapper {

        @Override
        public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
                Collection<? extends GrantedAuthority> authorities) {
    
    
            List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
    
            Attributes attrs = ctx.getAttributes();
    
                Sysout(attr)
            UserDetails userDetails = super.mapUserFromContext(ctx, username, roles);
    
            return userDetails;
        }
    

    }

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 2012-04-22
      • 2013-02-07
      • 1970-01-01
      • 2011-08-07
      • 2015-03-04
      • 2011-09-21
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多