2017-02-28 217 views
4

LDAP身份验证设置超时时间我通过使用Spring LDAP认证。我想知道我是否可以在相当长的时间内登录。在春季启动

这里是我使用的依赖:

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

我怎样才能在春季启动设置LDAP身份验证的超时值?

回答

0

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

然后,创建上下文源(我做到了在同confiuration类,不制造商)的时候,你可以指定环境属性,并可以添加有超时属性:

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