11 changed files with 315 additions and 50 deletions
@ -0,0 +1,42 @@ |
|||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
package org.acegisecurity.util; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.RuntimeBeanNameReference; |
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; |
||||||
|
import org.springframework.beans.factory.support.RootBeanDefinition; |
||||||
|
import org.springframework.util.StringUtils; |
||||||
|
import org.w3c.dom.Element; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Vishal Puri |
||||||
|
* |
||||||
|
*/ |
||||||
|
public class BeanDefinitionParserUtils { |
||||||
|
/** |
||||||
|
* Prevents instantiation |
||||||
|
*/ |
||||||
|
private BeanDefinitionParserUtils() { |
||||||
|
} |
||||||
|
|
||||||
|
public static void setConstructorArgumentIfAvailable(int index, Element element, String attribute, |
||||||
|
boolean isRunTimeBeanReference, RootBeanDefinition definition) { |
||||||
|
String propertyValue = element.getAttribute(attribute); |
||||||
|
if (StringUtils.hasText(propertyValue)) { |
||||||
|
if(!isRunTimeBeanReference){ |
||||||
|
definition.getConstructorArgumentValues().addIndexedArgumentValue(index, propertyValue); |
||||||
|
} else { |
||||||
|
definition.getConstructorArgumentValues().addIndexedArgumentValue(index, new RuntimeBeanNameReference(propertyValue)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static void setPropertyIfAvailable(Element element, String attribute, String property, |
||||||
|
RootBeanDefinition definition) { |
||||||
|
String propertyValue = element.getAttribute(attribute); |
||||||
|
if (StringUtils.hasText(propertyValue)) { |
||||||
|
definition.getPropertyValues().addPropertyValue(property, propertyValue); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,52 @@ |
|||||||
|
package org.acegisecurity.config; |
||||||
|
|
||||||
|
import junit.framework.TestCase; |
||||||
|
|
||||||
|
import org.acegisecurity.ldap.InitialDirContextFactory; |
||||||
|
import org.acegisecurity.providers.ldap.LdapAuthenticationProvider; |
||||||
|
import org.acegisecurity.providers.ldap.authenticator.BindAuthenticator; |
||||||
|
import org.springframework.beans.PropertyValue; |
||||||
|
import org.springframework.beans.PropertyValues; |
||||||
|
import org.springframework.beans.factory.config.BeanDefinition; |
||||||
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
||||||
|
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder; |
||||||
|
import org.springframework.beans.factory.support.ManagedList; |
||||||
|
import org.springframework.beans.factory.support.RootBeanDefinition; |
||||||
|
import org.springframework.context.ApplicationContext; |
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Vishal Puri |
||||||
|
* |
||||||
|
*/ |
||||||
|
public class LdapAuthenticationProviderBeanDefinitionParserTests extends TestCase { |
||||||
|
|
||||||
|
public void testBeanDefinitionCreation() { |
||||||
|
ApplicationContext context = new ClassPathXmlApplicationContext("org/acegisecurity/config/ldap-config.xml"); |
||||||
|
ConfigurableListableBeanFactory bf = (ConfigurableListableBeanFactory) context.getAutowireCapableBeanFactory(); |
||||||
|
BeanDefinition def = (RootBeanDefinition) bf.getBeanDefinition("authenticationManager"); |
||||||
|
assertNotNull(def); |
||||||
|
PropertyValues values = def.getPropertyValues(); |
||||||
|
PropertyValue value = values.getPropertyValue("providers"); |
||||||
|
assertNotNull(value); |
||||||
|
ManagedList list = (ManagedList) value.getValue(); |
||||||
|
assertEquals(1, list.size()); |
||||||
|
|
||||||
|
RootBeanDefinition definition = (RootBeanDefinition) list.get(0); |
||||||
|
assertEquals(LdapAuthenticationProvider.class, definition.getBeanClass()); |
||||||
|
|
||||||
|
assertEquals(2, definition.getConstructorArgumentValues().getArgumentCount()); |
||||||
|
|
||||||
|
ValueHolder holder = definition.getConstructorArgumentValues().getArgumentValue(0, BindAuthenticator.class); |
||||||
|
assertNotNull(holder.getConvertedValue() instanceof BindAuthenticator); |
||||||
|
RootBeanDefinition authenticatorDefinition = (RootBeanDefinition) holder.getValue(); |
||||||
|
assertEquals(1, authenticatorDefinition.getConstructorArgumentValues().getArgumentCount()); |
||||||
|
|
||||||
|
RootBeanDefinition initialContextDir = (RootBeanDefinition) authenticatorDefinition |
||||||
|
.getConstructorArgumentValues().getArgumentValue(0, InitialDirContextFactory.class).getValue(); |
||||||
|
assertEquals("cn=manager,dc=acegisecurity,dc=org", initialContextDir.getPropertyValues().getPropertyValue( |
||||||
|
"managerDn").getValue()); |
||||||
|
assertEquals("ldap://monkeymachine:389/dc=acegisecurity,dc=org", initialContextDir.getConstructorArgumentValues() |
||||||
|
.getArgumentValue(0, String.class).getValue()); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,73 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns:security="http://www.springframework.org/schema/security" |
||||||
|
xmlns:beans="http://www.springframework.org/schema/beans" |
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd |
||||||
|
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd"> |
||||||
|
|
||||||
|
<!-- http://www.springframework.org/schema/security file:/Users/vpuri/interface21/acegisecurity/trunk/acegisecurity/core/src/main/resources/org/acegisecurity/config/spring-security-2.0.xsd --> |
||||||
|
<!-- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd" --> |
||||||
|
|
||||||
|
<!-- make it optional, if not supplied autodetect all auth-providers from app ctx, using Ordered to resolve their order --> |
||||||
|
<security:authentication-mechanism id="authenticationManager"> |
||||||
|
<security:authentication-ldap |
||||||
|
ldapUrl="ldap://monkeymachine:389/dc=acegisecurity,dc=org" |
||||||
|
managerDn="cn=manager,dc=acegisecurity,dc=org" |
||||||
|
managerPassword="password" groupSearchBase="ou=groups" |
||||||
|
groupRoleAttribute="ou"> |
||||||
|
<security:property name="userDnPatterns"> |
||||||
|
<list> |
||||||
|
<value>uid={0},ou=people</value> |
||||||
|
</list> |
||||||
|
</security:property> |
||||||
|
</security:authentication-ldap> |
||||||
|
</security:authentication-mechanism> |
||||||
|
|
||||||
|
|
||||||
|
<!--<bean id="initialDirContextFactory" |
||||||
|
class="org.acegisecurity.ldap.DefaultInitialDirContextFactory"> |
||||||
|
<constructor-arg |
||||||
|
value="ldap://monkeymachine:389/dc=acegisecurity,dc=org" /> |
||||||
|
<property name="managerDn"> |
||||||
|
<value>cn=manager,dc=acegisecurity,dc=org</value> |
||||||
|
</property> |
||||||
|
<property name="managerPassword"> |
||||||
|
<value>password</value> |
||||||
|
</property> |
||||||
|
</bean> |
||||||
|
|
||||||
|
<bean id="ldapAuthProvider" |
||||||
|
class="org.acegisecurity.providers.ldap.LdapAuthenticationProvider"> |
||||||
|
<constructor-arg> |
||||||
|
<bean |
||||||
|
class="org.acegisecurity.providers.ldap.authenticator.BindAuthenticator"> |
||||||
|
<constructor-arg> |
||||||
|
<ref local="initialDirContextFactory" /> |
||||||
|
</constructor-arg> |
||||||
|
<property name="userDnPatterns"> |
||||||
|
<list> |
||||||
|
<value>uid={0},ou=people</value> |
||||||
|
</list> |
||||||
|
</property> |
||||||
|
</bean> |
||||||
|
</constructor-arg> |
||||||
|
<constructor-arg> |
||||||
|
<bean |
||||||
|
class="org.acegisecurity.providers.ldap.populator.DefaultLdapAuthoritiesPopulator"> |
||||||
|
<constructor-arg> |
||||||
|
<ref local="initialDirContextFactory" /> |
||||||
|
</constructor-arg> |
||||||
|
<constructor-arg> |
||||||
|
<value>ou=groups</value> |
||||||
|
</constructor-arg> |
||||||
|
<property name="groupRoleAttribute"> |
||||||
|
<value>ou</value> |
||||||
|
</property> |
||||||
|
</bean> |
||||||
|
</constructor-arg> |
||||||
|
</bean> |
||||||
|
--> |
||||||
|
|
||||||
|
</beans> |
||||||
Loading…
Reference in new issue