【问题标题】:Setting Timeout Value for LDAP authentication at Spring Boot在 Spring Boot 中设置 LDAP 身份验证的超时值
【发布时间】:2019-10-25 17:57:03
【问题描述】:

我通过以下方式使用 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 身份验证的超时值?

【问题讨论】:

    标签: spring-boot timeout spring-ldap


    【解决方案1】:

    我也遇到了这个问题,找了几个答案指出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);
    }
    

    然后,在创建上下文源时(我是在同一个配置类中做的,没有builder),你可以指定环境属性,你可以在那里添加超时属性:

    @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_ 变量在我的配置类中都是常量。

    【讨论】:

    • 太棒了!非常感谢!
    【解决方案2】:

    对于那些使用 .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-enviroment

    注意:我用的是弹簧

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

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 1970-01-01
      • 2022-01-20
      • 2017-07-30
      • 2019-11-13
      • 2017-07-06
      • 2018-03-11
      • 1970-01-01
      • 2020-09-08
      相关资源
      最近更新 更多