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

kam*_*aci 6 timeout spring-ldap spring-boot

我使用Spring LDAP身份验证:

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

但是,当LDAP服务器不可用时,在登录页面上花费太多时间.我想知道我是否可以在相当长的一段时间内登录.

这是我使用的依赖项:

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-ldap</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

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

Mát*_*ond 9

我也遇到了这个问题,找到了几个指出com.sun.jndi.ldap.connect.timeout环境变量的答案,但是找不到如何用Java Config添加到Spring Security。

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

@Autowired
private DefaultSpringSecurityContextSource context;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder
                .ldapAuthentication()
                .userSearchFilter(LDAP_USER_SEARCH_FILTER)
                .contextSource(context);
}
Run Code Online (Sandbox Code Playgroud)

然后,在创建上下文源时(我在同一个配置类中完成,没有构建器),您可以指定环境属性,并且可以在那里添加超时属性:

@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;
}
Run Code Online (Sandbox Code Playgroud)

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


nok*_*eng 6

对于那些使用 .yml 或 .properties 文件的人


  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

Run Code Online (Sandbox Code Playgroud)

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

注意:我用的是弹簧

<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)