Allow configuration of symmetric key in server outside bootstrap

There's no reason the encrypotor needs to be in the bootstrap context
so we can still support symmetric key encryption via /encrypt
and /decrypt without requiring it to be configured there. With
this change we just look for key properties in the current context
and create an encryptor the same way the bootstrap listener does
in spring-cloud-commons.
This commit is contained in:
Dave Syer
2015-12-18 10:17:52 +00:00
parent f3acf50be2
commit 9a65a2f1a8
2 changed files with 10 additions and 1 deletions

View File

@@ -70,7 +70,7 @@ public class ConfigServerMvcConfiguration extends WebMvcConfigurerAdapter {
return controller;
}
private EnvironmentEncryptorEnvironmentRepository encrypted() {
private EnvironmentRepository encrypted() {
EnvironmentEncryptorEnvironmentRepository encrypted = new EnvironmentEncryptorEnvironmentRepository(
this.repository, this.environmentEncryptor);
encrypted.setOverrides(this.server.getOverrides());

View File

@@ -27,12 +27,14 @@ import org.springframework.cloud.config.server.encryption.CipherEnvironmentEncry
import org.springframework.cloud.config.server.encryption.EnvironmentEncryptor;
import org.springframework.cloud.config.server.encryption.KeyStoreTextEncryptorLocator;
import org.springframework.cloud.config.server.encryption.TextEncryptorLocator;
import org.springframework.cloud.context.encrypt.EncryptorFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
import org.springframework.security.rsa.crypto.RsaSecretEncryptor;
import org.springframework.util.StringUtils;
/**
* Auto configuration for text encryptors and environment encryptors (non-web stuff).
@@ -45,13 +47,20 @@ import org.springframework.security.rsa.crypto.RsaSecretEncryptor;
*
*/
@Configuration
@EnableConfigurationProperties(KeyProperties.class)
public class EncryptionAutoConfiguration {
@ConditionalOnMissingBean(TextEncryptor.class)
protected static class DefaultTextEncryptorConfiguration {
@Autowired
private KeyProperties key;
@Bean
public TextEncryptor nullTextEncryptor() {
if (StringUtils.hasText(this.key.getKey())) {
return new EncryptorFactory().create(this.key.getKey());
}
return Encryptors.noOpText();
}