Custom Encryption/Decryption for Spring-Cloud-Config Server

跟風遠走 提交于 2019-12-23 05:19:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!