Skip to content

Instantly share code, notes, and snippets.

@andreldm
Created February 1, 2017 12:24
Show Gist options
  • Save andreldm/7f89a3279438467a0bd41e6c1249d014 to your computer and use it in GitHub Desktop.
Save andreldm/7f89a3279438467a0bd41e6c1249d014 to your computer and use it in GitHub Desktop.
APR on Spring Boot
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.core.AprLifecycleListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* NOTE: You also need to install APR on your system, on Arch Linux the package is called `tomcat-native`.
*/
@Configuration
public class AprConfiguration {
@Value("${server.tomcat.apr.enabled:false}")
private boolean enabled;
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
if (enabled) {
LifecycleListener arpLifecycle = new AprLifecycleListener();
container.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
container.addContextLifecycleListeners(arpLifecycle);
}
return container;
}
}
@metrue
Copy link

metrue commented Apr 23, 2021

Got something wrong with SSL.

lization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: To use SSL, the connector's protocol handler must be an AbstractHttp11JsseProtocol subclass
2021-04-23 08:57:05.294 |             main |  INFO | ------------------------------------ | o.s.b.a.l.ConditionEvaluationReportLoggingListener:136 |

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-04-23 08:57:05.336 |             main | ERROR | ------------------------------------ | o.s.b.SpringApplication:856 | Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: To use SSL, the connector's protocol handler must be an AbstractHttp11JsseProtocol subclass
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:162) ~[spring-boot-2.4.1.jar!/:2.4.1]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582) ~[spring-context-5.3.2.jar!/:5.3.2]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.1.jar!/:2.4.1]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.1.jar!/:2.4.1]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.1.jar!/:2.4.1]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.1.jar!/:2.4.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.1.jar!/:2.4.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-2.4.1.jar!/:2.4.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-2.4.1.jar!/:2.4.1]

@andreldm
Copy link
Author

@metrue, it's been a while since I wrote that and didn't have to worry about APR since then. That code was written for Spring Boot 1.4.1 (Tomcat 8.x), probably you need to use a different protocol class or this approach doesn't work anymore for Spring Boot 2.x (Tomcat 9.x).

@dirask
Copy link

dirask commented Apr 6, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment