Skip to content

Instantly share code, notes, and snippets.

@vanjis
Last active March 1, 2019 18:43
Show Gist options
  • Save vanjis/6523a577f6ab3eec8d0518c9880065cf to your computer and use it in GitHub Desktop.
Save vanjis/6523a577f6ab3eec8d0518c9880065cf to your computer and use it in GitHub Desktop.
Forcing HTTPS in spring boot 2
@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;
}
private Connector redirectConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment