1. 引言
Jasypt (Java Simplified Encryption) Spring Boot 提供用於加密 Boot 應用程序 property 來源的實用工具
在本文中,我們將討論如何添加 jasypt-spring-boot 的支持以及如何使用它。
有關如何使用 Jasypt 作為加密框架的更多信息,請查看我們的 Jasypt 簡介 此處。
2. 為什麼選擇Jasypt?
在配置文件中存儲敏感信息時,實際上就使這些信息面臨風險;這包括憑據等各種敏感信息,當然也遠不止這些。
通過使用Jasypt,我們可以為屬性文件提供加密功能, 並且我們的應用程序將負責解密並檢索原始值。
3. 使用 Jasypt 與 Spring Boot 的不同方法
讓我們討論一下使用 Jasypt 與 Spring Boot 的不同方法。
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 提供最新版本的 jasypt-spring-boot-starter。
現在,讓我們使用密鑰“password”對文本 “Password@1” 進行加密,並將其添加到 encrypted.properties 中。
encrypted.property=ENC(uTSqb9grs1+vUv3iN8lItC0kl65lMG+8)讓我們定義一個配置類 AppConfigForJasyptStarter – 將 encrypted.properties 文件指定為 PropertySource:
@Configuration
@PropertySource("encrypted.properties")
public class AppConfigForJasyptStarter {
}
現在,我們將編寫一個服務 Bean PropertyServiceForJasyptStarter,以從 encrypted.properties 中檢索值。 可以使用 @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("Password@1", service.getProperty());
Environment environment = appCtx.getBean(Environment.class);
assertEquals(
"Password@1",
service.getPasswordUsingEnvironment(environment));
}
3.2. 使用 jasypt-spring-boot
對於不使用 @SpringBootApplication 或 @EnableAutoConfiguration 的項目,我們可以直接使用 jasypt-spring-boot 依賴:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>同樣,讓我們使用密鑰 “password” 對文本 “Password@2” 進行加密,並將其添加到 encryptedv2.properties 文件中:
encryptedv2.property=ENC(dQWokHUXXFe+OqXRZYWu22BpXoRZ0Drt)讓我們為 jasypt-spring-boot 依賴項創建一個新的配置類。
在這裏,我們需要添加註解 >:
@Configuration
@EncryptablePropertySource("encryptedv2.properties")
public class AppConfigForJasyptSimple {
}此外,已定義一個新的 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("Password@2", service.getProperty());
}
3.3. 使用自定義 Jasypt 加密器
本節中定義的加密器使用默認配置值構建。
但是,讓我們定義一個自定義的 Jasypt 加密器,並嘗試將其用於我們的應用程序。
因此,自定義加密器 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 的屬性。
另外,我們需要在 application.properties 中添加屬性 “jasypt.encryptor.bean”,以便 Spring Boot 知道應該使用哪個自定義加密器
例如,我們在 application.properties 中添加自定義文本 “Password@3”,使用密鑰 “password” 加密。
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(
"Password@3",
environment.getProperty("encryptedv3.property"));
}
4. 結論
通過使用 Jasypt,我們可以為應用程序處理的數據提供額外的安全保障。
它使我們能夠更專注於應用程序的核心,並且還可以用於提供自定義加密,如果需要的話。