繁体   English   中英

在用于spring boot之前,我如何首先在application.properties中解密密码

[英]How do I KMS decrypt the password in the application.properties first before being used in spring boot

我第一次使用AWS和spring boot。

我在application.properties中设置了我的数据库凭据。

但是我仍然需要KMS解密密码。

我如何在spring boot框架中执行此操作?

首先,您应该在项目中包含“zalando / spring-cloud-config-aws-kms”依赖性,有关该项目的更多详细信息,请查看此链接: https://github.com/zalando/spring-cloud -config-aws-kms你应该注意版本的选择,例如,如果你使用Spring Cloud Greenwich + Spring Boot 2.1zalando依赖版本应该是4.1

现在假设你的spring-boot项目是一个maven项目,那么你应该有这样的东西:

        <dependency>
            <groupId>org.zalando</groupId>
            <artifactId>spring-cloud-config-aws-kms</artifactId>
            <version>4.1</version>
        </dependency>

其次,application.properties中的加密密码值应以{cipher}开头,例如(下面显示的密码不是有效密码):

DataBase.Password = {cipher}UmjDPAmJr78ypSphQycO9DAQECAHgC4i08YQPW

最后,因为你的项目classPath中有spring-cloud-config-aws-kms ,你只需要通过@Value注释在你的类中注入加密密码的值,例如:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Example {
    //reads the encrypted password, decrypts it 
    // and injects it in the field DataBasePassword 
    @Value("${DataBase.Password}")
    private String DataBasePassword;

    @RequestMapping("/")
    public String decryptPassword() {
        return DataBasePassword;
    }
    public static void main(String[] args) {
        SpringApplication.run(Example.class, args);
    }
}

启动此Spring-Boot应用程序,打开浏览器并键入URL http:// localhost:8080 /以查看结果。 这个答案来自这个项目https://github.com/kinow/spring-boot-aws-kms-configuration 希望会有所帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM