Skip to content

Instantly share code, notes, and snippets.

@drissamri
Last active November 17, 2018 05:33
Show Gist options
  • Save drissamri/8def1ce9322caab47e8e to your computer and use it in GitHub Desktop.
Save drissamri/8def1ce9322caab47e8e to your computer and use it in GitHub Desktop.
Enable HTTPS in Spring Boot
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
return tomcat;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
@svanscho
Copy link

svanscho commented Aug 4, 2015

How can you inject runtime configuration values here? I am having trouble to substitute the port numbers with some more dynamic value.

@romanm
Copy link

romanm commented Jul 7, 2017

Context context, package?

@parmarkaushik15
Copy link

import org.apache.catalina.Context; This package required to import Context in protected void postProcessContext(Context context) {}

@stefantwog
Copy link

Can you share all your imports please ?

@quintessential43
Copy link

Hi All

Anyone here looking for the the imports, chances are that you are using springboot 2 If so please modify you code as follows:
@bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@OverRide
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(redirectConnector());
return tomcat;
}

@LeeReindeer
Copy link

Hi All

Anyone here looking for the the imports, chances are that you are using springboot 2 If so please modify you code as follows:
@bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@OverRide
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(redirectConnector());
return tomcat;
}

Thanks! That works fine.

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