Dropwizard环境变量

huangapple go评论87阅读模式
英文:

Dropwizard Environement variables

问题

我在寻找有关Dropwizard环境配置的好资源时遇到了问题,Dropwizard的手册对我帮助不大。

我尝试将我的jwtSecret保存在配置文件config.yml中,作为环境变量,以便即使我将代码开源,它也能保持机密性,如下所示:

jwtSecret: ${JWT_SECRET}

我阅读了手册,我知道我需要添加SubstitutingSourceProvider来成功地用我的环境变量替换配置。然而,我找不到保存环境变量的位置。Dropwizard是否有特定的位置来查找它,还是我需要在某个地方添加环境变量的路径?

英文:

I am having problems finding good resources for the dropwizard environment configuration, the manuals from dropwizard are not really helpful for me.

I am trying to save my jwtSecret in my configuration config.yml as a environment variable so it stays a secret even if I make my code open source as

jwtSecret: ${JWT_SECRET}

I have read the manuals and I know I need to add SubstitutingSourceProvider to successfully substitute the config with my environment variables. However I do not find where to save my environment variables. Is there a specific place where dropwizard finds it or do I need to add the path to the environment variables somewhere?

答案1

得分: 2

我并不完全确定您需要帮助的部分,但是添加环境变量涉及多个步骤。

首先,我们通过将其添加到应用程序的初始化函数中来启用此功能:

bootstrap.setConfigurationSourceProvider(new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false)));

然后是 config.yml 文件:

jwtSecret: ${JWT_SECRET}

要在您的应用程序中访问该变量,您需要将以下内容添加到应用程序的配置类中:

private String jwtSecret;

// 还需要为其添加 getter 和 setter 方法

最后,您需要将 JWT_SECRET 变量添加到您的系统环境变量中。这取决于您使用的操作系统以及您是想临时设置还是永久设置。

英文:

I'm not entirely sure which part you need help with, but adding environmental variables touches many points.

First we enable this feature by adding it to the Application's initialize function:

bootstrap.setConfigurationSourceProvider(new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(), new EnvironmentVariableSubstitutor(false)));

Then there is the config.yml:

jwtSecret: ${JWT_SECRET}

To access the variable in your application, you need to add this to the Application's Configuration class:

private String jwtSecret;

(+ getter and setter for it)

And lastly you need to add the JWT_SECRET variable to your system environmental variables. This varies based on which operating system you are using and whether you want to set it temporarily or permanently.

huangapple
  • 本文由 发表于 2020年10月27日 18:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64552844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定