Spring Boot Apache Web Server



Before you get started, it’s important to keep in mind that a Spring Boot app is just a web app that’s packaged with an embedded Apache Tomcat Application Server. That means the process of setting up Tomcat to run behind an Apache Web Server is very similar to what you’ll see here. Bottom line: there’s no need to over-complicate this. While the post heading says running a Spring boot web application behind Apache, this article covers most of all J2EE based web applications be it Spring, Struts, Seam or JSF framework. It also broadly applies to any Web Service or Web Application which are bound to a port. A web server is a network service that serves content to a client over the web. This typically means web pages, but any other documents can be served as well. Web servers are also known as HTTP servers, as they use the hypertext transport protocol (HTTP). The Apache HTTP Server, httpd, is an open source web server developed by the Apache Software Foundation. The server-side of Spring-WS is designed around a central class that dispatches incoming XML messages to endpoints. Spring-WS's MessageDispatcher is extremely flexible, allowing you to use any sort of class as an endpoint, as long as it can be configured in the Spring IoC container. In a way, the message dispatcher resembles Spring's DispatcherServlet, the “ Front Controller ” used in. @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.

Spring Boot Apache Web Server

Spring Boot can run as a standalone server, but putting it behind an Apache web server has several advantages, such as load balancing and cluster management. Now with LetsEncrypt it’s easier than ever (and free) to secure your site with SSL.

In this tutorial, we’ll secure an Apache server with SSL and forward requests to a Spring Boot app running on the same machine. (And once you’re done you can add Stormpath’s Spring Boot integration for robust, secure identity
management that sets up in minutes.)

Set Up Your Spring Boot Application

The most basic Spring Boot web application just shows a homepage. Using Maven, this has four files: pom.xml, Application.java, RequestController.java, and home.html.

The pom.xml file (in the root folder) declares four things: application details, starter parent, starter web dependency, and the Maven plugin (for convenience in running from the console).

2
4
6
8
publicclassRequestController{
@RequestMapping('/')
return'home.html';
}

Finally home.html (src/main/resources/static) is just declares a title and message.

2
4
git clonehttps://github.com/stormpath/apache-ssl-tutorial
mvn spring-boot:run

Reroute Apache

Now we tell Apache to pass all traffic to our application. We can use the proxy and proxy_ajp modules for that. But first, we need to enable them.

2
sudo a2enmod proxy_ajp

Now we need to update the virtual host on port 443 to use the connector we created. For me the relevant file was in /etc/apache2/sites-available/000-default-le-ssl.conf. Add the follow to the bottom of the <VirtualHost *.443> element.

2
server.tomcat.protocol_header=x-forwarded-proto

Apache will set these headers by default and then Tomcat (embedded in Spring Boot) will properly identify the incoming traffic as SSL.

Spring Boot Apache Web Servers

Add Authentication

Spring Boot Apache

Application security is intrinsic to what we do here at Stormpath. Our team of Java security experts have just released the 1.0 version of our Java SDK, and with it massive updates to our Spring and Spring Boot integrations. You can add authentication for secure user management in this or any Spring Boot application in just 15 minutes! Check out our Spring Boot Quickstart to learn how!