001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.acegisecurity.config;
016:
017: import org.acegisecurity.ldap.DefaultInitialDirContextFactory;
018: import org.acegisecurity.providers.ProviderManager;
019: import org.acegisecurity.providers.ldap.LdapAuthenticationProvider;
020: import org.acegisecurity.providers.ldap.authenticator.BindAuthenticator;
021: import org.acegisecurity.providers.ldap.populator.DefaultLdapAuthoritiesPopulator;
022: import org.acegisecurity.util.BeanDefinitionParserUtils;
023: import org.springframework.beans.factory.config.RuntimeBeanReference;
024: import org.springframework.beans.factory.support.AbstractBeanDefinition;
025: import org.springframework.beans.factory.support.ManagedList;
026: import org.springframework.beans.factory.support.RootBeanDefinition;
027: import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
028: import org.springframework.beans.factory.xml.BeanDefinitionParser;
029: import org.springframework.beans.factory.xml.ParserContext;
030: import org.springframework.util.Assert;
031: import org.springframework.util.StringUtils;
032: import org.springframework.util.xml.DomUtils;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.Node;
035: import org.w3c.dom.NodeList;
036:
037: /**
038: * * {@link BeanDefinitionParser} for the <code>authentication-mechanism</code>
039: * tag, resolves to {@link org.acegisecurity.providers.ProviderManager} </br>
040: *
041: * @author vpuri
042: * @see {@link org.springframework.beans.factory.BeanFactory}
043: * @see {@link org.acegisecurity.providers.ProviderManager}
044: *
045: */
046: public class AuthenticationMechanismBeanDefinitionParser extends
047: AbstractBeanDefinitionParser implements BeanDefinitionParser {
048: // ~ Instance fields
049: // ================================================================================================
050:
051: private static final String AUTHENTICATION_JDBC = "authentication-jdbc";
052:
053: private static final String AUTHENTICATION_LDAP = "authentication-ldap";
054:
055: private static final String REF = "ref";
056:
057: // ~ Methods
058: // ========================================================================================================
059: protected AbstractBeanDefinition parseInternal(Element element,
060: ParserContext parserContext) {
061:
062: ManagedList providers = new ManagedList();
063: Assert.notNull(parserContext, "ParserContext must not be null");
064: RootBeanDefinition authMechanismBeanDef = new RootBeanDefinition(
065: ProviderManager.class);
066: NodeList childNodes = element.getChildNodes();
067:
068: for (int i = 0, n = childNodes.getLength(); i < n; i++) {
069: Node node = childNodes.item(i);
070:
071: if (node.getNodeType() == Node.ELEMENT_NODE) {
072: Element childElement = (Element) node;
073: // this.providerExists = true;
074:
075: if (AUTHENTICATION_JDBC.equals(node.getLocalName())) {
076: String attribute = childElement.getAttribute(REF);
077: if (StringUtils.hasLength(attribute)) {
078: // create a beandefinition
079: providers.add(new RuntimeBeanReference(
080: attribute));
081: }
082: } else if (AUTHENTICATION_LDAP.equals(node
083: .getLocalName())) {
084: providers
085: .add(createLdapAuthencticationProviderBeanDefinition(
086: childElement, parserContext));
087: }
088: }
089: authMechanismBeanDef.getPropertyValues().addPropertyValue(
090: "providers", providers);
091:
092: }
093: return authMechanismBeanDef;
094: }
095:
096: /**
097: * Creates a default bean definition.
098: * @return
099: */
100: protected static RootBeanDefinition createAndRegisterBeanDefinitionWithDefaults(
101: ParserContext parserContext) {
102: RootBeanDefinition beanDefinition = new RootBeanDefinition(
103: ProviderManager.class);
104: ManagedList providers = new ManagedList();
105: // create authentication-repository (DaoAuthenticationProvider) and add
106: // that to list
107: RootBeanDefinition authRepo = AuthenticationRepositoryBeanDefinitionParser
108: .createBeanDefinitionWithDefaults();
109: providers.add(authRepo);
110: beanDefinition.getPropertyValues().addPropertyValue(
111: "providers", providers);
112: parserContext.getReaderContext().registerWithGeneratedName(
113: beanDefinition);
114: return beanDefinition;
115: }
116:
117: protected static RootBeanDefinition createLdapAuthencticationProviderBeanDefinition(
118: Element element, ParserContext parserContext) {
119: // element ldap
120: RootBeanDefinition ldapAuthProvider = new RootBeanDefinition(
121: LdapAuthenticationProvider.class);
122: RootBeanDefinition initialDirContextFactory = createInitialDirContextFactoryBeanDefinition(element);
123: RootBeanDefinition ldapAuthoritiesPopulator = new RootBeanDefinition(
124: DefaultLdapAuthoritiesPopulator.class);
125:
126: RootBeanDefinition bindAuthenticator = new RootBeanDefinition(
127: BindAuthenticator.class);
128: Element property = DomUtils.getChildElementByTagName(element,
129: "property");
130: Assert.notNull(property);
131: parserContext.getDelegate().parsePropertyElement(property,
132: bindAuthenticator);
133: bindAuthenticator.getConstructorArgumentValues()
134: .addIndexedArgumentValue(0, initialDirContextFactory);
135:
136: // LdapAuthenticator
137: ldapAuthProvider.getConstructorArgumentValues()
138: .addIndexedArgumentValue(0, bindAuthenticator);
139:
140: ldapAuthoritiesPopulator.getConstructorArgumentValues()
141: .addIndexedArgumentValue(0, initialDirContextFactory);
142: BeanDefinitionParserUtils.setConstructorArgumentIfAvailable(1,
143: element, "groupSearchBase", false,
144: ldapAuthoritiesPopulator);
145: BeanDefinitionParserUtils.setPropertyIfAvailable(element,
146: "groupRoleAttribute", "groupRoleAttribute", false,
147: ldapAuthoritiesPopulator);
148:
149: // LdapAuthoritiesPopulator
150: ldapAuthProvider.getConstructorArgumentValues()
151: .addIndexedArgumentValue(1, ldapAuthoritiesPopulator);
152:
153: return ldapAuthProvider;
154:
155: }
156:
157: private static RootBeanDefinition createInitialDirContextFactoryBeanDefinition(
158: Element element) {
159: RootBeanDefinition initialDirContextFactory = new RootBeanDefinition(
160: DefaultInitialDirContextFactory.class);
161: BeanDefinitionParserUtils.setConstructorArgumentIfAvailable(0,
162: element, "ldapUrl", false, initialDirContextFactory);
163: BeanDefinitionParserUtils.setPropertyIfAvailable(element,
164: "managerDn", "managerDn", false,
165: initialDirContextFactory);
166: BeanDefinitionParserUtils.setPropertyIfAvailable(element,
167: "managerPassword", "managerPassword", false,
168: initialDirContextFactory);
169: return initialDirContextFactory;
170: }
171: }
|