Jasyptを使ったSpring Bootの設定
1前書き
-
Jasypt(Java Simplified Encryption)Spring BootはBootアプリケーションでプロパティソースを暗号化するためのユーティリティを提供します。
この記事では、https://github.com/ulisesbocchio/jasypt-spring-boot[jasypt-spring-boot]のサポートを追加して使用する方法について説明します。
Jasyptを暗号化のフレームワークとして使用する方法の詳細については、Jasyptの紹介リンクを参照してください。
2なぜJasyptですか?
機密情報を設定ファイルに保存する必要があるときはいつでも – それは本質的にその情報を脆弱にするということです。これには、資格情報などのあらゆる種類の機密情報が含まれますが、それ以上のものであることは確かです。
-
Jasyptを使うことで、プロパティファイルの属性に暗号化を提供することができます** そして私たちのアプリケーションはそれを復号化して元の値を取得する仕事をします。
3 Spring Boot
でJASYPTを使う方法
Spring BootでJasyptを使用するさまざまな方法について説明しましょう。
3.1.
jasypt
– spring-boot-starter
を使う
プロジェクトに単一の依存関係を追加する必要があります。
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
Maven Centralにはhttps://search.maven.org/classic/#search%7C1%7Cg%3A%22com.github.ulisesbocchio%22%20AND%20a%3A%22jasypt-spring-bootの最新バージョンがあります-starter%22[jasypt-spring-boot-starter]
それでは、「
[email protected
]」
というテキストを秘密鍵「password」で暗号化し、それを
encrypted.properties:
に追加しましょう
encrypted.property=ENC(uTSqb9grs1+vUv3iN8lItC0kl65lMG+8)
そして設定クラス
AppConfigForJasyptStarter
を定義しましょう。
@Configuration
@PropertySource("encrypted.properties")
public class AppConfigForJasyptStarter {
}
それでは、
encrypted.properties
から値を取得するためのサービスBean
PropertyServiceForJasyptStarter
を作成します。復号化された値は、
@ Value
アノテーションまたは
Environment
クラスの__getProperty()メソッドを使用して取得できます。
@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");
}
}
最後に、** 上記のサービスクラスを使用し、暗号化に使用した秘密鍵を設定すると、復号化したパスワードを簡単に取得してアプリケーションで使用できます。
@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
を使用していないプロジェクトの場合は、https://search.maven.org/classic/#search%7Cgav%7C1%7Cg%3A%22com.github.ulisesbocchio%22%20AND%を使用できます20a%3A%22jasypt-spring-boot%22[jasypt
_- spring-boot
_
]依存関係:直接**
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>
同様に、テキスト「“
[email protected
]”」
を秘密鍵
“ password”
で暗号化し、それを
encryptedv2.properties__に追加します。
encryptedv2.property=ENC(dQWokHUXXFe+OqXRZYWu22BpXoRZ0Drt)
そして、jasypt-spring-boot依存関係のための新しい設定クラスを作りましょう。
ここで、アノテーション
@ EncryptablePropertySource
を追加する必要があります。
@Configuration
@EncryptablePropertySource("encryptedv2.properties")
public class AppConfigForJasyptSimple {
}
また、
encryptedv2.properties
を返すための新しい
PropertyServiceForJasyptSimple
Beanが定義されています。
@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. カスタムJASYPT暗号化機能の使用
セクション3.1で定義された暗号化器。 3.2。デフォルトの設定値で構築されます。
-
しかし、私たち自身のJasypt暗号化装置を定義して** 使用してみましょう。
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.
のすべてのプロパティを変更できます。
また、Spring BootがどのCustom Encryptorを使うべきかを認識できるように、
application.propertiesに
jasypt.encryptor.beanというプロパティを追加する必要があります。
たとえば、
application.properties:
に、秘密鍵で暗号化されたカスタムテキスト
“
[email protected
]”
を追加します。
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結論
-
Jasyptを使用することで、アプリケーションが処理するデータに追加のセキュリティを提供することができます。
それは私達が私達のアプリケーションのコアにもっと集中することを可能にし、また必要ならカスタム暗号化を提供するために使用することができます。
いつものように、この例の完全なコードはhttps://github.com/eugenp/tutorials/tree/master/spring-boot-jasypt[over on Github]から入手できます。