1

springboot 2.0.4.RELEASEアプリのデータベース プロパティを使用して構成しようとしています。次のように新しい構成ファイルを追加しました。

@Configuration
@ConfigurationProperties(prefix="spring.datasource")
public class DatabaseConfig 
{
    private String userName;
    private String password;
    
    public String getUserName() {
        try {
            userName = Files.readAllLines(Paths.get("/secrets/username.txt")).get(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        try {
            password = Files.readAllLines(Paths.get("/secrets/password.txt")).get(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

これが私のapplication.properties

spring.datasource.url=jdbc:sqlserver://mno35978487001.cloud.xyz.com:14481
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

アプリケーションが実行されたときに、ファイルからユーザー名とパスワードを読み取ることができることを望んapplication.propertiesでいましたが、それは起こらないようです。私が欠けているものについて何か考えはありますか?これらの値を読み取り、動的に設定するより良い方法はありますか?

4

2 に答える 2

3

カスタム プロパティ ソース ロケータを追加できます。

https://source.coveo.com/2018/08/03/spring-boot-and-aws-parameter-store/

基本的に、あなたが必要です

  1. プロパティ ソースを作成する
    public class SecretStorePropertySource extends PropertySource<SecretStoreSource> {
    
        public SecretStorePropertySource(String name) {
            super(name);
        }
    
        @Override
        public Object getProperty(String name) {
            if (name.startsWith("secrets.")) {
                // converts property name into file name, 
                // e.g. secrets.username -> /secrets/username.txt
                return Files.readAllLines(Paths.get("/"+name.replace('.','/')+".txt")).get(0);           
            }
            return null;
        }
    }
  1. カスタム環境のポスト プロセッサを使用して起動時に登録する
    public class SecretStorePropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor {
    
        @Override
        public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
            environment.getPropertySources()
                    .addLast(new SecretStorePropertySource("customSecretPropertySource",
                            new SecretStorePropertySource()));
        }
    }
  1. カスタム環境のポスト プロセッサを使用するように SpringBoot に指示する

src/main/resources/META-INF/spring.factoriesファイルに追加

org.springframework.boot.env.EnvironmentPostProcessor=com.mycompany.myproduct.SecretStorePropertySourceEnvironmentPostProcessor
  1. secrets.プレフィックスを使用して、ソースを介してプロパティを渡します
spring.datasource.username=${secrets.username}
spring.datasource.password=${secrets.password}

ただし、ファイル経由で提供する場合は、このファイルの名前を に変更しapplication-secrets.yml、ターゲット サーバーconfigのフォルダーに配置します。このフォルダー自体は、アプリケーション jar がコピーされたフォルダーと同じフォルダーにあります。

secretsこれで、アクティブなプロファイル リストに追加することで、SpringBoot にそれをロードするように依頼できます。

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-profile-specific-properties

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-adding-active-profiles

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-set-active-spring-profiles


アドバイスしているわけではありませんが、コードを機能させることもできます

@Configuration
public class Secrets {
    @Bean("SecretUserName")
    public String getUserName() {
        try {
            return Files.readAllLines(Paths.get("/secrets/username.txt")).get(0);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    @Bean("SecretUserPassword")
    public String getPassword() {
        try {
            return Files.readAllLines(Paths.get("/secrets/password.txt")).get(0);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}

に次の行を追加しますapplication.yml

spring.datasource.username=#{@SecretUserName}
spring.datasource.password=#{@SecretUserPassword}
于 2020-08-28T23:13:35.110 に答える
3

txt ファイルを読み取る代わりに、環境変数を作成し、application.properties でプレースホルダーを使用してそれらを参照できます。

spring.datasource.username: ${USERNAME}
spring.datasource.password: ${PASSWORD}

プレースホルダーの詳細については、こちらをご覧ください。

于 2020-08-28T23:03:24.683 に答える