Jasyptを使ったSpring Bootの設定

Jasyptを使用したスプリングブート構成

1. 前書き

ブートアプリケーションのJasypt (Java Simplified Encryption) Spring Boot provides utilities for encrypting property sources

この記事では、jasypt-spring-bootのサポートを追加して使用する方法について説明します。

暗号化のフレームワークとしてJasyptを使用する方法の詳細については、Jasypthereの概要を参照してください。

2. なぜジャシプト?

機密情報を構成ファイルに保存する必要があるときはいつでも、つまり、本質的にその情報を脆弱にしているということです。これには、資格情報など、あらゆる種類の機密情報が含まれますが、それだけではありません。

using Jasypt, we can provide encryption for the property file attributesによって、アプリケーションはそれを復号化し、元の値を取得するジョブを実行します。

3. SpringBootでJASYPTを使用する方法

SpringBootでJasyptを使用するさまざまな方法について説明しましょう。

3.1. Using jasypt-spring-boot-starter

プロジェクトに単一の依存関係を追加する必要があります。


    com.github.ulisesbocchio
    jasypt-spring-boot-starter
    2.0.0

Maven Centralには最新バージョンのjasypt-spring-boot-starterがあります。

次に、テキスト[email protected]”を秘密鍵「password」で暗号化してencrypted.properties:に追加しましょう。

encrypted.property=ENC(uTSqb9grs1+vUv3iN8lItC0kl65lMG+8)

そして、構成クラスAppConfigForJasyptStarterを定義しましょう–encrypted.propertiesファイルをPropertySourceとして指定します:

@Configuration
@PropertySource("encrypted.properties")
public class AppConfigForJasyptStarter {
}

次に、サービスBeanPropertyServiceForJasyptStarterを記述して、encrypted.propertiesから値を取得します。 The decrypted value can be retrieved using the @Value annotation or the getProperty() method of Environment class:

@Service
public class PropertyServiceForJasyptStarter {

    @Value("${encrypted.property}")
    private String property;

    public String getProperty() {
        return property;
    }

    public String getPasswordUsingEnvironment(Environment environment) {
        return environment.getProperty("encrypted.property");
    }
}

最後に、using the above service class and setting the secret key which we used for encryption, we can easily retrieve the decrypted password and use in our application

@Test
public void whenDecryptedPasswordNeeded_GetFromService() {
    System.setProperty("jasypt.encryptor.password", "password");
    PropertyServiceForJasyptStarter service = appCtx
      .getBean(PropertyServiceForJasyptStarter.class);

    assertEquals("[email protected]", service.getProperty());

    Environment environment = appCtx.getBean(Environment.class);

    assertEquals(
      "[email protected]",
      service.getPasswordUsingEnvironment(environment));
}

3.2. jasypt-spring-bootの使用

@SpringBootApplicationまたは@EnableAutoConfigurationを使用しないプロジェクトの場合、jasypt-spring-boot依存関係を直接使用できます。


    com.github.ulisesbocchio
    jasypt-spring-boot
    2.0.0

同様に、テキスト[email protected]”を秘密鍵“password”で暗号化し、それをencryptedv2.propertiesに追加しましょう。

encryptedv2.property=ENC(dQWokHUXXFe+OqXRZYWu22BpXoRZ0Drt)

そして、jasypt-spring-boot依存関係の新しい構成クラスを作成しましょう。

ここで、注釈@EncryptablePropertySourceを追加する必要があります:

@Configuration
@EncryptablePropertySource("encryptedv2.properties")
public class AppConfigForJasyptSimple {
}

また、encryptedv2.propertiesを返す新しいPropertyServiceForJasyptSimpleBeanが定義されています。

@Service
public class PropertyServiceForJasyptSimple {

    @Value("${encryptedv2.property}")
    private String property;

    public String getProperty() {
        return property;
    }
}

最後に、上記のサービスクラスを使用し、暗号化に使用した秘密鍵を設定すると、encryptedv2.property:を簡単に取得できます。

@Test
public void whenDecryptedPasswordNeeded_GetFromService() {
    System.setProperty("jasypt.encryptor.password", "password");
    PropertyServiceForJasyptSimple service = appCtx
      .getBean(PropertyServiceForJasyptSimple.class);

    assertEquals("[email protected]", service.getProperty());
}

3.3. カスタムJASYPTEncryptorの使用

セクション3.1で定義されている暗号化プログラム。 および3.2。 デフォルトの構成値で構築されます。

However, let’s go and define our own Jasypt encryptorを使用して、アプリケーションに使用してみてください。

S0、カスタム暗号化Beanは次のようになります。

@Bean(name = "encryptorBean")
public StringEncryptor stringEncryptor() {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    config.setPassword("password");
    config.setAlgorithm("PBEWithMD5AndDES");
    config.setKeyObtentionIterations("1000");
    config.setPoolSize("1");
    config.setProviderName("SunJCE");
    config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    config.setStringOutputType("base64");
    encryptor.setConfig(config);
    return encryptor;
}

さらに、SimpleStringPBEConfig.のすべてのプロパティを変更できます

また、we need to add a property “jasypt.encryptor.bean” to our application.properties, so that Spring Boot knows which Custom Encryptor it should use

たとえば、秘密鍵“password”で暗号化されたカスタムテキスト[email protected]”application.properties:に追加します。

jasypt.encryptor.bean=encryptorBean
encryptedv3.property=ENC(askygdq8PHapYFnlX6WsTwZZOxWInq+i)

設定すると、SpringのEnvironmentからencryptedv3.propertyを簡単に取得できます。

@Test
public void whenConfiguredExcryptorUsed_ReturnCustomEncryptor() {
    Environment environment = appCtx.getBean(Environment.class);

    assertEquals(
      "[email protected]",
      environment.getProperty("encryptedv3.property"));
}

4. 結論

By using Jasypt we canprovide additional security for the data that application handles

これにより、アプリケーションのコアに集中できるようになり、必要に応じてカスタム暗号化を提供することもできます。

いつものように、この例の完全なコードはover on Githubで利用できます。