0

Well, I'm trying to deploy a spring boot application in a tomcat 10 server, passing a one environment key. I need to pass de "secret" of jasypt for decode the passwords in my application, but I can't do this because the context don't run the same as the spring boot application normally.

In my App.java with main loos like

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");
        }
    }

}

This code works with an application runs in a normally deploy with Spring Boot, with the

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

The code commented is that I tried with tomcat but don't work, the application starts even before that this code, I can't see any log, even this log was in the method configure. But in the tomcat 10, this approach don't work. I need to pass this secret like a property or with the environment. How can I do?

abeld89
  • 57
  • 4
  • 11
  • `encriptor`? You sure that is correct? Also you can use `/bin/tomcat10w.exe` to set java options – XtremeBaumer Sep 06 '22 at 10:09
  • Yes, I need to set jasypt.encryptor.password before the app is UP because If this property is not set, the app throws an exception and don't start up. I tried to set this property in the /bin/setenv.bat like: set "JAVA_OPTS=%JAVA_OPTS% -Djasypt.encryptor.password="secret" and don't works. – abeld89 Sep 06 '22 at 10:24
  • Try removing the quotes from the password value; this might have unexpected effects on the parsing of the environment variable. – digitalbreed Sep 06 '22 at 10:33
  • No, it doesn't matter. I tried to set with doble quote or simple quote or nothing, all time I get the same result. The main problem is that I can't arrive to Override method configure, the application fails before, because I added a log.debug before the line return application.sources(App.class); and don't see anything. – abeld89 Sep 06 '22 at 13:02

1 Answers1

0

The solution in the end was to create the file setenv.bat in ${TOMCAT_HOME}/bin/setenv.bat

Following the approach: How to pass the -D additional parameter while starting tomcat?

The content of setenv.bat:

set CATALINA_OPTS=%CATALINA_OPTS -Djasypt.encryptor.password="secret"
abeld89
  • 57
  • 4
  • 11