在Spring Boot处设置LDAP身份验证的超时值



我使用spring ldap身份验证:

auth
            .ldapAuthentication()
            .userSearchFilter("userPrincipalName={0}")
            .contextSource()
            .managerDn(ldapAuthenticationConfig.getManagerDn())
            .managerPassword(ldapAuthenticationConfig.getManagerPassword())
            .url(ldapAuthenticationConfig.getUrl());

但是,当LDAP服务器不可用时,在登录页面上需要太多时间。我想了解是否可以在相当长的时间内登录。

这是我使用的依赖性:

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-ldap</artifactId>
    </dependency>

如何在Spring Boot处设置LDAP身份验证的超时值?

我也遇到了这个问题,并找到了一些指出com.sun.jndi.ldap.connect.timeout环境变量的答案,但找不到如何使用Java Config。

要完成它,首先提取上下文来源的创建:

@Autowired
private DefaultSpringSecurityContextSource context;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder
                .ldapAuthentication()
                .userSearchFilter(LDAP_USER_SEARCH_FILTER)
                .contextSource(context);
}

然后,当创建上下文源时(我在同一共产类中进行了,而无需构建器),您可以指定环境属性,并且可以添加超时属性:

@Bean
public DefaultSpringSecurityContextSource createContext() {
    DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(LDAP_SERVER);
    contextSource.setUserDn(LDAP_MANAGER_DN);
    contextSource.setPassword(LDAP_MANAGER_PASSWORD);
    Map<String, Object> environment = new HashMap<>();
    environment.put("com.sun.jndi.ldap.connect.timeout", LDAP_TIMEOUT);
    contextSource.setBaseEnvironmentProperties(environment);
    return contextSource;
}

请注意,大写LDAP_变量都是我的config类中的常数。

适用于使用.yml或.properties file

的人

  ldap:
    urls: LDAP://[YOUR FAKE DOMAIN OR IP]
    base: dc=fakedomain,dc=com
    username: [AD_USER_NAME]
    password: [AD_USER_PASSWORD]
    base-environment:
      com.sun.jndi.ldap.connect.timeout: 500

我将com.sun.jndi.ldap.connect.timeout: 500放入spring.ldap.base-enviroment

注意:我使用Spring

<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
</dependency>

最新更新