01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package info.jtrac.config;
18:
19: import info.jtrac.Jtrac;
20: import info.jtrac.acegi.JtracLdapAuthenticationProvider;
21: import java.util.ArrayList;
22: import java.util.List;
23: import org.acegisecurity.providers.AuthenticationProvider;
24: import org.acegisecurity.providers.ProviderManager;
25: import org.slf4j.Logger;
26: import org.slf4j.LoggerFactory;
27: import org.springframework.beans.factory.FactoryBean;
28:
29: /**
30: * acegi authentication provider manager factory bean
31: * conditionally sets up ldap authentication
32: */
33: public class ProviderManagerFactoryBean implements FactoryBean {
34:
35: private final Logger logger = LoggerFactory.getLogger(getClass());
36:
37: private Jtrac jtrac;
38: private String ldapUrl;
39: private String activeDirectoryDomain;
40: private String searchBase;
41: private AuthenticationProvider authenticationProvider;
42:
43: public void setJtrac(Jtrac jtrac) {
44: this .jtrac = jtrac;
45: }
46:
47: public void setLdapUrl(String ldapUrl) {
48: this .ldapUrl = ldapUrl;
49: }
50:
51: public void setActiveDirectoryDomain(String activeDirectoryDomain) {
52: this .activeDirectoryDomain = activeDirectoryDomain;
53: }
54:
55: public void setSearchBase(String searchBase) {
56: this .searchBase = searchBase;
57: }
58:
59: public void setAuthenticationProvider(
60: AuthenticationProvider authenticationProvider) {
61: this .authenticationProvider = authenticationProvider;
62: }
63:
64: public Object getObject() throws Exception {
65: List providers = new ArrayList();
66: if (ldapUrl.length() > 0) {
67: logger.info("switching on ldap authentication provider");
68: JtracLdapAuthenticationProvider ldapProvider = new JtracLdapAuthenticationProvider();
69: ldapProvider.setLdapUrl(ldapUrl);
70: ldapProvider
71: .setActiveDirectoryDomain(activeDirectoryDomain);
72: ldapProvider.setSearchBase(searchBase);
73: ldapProvider.setJtrac(jtrac);
74: // **IMPORTANT!** we have to call this one time init ourselves
75: // as we are manually doing the factory stuff not Spring
76: ldapProvider.afterPropertiesSet();
77: // this is added at the top of the list or providers, and will fall back to local database
78: providers.add(ldapProvider);
79: } else {
80: logger.info("not using ldap authentication");
81: }
82: // add dependency injected local database based authentication
83: providers.add(authenticationProvider);
84: ProviderManager mgr = new ProviderManager();
85: mgr.setProviders(providers);
86: return mgr;
87: }
88:
89: public Class getObjectType() {
90: return ProviderManager.class;
91: }
92:
93: public boolean isSingleton() {
94: return true;
95: }
96:
97: }
|