PasswordEncoder as Bean default for XML
Issue: gh-4873
This commit is contained in:
@@ -26,6 +26,7 @@ import org.springframework.security.authentication.ProviderManager;
|
||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||
import org.springframework.security.config.BeanIds;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -49,21 +50,24 @@ public class AuthenticationManagerFactoryBean implements
|
||||
return (AuthenticationManager) bf.getBean(BeanIds.AUTHENTICATION_MANAGER);
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException e) {
|
||||
if (BeanIds.AUTHENTICATION_MANAGER.equals(e.getBeanName())) {
|
||||
try {
|
||||
UserDetailsService uds = bf.getBean(UserDetailsService.class);
|
||||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||
provider.setUserDetailsService(uds);
|
||||
provider.afterPropertiesSet();
|
||||
return new ProviderManager(
|
||||
Arrays.<AuthenticationProvider> asList(provider));
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException noUds) {
|
||||
}
|
||||
throw new NoSuchBeanDefinitionException(BeanIds.AUTHENTICATION_MANAGER,
|
||||
MISSING_BEAN_ERROR_MESSAGE);
|
||||
if (!BeanIds.AUTHENTICATION_MANAGER.equals(e.getBeanName())) {
|
||||
throw e;
|
||||
}
|
||||
throw e;
|
||||
|
||||
UserDetailsService uds = getBeanOrNull(UserDetailsService.class);
|
||||
if(uds == null) {
|
||||
throw new NoSuchBeanDefinitionException(BeanIds.AUTHENTICATION_MANAGER,
|
||||
MISSING_BEAN_ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||
provider.setUserDetailsService(uds);
|
||||
PasswordEncoder passwordEncoder = getBeanOrNull(PasswordEncoder.class);
|
||||
if (passwordEncoder != null) {
|
||||
provider.setPasswordEncoder(passwordEncoder);
|
||||
}
|
||||
provider.afterPropertiesSet();
|
||||
return new ProviderManager(Arrays.<AuthenticationProvider> asList(provider));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,4 +83,11 @@ public class AuthenticationManagerFactoryBean implements
|
||||
bf = beanFactory;
|
||||
}
|
||||
|
||||
private <T> T getBeanOrNull(Class<T> type) {
|
||||
try {
|
||||
return this.bf.getBean(type);
|
||||
} catch (NoSuchBeanDefinitionException noUds) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.security.config.authentication;
|
||||
|
||||
import org.springframework.beans.BeanMetadataElement;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
@@ -36,17 +37,16 @@ public class AuthenticationProviderBeanDefinitionParser implements BeanDefinitio
|
||||
private static final String ATT_USER_DETAILS_REF = "user-service-ref";
|
||||
|
||||
public BeanDefinition parse(Element element, ParserContext pc) {
|
||||
RootBeanDefinition authProvider = new RootBeanDefinition(
|
||||
DaoAuthenticationProvider.class);
|
||||
RootBeanDefinition authProvider = new RootBeanDefinition(DaoAuthenticationProvider.class);
|
||||
authProvider.setSource(pc.extractSource(element));
|
||||
|
||||
Element passwordEncoderElt = DomUtils.getChildElementByTagName(element,
|
||||
Elements.PASSWORD_ENCODER);
|
||||
Element passwordEncoderElt = DomUtils.getChildElementByTagName(element, Elements.PASSWORD_ENCODER);
|
||||
|
||||
if (passwordEncoderElt != null) {
|
||||
PasswordEncoderParser pep = new PasswordEncoderParser(passwordEncoderElt, pc);
|
||||
authProvider.getPropertyValues().addPropertyValue("passwordEncoder",
|
||||
pep.getPasswordEncoder());
|
||||
PasswordEncoderParser pep = new PasswordEncoderParser(passwordEncoderElt, pc);
|
||||
BeanMetadataElement passwordEncoder = pep.getPasswordEncoder();
|
||||
if (passwordEncoder != null) {
|
||||
authProvider.getPropertyValues()
|
||||
.addPropertyValue("passwordEncoder", passwordEncoder);
|
||||
}
|
||||
|
||||
Element userServiceElt = DomUtils.getChildElementByTagName(element,
|
||||
|
||||
@@ -56,6 +56,12 @@ public class PasswordEncoderParser {
|
||||
}
|
||||
|
||||
private void parse(Element element, ParserContext parserContext) {
|
||||
if (element == null) {
|
||||
if (parserContext.getRegistry().containsBeanDefinition("passwordEncoder")) {
|
||||
this.passwordEncoder = parserContext.getRegistry().getBeanDefinition("passwordEncoder");
|
||||
}
|
||||
return;
|
||||
}
|
||||
String hash = element.getAttribute(ATT_HASH);
|
||||
boolean useBase64 = false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user