在Spring Boot中设置LDAP认证的超时值

8

我使用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认证的超时时间?

2个回答

10

我也遇到了这个问题,并找到了几个答案指出com.sun.jndi.ldap.connect.timeout环境变量,但无法找到如何在Spring Security与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_变量都是常量。


太好了!非常感谢你! - CelinHC

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

我将com.sun.jndi.ldap.connect.timeout: 500添加到了spring.ldap.base-environment中。

注意:我使用的是Spring框架。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接