LDAP Spring Security未引发密码策略错误

时间:2018-09-18 22:52:12

标签: spring spring-security ldap freeipa password-policy

我对使用LDAP的Spring Security来说还很陌生,我正在尝试使用LDAP服务器(FreeIPA)上密码已过期的用户进行身份验证。

我似乎无法触发任何密码过期异常等。“密码策略响应控件”始终返回null。...

这是我的代码,也许我做错了什么。如何处理密码策略错误?他们目前根本不会开火。

<bean id="freeIpaContextSource" class="org.springframework.security.ldap.ppolicy.PasswordPolicyAwareContextSource">
    <constructor-arg value="${logon.freeipa.zone.ldap.connection.url}"/>
    <property name="base" value="${logon.freeipa.zone.user.dn.base}"/>
</bean>

<bean id="freeIpaLdapTemplate" class="org.springframework.security.ldap.SpringSecurityLdapTemplate">
    <constructor-arg name="contextSource" ref="freeIpaContextSource"/>
</bean>

我有一个自定义的LdapAuthenticator,下面使用ldaptemplate来验证用户身份。

@Override
public DirContextOperations authenticate(Authentication authentication) {
    checkForIllegalStateDuringAuthentication(authentication);
    logger.info(String.format("*** Beginning to authenticate against LDAP zone %s ***", authorizationZone.getName()));
    zoneAuthenticationService.saveRequestDataInSession((UsernamePasswordAuthenticationToken) authentication, authorizationZone.getName());
    CollectingAuthenticationErrorCallback errorCallback = new CollectingAuthenticationErrorCallback();
    boolean isAuthenticated = false;
    String userName = authentication.getName();
    String password = authentication.getCredentials().toString();
    String filterLookup = buildLDAPFilterLookupString(userName);
    if (StringUtils.isNotBlank(password)) {
        logger.info(String.format("*** Attempting authentication for user %s ***", userName));
        try {
            isAuthenticated = ldapTemplate.authenticate(StringUtils.EMPTY, filterLookup, password, errorCallback);

        } catch (Exception exception) {
            errorCallback.execute(exception);
        }
    }
    if (!isAuthenticated) {
        if (errorCallback.getError() == null) {
            errorCallback.execute(new AuthenticationException(null));
        }
        //Any LDAP exception caught are stored inside the errorCallBack for use later to display an appropriate error.
        logger.error(String.format("*** Authentication for user %s has failed. Exception has occurred while system performed LDAP authentication. ***", userName), errorCallback.getError());
        throw new LdapAuthenticationException(errorCallback.getError().getMessage(), errorCallback.getError());
    }
    logger.info(String.format("*** Authentication for user %s has succeeded ***", userName));
    return new DirContextAdapter(buildAuthenticatedDN(userName));
}

无论我做什么,我都无法获得任何密码策略错误来返回。据我了解,您在尝试进行身份验证时需要使用PasswordPolicyControl设置请求控件,但是我从服务器上从未收到任何响应控件。我已经尝试实现以下类似的方法,但是在任何方面都没有运气。

LdapContext context = (LdapContext)ldapTemplate.getContextSource().getContext(buildAuthenticatedDN(userName).toString(), password);
Control[] rctls = new Control[]{new PasswordPolicyControl(false)};
context.reconnect(rctls);
PasswordPolicyResponseControl ctrl = PasswordPolicyControlExtractor.extractControl(context);
//ctrl is always null
 if (ctrl.isExpired()) {
                throw new 
PasswordPolicyException(ctrl.getErrorStatus());
            }

我在做什么错?我为此非常努力,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果您的客户确实发送了正确的响应控制,则可能会遇到此问题(已存在7年了):

#1539 [RFE] Add code to check password expiration on ldap bind

IIRC FreeIPA 仅在Kerberos预身份验证(kinit)期间强制执行密码过期。