mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-01 09:42:13 +00:00
SEC-1186: Make sure an Element is always supplied when registering the AuthenticationManager. Fixes broken tests.
This commit is contained in:
parent
980b9b73b8
commit
853b4c8753
@ -64,7 +64,7 @@ public class AnonymousBeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
provider.getPropertyValues().addPropertyValue(ATT_KEY, key);
|
provider.getPropertyValues().addPropertyValue(ATT_KEY, key);
|
||||||
|
|
||||||
parserContext.getRegistry().registerBeanDefinition(BeanIds.ANONYMOUS_AUTHENTICATION_PROVIDER, provider);
|
parserContext.getRegistry().registerBeanDefinition(BeanIds.ANONYMOUS_AUTHENTICATION_PROVIDER, provider);
|
||||||
ConfigUtils.addAuthenticationProvider(parserContext, BeanIds.ANONYMOUS_AUTHENTICATION_PROVIDER);
|
ConfigUtils.addAuthenticationProvider(parserContext, BeanIds.ANONYMOUS_AUTHENTICATION_PROVIDER, null);
|
||||||
|
|
||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ class AuthenticationProviderBeanDefinitionParser implements BeanDefinitionParser
|
|||||||
parserContext.getRegistry().registerBeanDefinition(name , cacheResolver);
|
parserContext.getRegistry().registerBeanDefinition(name , cacheResolver);
|
||||||
parserContext.registerComponent(new BeanComponentDefinition(cacheResolver, name));
|
parserContext.registerComponent(new BeanComponentDefinition(cacheResolver, name));
|
||||||
|
|
||||||
ConfigUtils.addAuthenticationProvider(parserContext, id);
|
ConfigUtils.addAuthenticationProvider(parserContext, id, element);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -84,8 +84,8 @@ abstract class ConfigUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
static void addAuthenticationProvider(ParserContext parserContext, String beanName) {
|
static void addAuthenticationProvider(ParserContext parserContext, String beanName, Element element) {
|
||||||
registerProviderManagerIfNecessary(parserContext, null);
|
registerProviderManagerIfNecessary(parserContext, element);
|
||||||
BeanDefinition authManager = parserContext.getRegistry().getBeanDefinition(BeanIds.AUTHENTICATION_MANAGER);
|
BeanDefinition authManager = parserContext.getRegistry().getBeanDefinition(BeanIds.AUTHENTICATION_MANAGER);
|
||||||
((ArrayList) authManager.getPropertyValues().getPropertyValue("providerBeanNames").getValue()).add(beanName);
|
((ArrayList) authManager.getPropertyValues().getPropertyValue("providerBeanNames").getValue()).add(beanName);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package org.springframework.security.config;
|
|||||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||||
import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
|
import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
|
||||||
import org.springframework.beans.factory.xml.ParserContext;
|
import org.springframework.beans.factory.xml.ParserContext;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -14,7 +15,7 @@ import org.w3c.dom.Node;
|
|||||||
*/
|
*/
|
||||||
public class CustomAuthenticationProviderBeanDefinitionDecorator implements BeanDefinitionDecorator {
|
public class CustomAuthenticationProviderBeanDefinitionDecorator implements BeanDefinitionDecorator {
|
||||||
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder holder, ParserContext parserContext) {
|
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder holder, ParserContext parserContext) {
|
||||||
ConfigUtils.addAuthenticationProvider(parserContext, holder.getBeanName());
|
ConfigUtils.addAuthenticationProvider(parserContext, holder.getBeanName(), (Element) node);
|
||||||
|
|
||||||
return holder;
|
return holder;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package org.springframework.security.config;
|
|||||||
|
|
||||||
import static org.springframework.security.config.FilterChainOrder.*;
|
import static org.springframework.security.config.FilterChainOrder.*;
|
||||||
|
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
@ -30,6 +32,7 @@ import org.springframework.security.access.ConfigAttribute;
|
|||||||
import org.springframework.security.access.SecurityConfig;
|
import org.springframework.security.access.SecurityConfig;
|
||||||
import org.springframework.security.access.vote.AuthenticatedVoter;
|
import org.springframework.security.access.vote.AuthenticatedVoter;
|
||||||
import org.springframework.security.access.vote.RoleVoter;
|
import org.springframework.security.access.vote.RoleVoter;
|
||||||
|
import org.springframework.security.authentication.AnonymousAuthenticationProvider;
|
||||||
import org.springframework.security.web.FilterChainProxy;
|
import org.springframework.security.web.FilterChainProxy;
|
||||||
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
|
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
|
||||||
import org.springframework.security.web.access.ExceptionTranslationFilter;
|
import org.springframework.security.web.access.ExceptionTranslationFilter;
|
||||||
@ -43,6 +46,7 @@ import org.springframework.security.web.access.expression.WebExpressionVoter;
|
|||||||
import org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource;
|
import org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource;
|
||||||
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
|
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
|
||||||
import org.springframework.security.web.access.intercept.RequestKey;
|
import org.springframework.security.web.access.intercept.RequestKey;
|
||||||
|
import org.springframework.security.web.authentication.AnonymousProcessingFilter;
|
||||||
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
|
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
|
||||||
import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter;
|
import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter;
|
||||||
import org.springframework.security.web.authentication.www.BasicProcessingFilter;
|
import org.springframework.security.web.authentication.www.BasicProcessingFilter;
|
||||||
@ -126,6 +130,17 @@ public class HttpSecurityBeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
static final String EXPRESSION_HANDLER_CLASS = "org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler";
|
static final String EXPRESSION_HANDLER_CLASS = "org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler";
|
||||||
private static final String EXPRESSION_HANDLER_ID = "_webExpressionHandler";
|
private static final String EXPRESSION_HANDLER_ID = "_webExpressionHandler";
|
||||||
|
|
||||||
|
final SecureRandom random;
|
||||||
|
|
||||||
|
public HttpSecurityBeanDefinitionParser() {
|
||||||
|
try {
|
||||||
|
random = SecureRandom.getInstance("SHA1PRNG");
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
// Shouldn't happen...
|
||||||
|
throw new RuntimeException("Failed find SHA1PRNG algorithm!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The aim of this method is to build the list of filters which have been defined by the namespace elements
|
* The aim of this method is to build the list of filters which have been defined by the namespace elements
|
||||||
* and attributes within the <http> configuration, along with any custom-filter's linked to user-defined
|
* and attributes within the <http> configuration, along with any custom-filter's linked to user-defined
|
||||||
@ -454,12 +469,50 @@ public class HttpSecurityBeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
private BeanDefinition createAnonymousFilter(Element element, ParserContext pc) {
|
private BeanDefinition createAnonymousFilter(Element element, ParserContext pc) {
|
||||||
Element anonymousElt = DomUtils.getChildElementByTagName(element, Elements.ANONYMOUS);
|
Element anonymousElt = DomUtils.getChildElementByTagName(element, Elements.ANONYMOUS);
|
||||||
|
|
||||||
if (anonymousElt == null || !"false".equals(anonymousElt.getAttribute("enabled"))) {
|
if (anonymousElt != null && "false".equals(anonymousElt.getAttribute("enabled"))) {
|
||||||
return new AnonymousBeanDefinitionParser().parse(anonymousElt, pc);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
String grantedAuthority = null;
|
||||||
|
String username = null;
|
||||||
|
String key = null;
|
||||||
|
Object source = null;
|
||||||
|
|
||||||
|
if (element != null) {
|
||||||
|
grantedAuthority = element.getAttribute("granted-authority");
|
||||||
|
username = element.getAttribute("username");
|
||||||
|
key = element.getAttribute("key");
|
||||||
|
source = pc.extractSource(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!StringUtils.hasText(grantedAuthority)) {
|
||||||
|
grantedAuthority = "ROLE_ANONYMOUS";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!StringUtils.hasText(username)) {
|
||||||
|
username = "anonymousUser";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!StringUtils.hasText(key)) {
|
||||||
|
// Generate a random key for the Anonymous provider
|
||||||
|
key = Long.toString(random.nextLong());
|
||||||
|
}
|
||||||
|
|
||||||
|
RootBeanDefinition filter = new RootBeanDefinition(AnonymousProcessingFilter.class);
|
||||||
|
|
||||||
|
PropertyValue keyPV = new PropertyValue("key", key);
|
||||||
|
filter.setSource(source);
|
||||||
|
filter.getPropertyValues().addPropertyValue("userAttribute", username + "," + grantedAuthority);
|
||||||
|
filter.getPropertyValues().addPropertyValue(keyPV);
|
||||||
|
|
||||||
|
RootBeanDefinition provider = new RootBeanDefinition(AnonymousAuthenticationProvider.class);
|
||||||
|
provider.setSource(source);
|
||||||
|
provider.getPropertyValues().addPropertyValue(keyPV);
|
||||||
|
|
||||||
|
pc.getRegistry().registerBeanDefinition(BeanIds.ANONYMOUS_AUTHENTICATION_PROVIDER, provider);
|
||||||
|
ConfigUtils.addAuthenticationProvider(pc, BeanIds.ANONYMOUS_AUTHENTICATION_PROVIDER, element);
|
||||||
|
|
||||||
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
private FilterAndEntryPoint createBasicFilter(Element elt, ParserContext pc, boolean autoConfig) {
|
private FilterAndEntryPoint createBasicFilter(Element elt, ParserContext pc, boolean autoConfig) {
|
||||||
@ -804,7 +857,7 @@ public class HttpSecurityBeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
|
|
||||||
BeanDefinition openIDProvider = openIDProviderBuilder.getBeanDefinition();
|
BeanDefinition openIDProvider = openIDProviderBuilder.getBeanDefinition();
|
||||||
pc.getRegistry().registerBeanDefinition(BeanIds.OPEN_ID_PROVIDER, openIDProvider);
|
pc.getRegistry().registerBeanDefinition(BeanIds.OPEN_ID_PROVIDER, openIDProvider);
|
||||||
ConfigUtils.addAuthenticationProvider(pc, BeanIds.OPEN_ID_PROVIDER);
|
ConfigUtils.addAuthenticationProvider(pc, BeanIds.OPEN_ID_PROVIDER, element);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (openIDFilter != null) {
|
if (openIDFilter != null) {
|
||||||
|
@ -100,7 +100,7 @@ public class LdapProviderBeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
LdapUserServiceBeanDefinitionParser.parseUserDetailsClassOrUserMapperRef(elt, parserContext));
|
LdapUserServiceBeanDefinitionParser.parseUserDetailsClassOrUserMapperRef(elt, parserContext));
|
||||||
parserContext.getRegistry().registerBeanDefinition(BeanIds.LDAP_AUTHENTICATION_PROVIDER, ldapProvider.getBeanDefinition());
|
parserContext.getRegistry().registerBeanDefinition(BeanIds.LDAP_AUTHENTICATION_PROVIDER, ldapProvider.getBeanDefinition());
|
||||||
|
|
||||||
ConfigUtils.addAuthenticationProvider(parserContext, BeanIds.LDAP_AUTHENTICATION_PROVIDER);
|
ConfigUtils.addAuthenticationProvider(parserContext, BeanIds.LDAP_AUTHENTICATION_PROVIDER, elt);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,11 @@ public class RememberMeBeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
pc.getRegistry().registerAlias(rememberMeServicesRef, BeanIds.REMEMBER_ME_SERVICES);
|
pc.getRegistry().registerAlias(rememberMeServicesRef, BeanIds.REMEMBER_ME_SERVICES);
|
||||||
}
|
}
|
||||||
|
|
||||||
registerProvider(pc, source, key);
|
RootBeanDefinition provider = new RootBeanDefinition(RememberMeAuthenticationProvider.class);
|
||||||
|
provider.setSource(source);
|
||||||
|
provider.getPropertyValues().addPropertyValue(ATT_KEY, key);
|
||||||
|
pc.getRegistry().registerBeanDefinition(BeanIds.REMEMBER_ME_AUTHENTICATION_PROVIDER, provider);
|
||||||
|
ConfigUtils.addAuthenticationProvider(pc, BeanIds.REMEMBER_ME_AUTHENTICATION_PROVIDER, element);
|
||||||
|
|
||||||
BeanDefinition filter = createFilter(pc, source);
|
BeanDefinition filter = createFilter(pc, source);
|
||||||
pc.popAndRegisterContainingComponent();
|
pc.popAndRegisterContainingComponent();
|
||||||
@ -123,15 +127,6 @@ public class RememberMeBeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
return servicesName;
|
return servicesName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerProvider(ParserContext pc, Object source, String key) {
|
|
||||||
//BeanDefinition authManager = ConfigUtils.registerProviderManagerIfNecessary(pc);
|
|
||||||
RootBeanDefinition provider = new RootBeanDefinition(RememberMeAuthenticationProvider.class);
|
|
||||||
provider.setSource(source);
|
|
||||||
provider.getPropertyValues().addPropertyValue(ATT_KEY, key);
|
|
||||||
pc.getRegistry().registerBeanDefinition(BeanIds.REMEMBER_ME_AUTHENTICATION_PROVIDER, provider);
|
|
||||||
ConfigUtils.addAuthenticationProvider(pc, BeanIds.REMEMBER_ME_AUTHENTICATION_PROVIDER);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BeanDefinition createFilter(ParserContext pc, Object source) {
|
private BeanDefinition createFilter(ParserContext pc, Object source) {
|
||||||
RootBeanDefinition filter = new RootBeanDefinition(RememberMeProcessingFilter.class);
|
RootBeanDefinition filter = new RootBeanDefinition(RememberMeProcessingFilter.class);
|
||||||
filter.setSource(source);
|
filter.setSource(source);
|
||||||
|
@ -43,7 +43,7 @@ public class X509BeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
|
|
||||||
BeanDefinition provider = new RootBeanDefinition(PreAuthenticatedAuthenticationProvider.class);
|
BeanDefinition provider = new RootBeanDefinition(PreAuthenticatedAuthenticationProvider.class);
|
||||||
parserContext.getRegistry().registerBeanDefinition(BeanIds.X509_AUTH_PROVIDER, provider);
|
parserContext.getRegistry().registerBeanDefinition(BeanIds.X509_AUTH_PROVIDER, provider);
|
||||||
ConfigUtils.addAuthenticationProvider(parserContext, BeanIds.X509_AUTH_PROVIDER);
|
ConfigUtils.addAuthenticationProvider(parserContext, BeanIds.X509_AUTH_PROVIDER, element);
|
||||||
|
|
||||||
String userServiceRef = element.getAttribute(ATT_USER_SERVICE_REF);
|
String userServiceRef = element.getAttribute(ATT_USER_SERVICE_REF);
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
<http-basic/>
|
<http-basic/>
|
||||||
<logout logout-success-url="/index.jsp"/>
|
<logout logout-success-url="/index.jsp"/>
|
||||||
<remember-me />
|
<remember-me />
|
||||||
|
<custom-filter ref="switchUserProcessingFilter" position="SWITCH_USER_FILTER"/>
|
||||||
</http>
|
</http>
|
||||||
|
|
||||||
<authentication-provider>
|
<authentication-provider>
|
||||||
@ -45,7 +46,6 @@
|
|||||||
based on the role granted the ability to 'switch' to another user -->
|
based on the role granted the ability to 'switch' to another user -->
|
||||||
<!-- In this example 'rod' has ROLE_SUPERVISOR that can switch to regular ROLE_USER(s) -->
|
<!-- In this example 'rod' has ROLE_SUPERVISOR that can switch to regular ROLE_USER(s) -->
|
||||||
<b:bean id="switchUserProcessingFilter" class="org.springframework.security.web.authentication.switchuser.SwitchUserProcessingFilter" autowire="byType">
|
<b:bean id="switchUserProcessingFilter" class="org.springframework.security.web.authentication.switchuser.SwitchUserProcessingFilter" autowire="byType">
|
||||||
<custom-filter position="SWITCH_USER_FILTER"/>
|
|
||||||
<b:property name="targetUrl" value="/secure/index.htm"/>
|
<b:property name="targetUrl" value="/secure/index.htm"/>
|
||||||
</b:bean>
|
</b:bean>
|
||||||
|
|
||||||
|
@ -37,8 +37,6 @@
|
|||||||
</td></tr>
|
</td></tr>
|
||||||
<tr><td colspan='2'><input name="exit" type="submit" value="Exit"></td></tr>
|
<tr><td colspan='2'><input name="exit" type="submit" value="Exit"></td></tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -38,6 +38,8 @@
|
|||||||
-->
|
-->
|
||||||
</http>
|
</http>
|
||||||
|
|
||||||
|
<authentication-manager alias="authManager"/>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
Usernames/Passwords are
|
Usernames/Passwords are
|
||||||
rod/koala
|
rod/koala
|
||||||
|
Loading…
x
Reference in New Issue
Block a user