Как настроить Spring boot LDAP Authentication для конкретного IP и порта - PullRequest
0 голосов
/ 14 июня 2019

У меня проблема с аутентификацией LDAP.Я пытаюсь настроить конфигурацию LDAP и использовать этот LDAP в моем API входа в систему при весенней загрузке.Я не знаю, правильно это или нет. Пожалуйста, кто-нибудь предлагает, как реализовать внутри API входа в систему.Ниже приведено мое кодирование конфигурации LDAP.

@Value("${spring.ldap.username}")
private String LDAP_USERNAME;

@Value("${spring.ldap.password}")
private String LDAP_PASSWORD;

@Value("${spring.ldap.urls}")
private String LDAP_URLS;

@Value("${spring.ldap.base}")
private String LDAP_BASE;

@Value("${spring.ldap.anonymous-read-only}")
private boolean LDAP_ReadOnly;

@Override
    public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        LdapContextSource lcs = new LdapContextSource();
        lcs.setUserDn(LDAP_USERNAME);
        lcs.setPassword(LDAP_PASSWORD);
        lcs.setUrl(LDAP_URLS);
        // lcs.setReferral("follow");
        lcs.setBase(LDAP_BASE);
        lcs.setAnonymousReadOnly(LDAP_ReadOnly);
        lcs.afterPropertiesSet();
        authenticationManagerBuilder.ldapAuthentication().contextSource(lcs).userSearchBase("ouBaseHere").groupSearchBase(LDAP_USERNAME).userSearchFilter("userNameSearchHere");
    }

Это мой код API входа в систему.

@PostMapping(value = {"/signin"})
public ResponseEntity < ?>signin(@Valid@RequestBody LoginRequest loginRequest) {
    System.out.println("Username : " + loginRequest.getUsernameOrEmail());
    System.out.println("Password : " + loginRequest.getPassword());
}

Это мои application.propeties.

spring.ldap.anonymous-read-only=true
spring.ldap.base= dc=example,dc=com
spring.ldap.password=XsR453!333@#q
spring.ldap.urls=ldap://192.168.111.1:1015
spring.ldap.username=test_usr

1 Ответ

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

Я включил LDAP в свой проект, поэтому вот информация, которая может помочь вам настроить ldap в вашей работе. Вам необходимо настроить аналогично тому, как указано ниже:

private LoginLdapSettings getLdapSettings() {
    LoginLdapSettings ldapSettings = new LoginLdapSettings();
    ldapSettings.setDn(/**Enter the Dn of the LDAP server**/);
    ldapSettings.setLdapUrl(/**Enter the URL of the LDAP server**/);
    ldapSettings.setLdapPassword(/**Enter the PASSWORD of the LDAP server**/);
    ldapSettings.setSearchFilter(/**Enter the SEARCH FILTER of the LDAP server**/);
    ldapSettings.setLdapUsername(/**Enter the USERNAME of the LDAP server**/);
    ldapSettings.setUserDnPattern(/**Enter the URL of the LDAP server**/);
    return ldapSettings;
}

Метод ниже, когда вы используете Active Directory

private ActiveDirectoryLdapAuthenticationProvider getActiveDirectoryLdapAuthenticationProvider(){

    LoginLdapSettings ldapSettings = getLdapSettings();
    StringBuilder ldapDomain = getLdapDomain(ldapSettings.getDn());
    ActiveDirectoryLdapAuthenticationProvider authenticationProvider = new ActiveDirectoryLdapAuthenticationProvider(ldapDomain.toString(), ldapSettings.getLdapUrl());
    authenticationProvider.setConvertSubErrorCodesToExceptions(true);
    authenticationProvider.setUseAuthenticationRequestCredentials(false);
    if(null != ldapSettings.getSearchFilter() && !ldapSettings.getSearchFilter().trim().isEmpty()) {
        authenticationProvider.setSearchFilter(ldapSettings.getSearchFilter().trim());
    }
    authenticationProvider.setUserDetailsContextMapper(ldapUserDetailsMapper);
    return authenticationProvider;
}

@Override
public void configure() throws ConfigurationException, InvalidAttributeValueException, IOException{
        auth.authenticationProvider(getActiveDirectoryLdapAuthenticationProvider());
}

Ниже приведен метод, когда вы используете OpenLDAP

@Override
public void configure() throws Exception {
    LoginLdapSettings ldapSettings = getLdapSettings();
    auth.ldapAuthentication()
    .contextSource()
    .url(ldapSettings.getLdapUrl() + "/" + ldapSettings.getDn())
    .managerDn(ldapSettings.getLdapUsername())
    .managerPassword(ldapSettings.getLdapPassword())
    .and()
    .userSearchFilter(searchFilter)     
    .userDnPatterns(ldapSettings.getUserDnPattern()) // I used this as the pattern "uid={0}"
    .userDetailsContextMapper(ldapUserDetailsMapper);
}

Пожалуйста, проголосуйте, если вам понравился ответ! :)

...