mossaab
mossaab

Reputation: 1824

How to set equivalent of web.xml JNDI <env-entry> in a Spring Boot project?

Referring to this SO answer, I'd like to setup the equivalent of this web.xml configuration in a JSF / JoinFaces / SpringBoot application (that doesn't have web.xml).

<env-entry>
    <env-entry-name>jsf/ClientSideSecretKey</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>[AES key in Base64 format]</env-entry-value>
</env-entry>

Any pointers?

Upvotes: 0

Views: 1606

Answers (2)

vsoni
vsoni

Reputation: 2858

If you are using spring boot and embedded tomcat server, you can add <env-entry> programmatically with the following configuration.

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
    
    
    @Bean
    public TomcatServletWebServerFactory tomcatFactory() {
        return new TomcatServletWebServerFactory() {
            @Override
            protected TomcatWebServer getTomcatWebServer(org.apache.catalina.startup.Tomcat tomcat) {
                tomcat.enableNaming(); 
                return super.getTomcatWebServer(tomcat);
            }

            @Override 
            protected void postProcessContext(Context context) {

                // adding <resource-ref>
                ContextResource resource = new ContextResource();
                resource.setName("jdbc/myJndiResource");
                resource.setType(DataSource.class.getName());
                resource.setProperty("driverClassName", "org.postgresql.Driver");

                resource.setProperty("url", "jdbc:postgresql://hostname:port/dbname");
                resource.setProperty("username", "username");
                resource.setProperty("password", "password");
                context.getNamingResources()
                       .addResource(resource); 
                
                // adding <env-entry>
                ContextEnvironment ce = new ContextEnvironment();
                ce.setName("jsf/ClientSideSecretKey");
                ce.setType(String.class.getName());
                ce.setValue("[AES key in Base64 format]");
                context.getNamingResources().addEnvironment(ce); 
                
            }
        };
    }

    public static void main(String[] args) throws NamingException {
        SpringApplication.run(DemoApplication.class, args);
    }

}

Once defined the jndi naming resources they can be accessed in your application using JndiTemplate of InitialContext.

JndiTemplate jndiTemplate = new JndiTemplate();
String str = (String) jndiTemplate.lookup("java:comp/env/jsf/ClientSideSecretKey");

Hope this helps you in resolving your problem.

Upvotes: 2

jccampanero
jccampanero

Reputation: 53411

Essentially <env-entry> declares a web application context attribute.

You can initialize your servlet context and provide the equivalent servlet context attributes in your Spring Boot application.

For that purpose, you can register a bean that implements the ServletContextInitializer interface (or WebApplicationInitializer if your app has to be deployed in a traditional servlet container). For example:

public class JsfServletContextInitializer implements ServletContextInitializer {

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.setAttribute("jsf/ClientSideSecretKey", "[AES key in Base64 format]");
  }

}

Do not forget to register it as a bean in your configuration.

Upvotes: -1

Related Questions