Spring Security - получение учетных данных возвращает ноль - PullRequest
0 голосов
/ 05 февраля 2020

У меня есть приложение Springboot, использующее ActiveDirectory для аутентификации. Я намерен использовать учетные данные для дальнейшего запроса AD. К сожалению, метод getCredentials () возвращает null.

Моя конфигурация:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    Environment env;

    @Bean
    public UserDetailsContextMapper userDetailsContextMapper() {
        return new AppUserDetailsContextMapper();
    }

    @Bean
    public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        ActiveDirectoryLdapAuthenticationProvider provider =
                new ActiveDirectoryLdapAuthenticationProvider(
                        env.getRequiredProperty("spring.ldap.domain"),
                        env.getRequiredProperty("spring.ldap.urls")
                );
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);
        provider.setUserDetailsContextMapper(userDetailsContextMapper());
        return provider;
    }

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

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(
                Collections
                        .singletonList(activeDirectoryLdapAuthenticationProvider())
        );
    }

}

Я намерен использовать эти данные для дальнейшего запроса AD, используя сведения о текущем вошедшем в систему пользователе, как показано ниже:

    @Bean
    public AuthenticationSource authenticationSource(){
        return new SpringSecurityAuthenticationSource();
    }

    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setReferral("follow");
        contextSource.setUrl(env.getRequiredProperty("spring.ldap.urls"));
        contextSource.setBase(env.getRequiredProperty("spring.ldap.base"));
        contextSource.setAuthenticationSource(authenticationSource());
        return contextSource;
    }

Но при доступе к зарегистрированному пользователю getcredentials возвращает null

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(authentication.getCredentials()); // null result
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...