Tomcat10将Environment Variable或Property传递到spring引导上下文



好吧,我正试图在tomcat 10服务器中部署一个spring-boot应用程序,传递一个环境密钥。我需要通过;秘密";的jasypt来解码我的应用程序中的密码,但我不能这样做,因为上下文的运行方式与spring引导应用程序的正常运行方式不同。

在我的App.java中,主卫生间像

public class App extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(App.class);
}

public static void main(String[] args) throws Exception {
setProp();
final SpringApplication application = new SpringApplication(AppBatch.class);
application.run(args);
}
private static void setProp() throws Exception {
// Context ctx = new InitialContext();
// Context envCtx = (Context) ctx.lookup("java:comp/env");
// String propertyKey = (String) envCtx.lookup("jasypt.encriptor.password");
String propertyKey = System.getProperty("jasypt.encriptor.password");
Properties props = new Properties(System.getProperties());
if (propertyKey != null && !propertyKey.isEmpty()) {
props.setProperty("jasypt.encryptor.password", propertyKey);
System.setProperties(props);
} else {
throw new Exception("Not setted property in jasypt password");
}
}
}

此代码适用于在正常部署的Spring Boot中运行的应用程序,使用

java -jar -Djasypt.encryptor.password="secret" app.jar ...

评论的代码是,我尝试了tomcat,但没有工作,应用程序甚至在这之前就启动了,我看不到任何日志,甚至这个日志在方法配置中。但在tomcat 10中,这种方法不起作用。我需要像传递财产或与环境一样传递这个秘密。我该怎么办?

最终的解决方案是在${TOMCAT_HOME}/bin/setenv.bat 中创建文件setenv.ba

遵循以下方法:如何在启动tomcat时传递-D附加参数?

setenv.bat:的内容

set CATALINA_OPTS=%CATALINA_OPTS -Djasypt.encryptor.password="secret"

最新更新