Как я могу получить роль пользователя из LDAP с помощью Spring Security? - PullRequest
0 голосов
/ 13 июня 2019

Я могу соединиться с ldap и получить ответ, но в моем основном объекте полномочия размера равны нулю, в котором детали роли доступны, я думаю.Какие дополнительные входные данные мне нужно передать, чтобы получить подробные сведения о роли ldap?

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

Я также пытался использовать объект DirContextOperations, он содержит много атрибутов, кроме роли. Роль определена в ldapit, и я могучтобы получить роль при выполнении запроса ldap, проблема заключается только в безопасности пружины

. Пожалуйста, помогите

Ответы [ 2 ]

0 голосов
/ 19 июня 2019

Понял !!!!!реализуя собственный AuthenticationProvider и LdapAuthenticator, и он использовал BindAuthenticator.Мы должны установить следующее с помощью BindAuthenticator

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

В Config

@ Переопределить public void configure (AuthenticationManagerBuilder auth) выдает Exception {

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

закрытый класс, расширяемый UserMapperLdapUserDetailsMapper {

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

}

0 голосов
/ 13 июня 2019

«Роль» ничего не значит для сервера каталогов LDAP.

LDAPv3 знает только о статических группах.

Некоторые продукты LDAP Directory Server позволяют получать членство в группах из «динамического атрибута» на начальном уровне.

Вы можете определить «роль» как атрибут для записей.

...