问题
I am using spring-cloud-config server and trying to use the encrypt/decrypt feature. Is there a way we can customize the encrypt/decrypt feature i.e. we do have our own encryption standards and want to leverage those libraries.
Appreciate any help in advance.
回答1:
If you want to customize the encryption/decryption, essentially you need customize the org.springframework.security.crypto.encrypt.TextEncryptor
bean by yourself.
Optimisticly, remove all the entrypt related configuration from your application.properties/application.yml, also you need make sure the JCE is not in your classpath, the main propose is disable the spring default encryption auto-configuration, then register your own TextEncryptor
bean.
Here I add a very simple sample, so you can implement the MyTextEncryptor
with your existing library.
Sample
@SpringBootApplication
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Component
static class MyTextEncryptor implements TextEncryptor {
@Override
public String encrypt(String text) {
return "encrypt\n";
}
@Override
public String decrypt(String encryptedText) {
return "decrypt\n";
}
}
}
Result
Reference:
org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration
org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer
org.springframework.cloud.config.server.encryption.EncryptionController
来源:https://stackoverflow.com/questions/39952528/custom-encryption-decryption-for-spring-cloud-config-server