All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ulisesbocchio.jasyptspringboot.StringEncryptorConfiguration Maven / Gradle / Ivy

There is a newer version: 3.0.5
Show newest version
package com.ulisesbocchio.jasyptspringboot;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.function.Supplier;

/**
 * @author Ulises Bocchio
 */
@Configuration
public class StringEncryptorConfiguration {

    private static final Logger LOG = LoggerFactory.getLogger(StringEncryptorConfiguration.class);

    @Bean
    @ConditionalOnMissingBean(StringEncryptor.class)
    public StringEncryptor stringEncryptor(Environment environment) {
        return new LazyStringEncryptor(() -> {
          PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
          SimpleStringPBEConfig config = new SimpleStringPBEConfig();
          config.setPassword(getRequiredProperty(environment, "jasypt.encryptor.password"));
          config.setAlgorithm(getProperty(environment, "jasypt.encryptor.algorithm", "PBEWithMD5AndDES"));
          config.setKeyObtentionIterations(getProperty(environment, "jasypt.encryptor.keyObtentionIterations", "1000"));
          config.setPoolSize(getProperty(environment, "jasypt.encryptor.poolSize", "1"));
          config.setProviderName(getProperty(environment, "jasypt.encryptor.providerName", "SunJCE"));
          config.setSaltGeneratorClassName(getProperty(environment, "jasypt.encryptor.saltGeneratorClassname", "org.jasypt.salt.RandomSaltGenerator"));
          config.setStringOutputType(getProperty(environment, "jasypt.encryptor.stringOutputType", "base64"));
          encryptor.setConfig(config);
          return encryptor;
        });
    }

    private String getProperty(Environment environment, String key, String defaultValue) {
        if (!propertyExists(environment, key)) {
            LOG.info("Encryptor config not found for property {}, using default value: {}", key, defaultValue);
        }
        return environment.getProperty(key, defaultValue);
    }

    private boolean propertyExists(Environment environment, String key) {
        return environment.getProperty(key) != null;
    }

    private String getRequiredProperty(Environment environment, String key) {
        if (!propertyExists(environment, key)) {
            throw new IllegalStateException(String.format("Required Encryption configuration property missing: %s", key));
        }
        return environment.getProperty(key);
    }

    private static final class LazyStringEncryptor implements StringEncryptor {
        private final Supplier supplier;

        private LazyStringEncryptor(final Supplier encryptorFactory) {
          supplier = new SingletonSupplier<>(encryptorFactory);
        }

        @Override
        public String encrypt(String message) {
          return supplier.get().encrypt(message);
        }

        @Override
        public String decrypt(String encryptedMessage) {
          return supplier.get().decrypt(encryptedMessage);
        }
    }

    private static final class SingletonSupplier implements Supplier {

      private Supplier singleton;
      private T value;

      private SingletonSupplier(final Supplier original) {
        this.singleton = () -> {
          synchronized (original) {
            if(value == null) {
              value = original.get();
              singleton = () -> value;
            }
            return value;
          }
        };
      }

      @Override
      public T get() {
        return singleton.get();
      }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy