如何使用spring security对db或ldap动态验证用户?

时间:2017-09-07 14:05:15

标签: java spring spring-boot spring-security spring-security-ldap

我将身份验证配置为使用db和ldap,如下所示:

auth.ldapAuthentication()
        .groupSearchBase(groupSearchBase)
        .groupSearchFilter(groupFilter)
        .userSearchFilter(userFilter).userSearchBase(userSearchBase)
        .contextSource(contextSource())
        .and()
        .jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery(
        "SELECT lower(username), password, active from USER_BTABLE where lower(username)=lower(?) and LDAPAUTH=0"
).authoritiesByUsernameQuery("select lower(username), 'ROLE_USER' from USER_ATABLE where lower(username)=lower(?)");

问题是,如果用户在配置的ldap中也存在另一个以第6个身份验证请求开头的密码,则会出现以下异常:

 org.springframework.ldap.InvalidAttributeValueException: [LDAP: error code 19 - Exceed password retry limit. Please try later.];

如果用户设置了db auth标志,我会检查登录过滤器,我是否可以动态配置 AuthenticationManagerBuilder

1 个答案:

答案 0 :(得分:0)

我最终在doFilter方法中从db auth服务器实例到ldap auth服务器实例进行了307重定向:

httpResponse.setStatus(TEMPORARY_REDIRECT);
httpResponse.setHeader("Location", req.getScheme() + "://"redirectLocation);