问题描述
最近,我将Jasypt集成到了Spring Boot项目中,并且该项目在连接到DB,MQ和Kafka上仍然可以正常工作。不幸的是,我发现从那时起,在服务器启动期间无法进行回退(启动完成后仍然可以使用),并且故障分析器也退出了工作(用于DB,MQ和Kafka连接问题检测) 。 这是我在Gradle脚本中添加的内容:
implementation('com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.2')
这是主要班级:
@SpringBootApplication
public class DataRiverApplication extends SpringBootServletinitializer {
public static void main(String[] args) throws InterruptedException {
applicationContext = SpringApplication.run(DataRiverApplication.class,args);
LOGGER.info(INFO_APP_START);
for (String name : applicationContext.getBeanDeFinitionNames()) {
System.out.println(name);
LOGGER.info(name);
}
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DataRiverApplication.class);
}
}
请注意,在主类中,LOGGER.info
和System.out.println
在此代码更改之前曾经可以正常工作。
这是我的故障分析器的外观:
public class Mqfailureanalyzer extends Abstractfailureanalyzer<JMSException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure,JMSException cause) {
LOGGER_ERROR.error(ERROR_APPLICATION_START_FAILURE + "JMSException" + ExceptionUtils.getStackTrace(cause));
return new FailureAnalysis(getDescription(cause),getAction(cause),cause);
}
String getDescription(JMSException ex) {
return String.format(ex.getMessage());
}
String getAction(JMSException ex) {
return String.format("Check MQ conenction details in conf/mq.properties.");
}
}
已在src/main/resources/meta-inf/spring.factories
中注册,如下所示:
org.springframework.boot.diagnostics.failureanalyzer=start.Databasefailureanalyzer,start.Mqfailureanalyzer,start.Kafkafailureanalyzer
它再次正常工作,但是自Jasypt集成以来,它在启动失败时不再被调用。这是我的db.properties中的相关属性:
spring.datasource.password=ENC(51TnfL6ieqAZcx9/WAXbnJRfpAerLoyPoFsOmKKFNODCemFDYSQ4EJc6Cqdyd/05)
jasypt.encryptor.algorithm=PBEWITHHMACSHA512ANDAES_256
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.RandomIvGenerator
以下是kafka.properties中的相关属性:
kafka.security.protocol=SSL
kafka.ssl.truststore.location=kafka.jks
kafka.ssl.truststore.password=ENC(9m7qPYDi7YEKSw108WQwyvF0ruH7f1KBPLV3GL/Jlsi0XRxL+KHufCbCisdA9p+G)
kafka.ssl.keystore.location=kafka.jks
kafka.ssl.keystore.password=ENC(9m7qPYDi7YEKSw108WQwyvF0ruH7f1KBPLV3GL/Jlsi0XRxL+KHufCbCisdA9p+G)
kafka.ssl.key.password=ENC(9m7qPYDi7YEKSw108WQwyvF0ruH7f1KBPLV3GL/Jlsi0XRxL+KHufCbCisdA9p+G)
这是catalina.properties中的MQ属性:
javax.net.ssl.trustStorePassword.encrypted=5MGKqSfK57P9MuFOi1D2gtaSnp3JiaveVq3DZDAtcfenYqBjMMjdpeLDv7Xv/yO2
javax.net.ssl.keyStorePassword.encrypted=5MGKqSfK57P9MuFOi1D2gtaSnp3JiaveVq3DZDAtcfenYqBjMMjdpeLDv7Xv/yO2
MQ的解密是手动过程,不同于DB和Kafka:
@Configuration
@PropertySource({ "classpath:classpath" })
public class MqConfig {
static {
JasyptEncryptor.setPassword(System.getenv(ENV_VAR_JASYPT_ENCRYPTOR_PASSWORD));
System.setProperty(PROP_SSL_TRUSTSTORE_PASSWORD,JasyptEncryptor.decrypt(System.getProperty(PROP_SSL_TRUSTSTORE_PASSWORD_ENCRYPTED)));
System.setProperty(PROP_SSL_KEYSTORE_PASSWORD,JasyptEncryptor.decrypt(System.getProperty(PROP_SSL_KEYSTORE_PASSWORD_ENCRYPTED)));
}
就像我说的那样,这些事情只是默默地开始失败。我一点都没有例外。 有人可以告诉我一些想法吗? 非常感谢!
解决方法
我可以通过将静态代码移入私有方法来解决问题:
@Configuration
@PropertySource({ "classpath:mq.properties" })
public class MqConfig {
private boolean envSet = false;
private void setEnvVariables() {
if (!envSet) {
JasyptEncryptor.setPassword(System.getenv(ENV_VAR_JASYPT_ENCRYPTOR_PASSWORD));
System.setProperty(PROP_SSL_TRUSTSTORE_PASSWORD,JasyptEncryptor.decrypt(System.getProperty(PROP_SSL_TRUSTSTORE_PASSWORD_ENCRYPTED)));
System.setProperty(PROP_SSL_KEYSTORE_PASSWORD,JasyptEncryptor.decrypt(System.getProperty(PROP_SSL_KEYSTORE_PASSWORD_ENCRYPTED)));
envSet = true;
}
}
一旦我从Bean配置方法中调用此私有方法,问题就消失了。