views:

495

answers:

2

I'm brand spanking new at Spring and have gotten a majority of the knowledge I do have from the Spring Recipes book from Apress.

I've got LDAP authentication working with Spring Security within one webapp. I would like to rip out my application context beans and properties files from this one webapp, however, and somehow externalize them so that all of our webapps can reference the same beans. So when we need to change something (like the ldapuser or the ldap urls), we change it in one place and the rest of the apps just know.

UPDATE I've implemented Reloadable Spring Properties which is reloading properties when the files they come from are touched. I am using encrypted properties, however, so below is class I created on top of the Reloadable Spring Properties ones.

ReloadingEncryptablePropertyPlaceholderConfigurer.java

package;

import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang.Validate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.util.text.TextEncryptor;
import org.jasypt.properties.PropertyValueEncryptionUtils;

import org.springframework.beans.factory.BeanDefinitionStoreException;

public class ReloadingEncryptablePropertyPlaceholderConfigurer extends ReloadingPropertyPlaceholderConfigurer {

    protected final Log logger = LogFactory.getLog(getClass());
    private final StringEncryptor stringEncryptor;
    private final TextEncryptor textEncryptor;

    public ReloadingEncryptablePropertyPlaceholderConfigurer(TextEncryptor textEncryptor) {
        super();
        logger.info("Creating configurer with TextEncryptor");
        Validate.notNull(textEncryptor, "Encryptor cannot be null");
        this.stringEncryptor = null;
        this.textEncryptor = textEncryptor;
    }

    public ReloadingEncryptablePropertyPlaceholderConfigurer(StringEncryptor stringEncryptor) {
        super();
        logger.info("Creating configurer with StringEncryptor");
        Validate.notNull(stringEncryptor, "Encryptor cannot be null");
        this.stringEncryptor = stringEncryptor;
        this.textEncryptor = null;
    }

    @Override
    protected String convertPropertyValue(String originalValue) {
        if (!PropertyValueEncryptionUtils.isEncryptedValue(originalValue)) {
            return originalValue;
        }
        if (this.stringEncryptor != null) {
            return PropertyValueEncryptionUtils.decrypt(originalValue, this.stringEncryptor);
        }
        return PropertyValueEncryptionUtils.decrypt(originalValue, this.textEncryptor);
    }

    @Override
    protected String parseStringValue(String strVal, Properties props, Set visitedPlaceholders) throws BeanDefinitionStoreException {
        return convertPropertyValue(super.parseStringValue(strVal, props, visitedPlaceholders));
    }
}

And here's how I use it in my securityContext.xml:

<bean id="securityContextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
    <constructor-arg value="ldaps://ldapserver" />
    <property name="urls" value="#{ldap.urls}" />
</bean>

<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <bean id="reloadProperties" class="org.springframework.scheduling.timer.ScheduledTimerTask">
            <property name="period" value="1000"/>
            <property name="runnable">
                <bean class="ReloadConfiguration">
                    <property name="reconfigurableBeans">
                        <list>
                            <ref bean="configproperties"/>
                        </list>
                    </property>
                </bean>
            </property>
        </bean>
    </property>
</bean>

<bean id="configproperties" class="ReloadablePropertiesFactoryBean">
    <property name="location" value="classpath:ldap.properties"/>
</bean>

<bean id="ldapPropertyConfigurer" class="ReloadingEncryptablePropertyPlaceholderConfigurer">
    <constructor-arg ref="configurationEncryptor" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="properties" ref="configproperties"/>
</bean>

<bean id="jasyptConfig" class="org.jasypt.encryption.pbe.config.SimpleStringPBEConfig">
    <property name="algorithm" value="PBEWithMD5AndTripleDES" />
    <property name="password" value="########" />
</bean>

<bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
    <property name="config" ref="jasyptConfig" />
</bean>
A: 

Wouldn't that be Spring Security? It can deal with LDAPs. And if you make it one security service that everyone uses, wouldn't that be the way to manage it?

duffymo
We have it set up in Spring Security. All of the LDAP servers are in an applicationContext file right now, and that's what I want to be able to pull in from an external source. If I have to maintain a list of ldap servers for each app, that would get horrendous.
I Never Finish Anythi
+1  A: 

How about:

  • Writing a method that returns a list of LDAP servers - reading from a database table or property files
  • expose this wethod via jndi and use it to inject a list of the servers into your spring config
  • If you need the ldap servers to be refreshed dynamically you could have a job poll for changes periodically or else have an admin webpage or jmx bean to trigger the update. Be careful of concurrency isses for both these methods (something reading the list while you are updating)
Pablojim
Thank you for your response! This sounds like an interesting solution, but I don't think I have enough experience with Spring to work out how to implement it. Could you give me an example involving the jndi exposure and spring injection? Would Spring handle the polling?
I Never Finish Anythi
This is essentially what I did, although different. I posted what I did in the update of the problem.
I Never Finish Anythi