首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring启动时设置LDAP身份验证的超时值

在Spring启动时设置LDAP身份验证的超时值
EN

Stack Overflow用户
提问于 2017-03-01 03:41:18
回答 2查看 5.2K关注 0票数 7

我通过以下方式使用Spring LDAP身份验证:

代码语言:javascript
复制
auth
            .ldapAuthentication()
            .userSearchFilter("userPrincipalName={0}")
            .contextSource()
            .managerDn(ldapAuthenticationConfig.getManagerDn())
            .managerPassword(ldapAuthenticationConfig.getManagerPassword())
            .url(ldapAuthenticationConfig.getUrl());

然而,当LDAP服务器不可用时,它在登录页面上花费了太多时间。我想知道我是否可以在相当长的时间内登录。

下面是我使用的依赖项:

代码语言:javascript
复制
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-ldap</artifactId>
    </dependency>

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

EN

回答 2

Stack Overflow用户

发布于 2018-02-27 18:42:57

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

为此,首先提取上下文源的创建:

代码语言:javascript
复制
@Autowired
private DefaultSpringSecurityContextSource context;

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

然后,在创建上下文源时(我是在相同的配置类中创建的,没有构建器),您可以指定环境属性,并且可以在其中添加timeout属性:

代码语言:javascript
复制
@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变量都是常量。

票数 9
EN

Stack Overflow用户

发布于 2019-10-25 17:57:03

适用于使用.yml或.properties文件的用户

代码语言:javascript
复制
  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

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
</dependency>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42517398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档