diff --git a/core/src/main/java/org/springframework/security/config/LdapProviderBeanDefinitionParser.java b/core/src/main/java/org/springframework/security/config/LdapProviderBeanDefinitionParser.java index 7cf2d56b42..94ecf3d9b3 100644 --- a/core/src/main/java/org/springframework/security/config/LdapProviderBeanDefinitionParser.java +++ b/core/src/main/java/org/springframework/security/config/LdapProviderBeanDefinitionParser.java @@ -27,7 +27,8 @@ public class LdapProviderBeanDefinitionParser implements BeanDefinitionParser { private Log logger = LogFactory.getLog(getClass()); private static final String ATT_USER_DN_PATTERN = "user-dn-pattern"; - private static final String ATT_USER_PASSWORD= "password-attribute"; + private static final String ATT_USER_PASSWORD = "password-attribute"; + private static final String ATT_HASH = PasswordEncoderParser.ATT_HASH; private static final String DEF_USER_SEARCH_FILTER="uid={0}"; @@ -51,8 +52,9 @@ public class LdapProviderBeanDefinitionParser implements BeanDefinitionParser { searchBean.getConstructorArgumentValues().addIndexedArgumentValue(2, contextSource); } - RootBeanDefinition authenticator = new RootBeanDefinition(BindAuthenticator.class); + RootBeanDefinition authenticator = new RootBeanDefinition(BindAuthenticator.class); Element passwordCompareElt = DomUtils.getChildElementByTagName(elt, Elements.LDAP_PASSWORD_COMPARE); + if (passwordCompareElt != null) { authenticator = new RootBeanDefinition(PasswordComparisonAuthenticator.class); @@ -62,16 +64,24 @@ public class LdapProviderBeanDefinitionParser implements BeanDefinitionParser { } Element passwordEncoderElement = DomUtils.getChildElementByTagName(passwordCompareElt, Elements.PASSWORD_ENCODER); + String hash = passwordCompareElt.getAttribute(ATT_HASH); if (passwordEncoderElement != null) { + if (StringUtils.hasText(hash)) { + parserContext.getReaderContext().warning("Attribute 'hash' cannot be used with 'password-encoder' and " + + "will be ignored.", parserContext.extractSource(elt)); + } PasswordEncoderParser pep = new PasswordEncoderParser(passwordEncoderElement, parserContext); authenticator.getPropertyValues().addPropertyValue("passwordEncoder", pep.getPasswordEncoder()); if (pep.getSaltSource() != null) { parserContext.getReaderContext().warning("Salt source information isn't valid when used with LDAP", passwordEncoderElement); } + } else if (StringUtils.hasText(hash)) { + Class encoderClass = (Class) PasswordEncoderParser.ENCODER_CLASSES.get(hash); + authenticator.getPropertyValues().addPropertyValue("passwordEncoder", new RootBeanDefinition(encoderClass)); } - } + } authenticator.getConstructorArgumentValues().addGenericArgumentValue(contextSource); authenticator.getPropertyValues().addPropertyValue("userDnPatterns", userDnPatternArray); diff --git a/core/src/test/java/org/springframework/security/config/LdapProviderBeanDefinitionParserTests.java b/core/src/test/java/org/springframework/security/config/LdapProviderBeanDefinitionParserTests.java index ad8a143be1..b1898eb6b3 100644 --- a/core/src/test/java/org/springframework/security/config/LdapProviderBeanDefinitionParserTests.java +++ b/core/src/test/java/org/springframework/security/config/LdapProviderBeanDefinitionParserTests.java @@ -41,9 +41,31 @@ public class LdapProviderBeanDefinitionParserTests { public void missingServerEltCausesConfigException() { setContext(""); } + @Test public void supportsPasswordComparisonAuthentication() { + setContext(" " + + "" + + " " + + ""); + LdapAuthenticationProvider provider = getProvider(); + provider.authenticate(new UsernamePasswordAuthenticationToken("ben", "benspassword")); + } + + + @Test + public void supportsPasswordComparisonAuthenticationWithHashAttribute() { + setContext(" " + + "" + + " " + + ""); + LdapAuthenticationProvider provider = getProvider(); + provider.authenticate(new UsernamePasswordAuthenticationToken("ben", "ben")); + } + + @Test + public void supportsPasswordComparisonAuthenticationWithPasswordEncoder() { setContext(" " + "" + " " + @@ -52,12 +74,11 @@ public class LdapProviderBeanDefinitionParserTests { ""); LdapAuthenticationProvider provider = getProvider(); provider.authenticate(new UsernamePasswordAuthenticationToken("ben", "ben")); - } - + } + private void setContext(String context) { appCtx = new InMemoryXmlApplicationContext(context); - } - + } private LdapAuthenticationProvider getProvider() { ProviderManager authManager = (ProviderManager) appCtx.getBean(BeanIds.AUTHENTICATION_MANAGER);