Results 1 to 6 of 6

Thread: Converting bean types in a beans definition file

  1. #1
    Join Date
    Aug 2004
    Location
    Vermont
    Posts
    27

    Default Converting bean types in a beans definition file

    I am setting up some datasources in my beans definition file. This all works fine. My problem is I do not want the passwords to my databases showing up as clear text in the definition file.

    To get around this I thought I could use some existing encryption routines I have to unencrypt the password from the beans definition file. So to do this I made a java bean called Password that has a property called encryptedPassword. The setter method takes a string and uses my existing decryption routines to store the password internally as clear text. I then set up the Password bean in my bean definitions file and I set the property of the password on the datasource to the Password bean. The problem is the datasource expects the password to be of type String and the Password object is of type Password. Since String is final I can't subclass it.

    So.... How do I go about doing this? Can I use a PropertyEditor some how? If so are there any examples of how to do this? Better yet is there some best practice that I should be following for setting passwords in configuration files? It seems like I must be missing something trivial.

    Thanks
    David Noel

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    One option would be for you to use PropertyPlaceholderConfigurer to bring in the pass from an external properties files. Then the password would not be stored in the main config file in CVS, but would rather be written to this separate properties file by whoever is deploying the app.

    Alternately, if it is convenient for you to bind objects to JNDI, you could grab it from the JNDI tree with JndiObjectFactoryBean.

    As for the PropertyEditor approach, yes, it would work. Using PropertyEditors with Spring is described here:

    http://www.springframework.org/docs/...-customeditors
    http://www.springframework.org/docs/...alidation.html

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  3. #3
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    You can create a BeanFactory PasswordResolver that implements org.springframework.aop.framework.ProxyFactoryBean and use it as follows:

    1.PasswordResolver:
    Code:
    import org.springframework.beans.factory.FactoryBean;
    
    public class PasswordResolver implements FactoryBean
    {
    	private String cryptedPassword;
    
    	public void setCryptedPassword (String cryptedPassword) { this.cryptedPassword = cryptedPassword; }
    
    	//implement your algorythm here
    	public Object getObject () throws Exception {
    		return cryptedPassword;
    	}
    
    	public Class getObjectType () {
    		return String.class;
    	}
    
    	public boolean isSingleton () {
    		return true;
    	}
    }
    1.applicationContext.xml:
    Code:
      ...
      <bean id="passwordResolver" class="PasswordResolver">
        <property name="name"><value>Taha Irbouh</value></property>
      </bean>
      ...
        <property name="password">
          <ref local="passwordResolver"/>
        </property>
      ...
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    You can also use a MethodInvokingFactoryBean to call a method on a bean that will decrypt your password.
    Setting a bean property as the result of a method invocation
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5
    Join Date
    Aug 2004
    Location
    Vermont
    Posts
    27

    Default

    Gee. I guess I need to put my glasses on before reading the docs. I don't know how I missed those sections.

    I'm all set now. Thanks guys
    David Noel

  6. #6
    Join Date
    Sep 2004
    Posts
    17

    Default Encryptors/Decryptors

    An even more flexible approach is something like the following:

    1. Create a general interface for cryption, which can be either
    direction
    Code:
         public interface Cryptor &#123;
            String crypt&#40;String value&#41;;
         &#125;
    2. Create a resolver for a value, which takes any Cryptor implementation
    Code:
          public class CryptorResolver implements FactoryBean &#123; 
             private String value; 
             private Cryptor cryptor;
    
             public void setValue&#40;String value&#41; &#123;
                  this.value = value;
             &#125; 
       
             public void setCryptor&#40;Cryptor cryptor&#41; &#123;
                  this.cryptor = cryptor;
             &#125;
    
             public Object getObject &#40;&#41; throws Exception &#123; 
                  return cryptor.crypt&#40;value&#41;; 
             &#125; 
    
             public Class getObjectType &#40;&#41; &#123; 
                  return String.class; 
             &#125; 
    
             public boolean isSingleton &#40;&#41; &#123; 
                  return false; 
             &#125; 
         &#125;
    3. Create encryptor and decryptor implementations

    Code:
    public class EncryptorImpl implements Cryptor &#123; ... &#125;
     
             public class DecryptorImpl implements Cryptor &#123; ... &#125;
    4. In your beans.xml, define system wide encryptor and decryptor

    Code:
    <bean id="encryptor" class="EncryptorImpl" isSingleton="true"/>
     
             <bean id="decryptor" class="DecryptorImpl" isSingleton="true" />
    5. When you want to decrypt, create an instance of the resolver

    Code:
    <bean id="passwordDecrypter" class="EncryptionResolver"> 
               <property name="cryptor"><ref bean="decryptor" /></property>
               <property name="value"><value>$&#123;epwd&#125;</value></property> 
             </bean>
    This approach is much more generic, and allows your application internally to encrypt decrypt items. This is helpful if you store any passwords in the database, such as for external datasources which
    are not part of the static configuration, and so forth.

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •