>Collegues,您能否帮助使用 jasypt 从属性文件中解密密码(它是stmCredentials
映射中的一个值(。
我的属性文件中有下一个字符串:
creds.users={testLogin: 'ENC(w0H***pgsj)'}
user2.login = ENC(9j3fHz5c****cLRCVvLTQmr5)
user2.pass = ENC(w0HxpKq7V3Lf***g3zs/hpgsj)
我在调试模式下运行测试:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("dev")
@Slf4j
public class CredentialsTest {
@BeforeClass
public static void beforeClass(){
System.setProperty("jasypt.encryptor.password", "C*******L");
}
@Autowired
StmCredentials stmCredentials;
@Value("${user2.login}")
private String user2Login;
@Value("${user2.pass}")
private String user2Pass;
@Test
public void getCredspairs() {
HashMap<String, String> credspairs = stmCredentials.getCredspairs();
}
}
运行后,我在变量中有下一个值:
credspairs:
key: "testLogin"
value:"ENC(w0HxpKq7V3LfEPsU5mbd0Vg3zs/hpgsj)" //it wasn't decrypt =(
和(注意!
user2Login = testLogin //it was decrypt
user2Pass = K1212Zrde
我的属性文件中似乎有问题,在creds.users
属性中。我尝试使用"单引号,双引号",但没有帮助。
StmCredentials
豆看起来像:
@Component
@EnableConfigurationProperties
public class StmCredentials {
@Value("#{${creds.users}}")
private HashMap<String, String> credspairs;
public HashMap<String, String> getCredspairs() {
return credspairs;
}
public void setCredspairs(HashMap<String, String> somedata) {
this.credspairs = somedata;
}
}
如何解密存储在StmCredentials
(值(中的密码? 谢谢你的任何建议。
希望这有帮助:
我不认为 Jasypt 可以以这种方式检测和解密属性文件中的值(从我有限的知识范围来看(。如果可以的话,您可以尝试将其放入application.yml
文件中。它应该在那里工作。无论如何,我们可以执行以下操作:
这实际上不是解决方案,而是一种可能的解决方法。我们可以创建一个类来自己解密值,如果 Jasypt 自动不会为我们做。
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
public class MyStringEncryptor {
private StringEncryptor encryptor;
public MyStringEncryptor(String password) {
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);
this.encryptor = encryptor;
}
public String encrypt(String message) {
return encryptor.encrypt(message);
}
public String decrypt(String message) {
return encryptor.decrypt(message);
}
}
现在我们可以创建一个类MyStringEncryptor
对象,并使用方法decrypt
来解密我们想要的值。
MyStringEncryptor encryptor = new MyStringEncryptor("mysecretpass"); // You can pass the password from properties file using @Value
String decryptedValue = encryptor.decerypt(encrypted-message);