Last active
March 1, 2019 18:43
-
-
Save vanjis/6523a577f6ab3eec8d0518c9880065cf to your computer and use it in GitHub Desktop.
Forcing HTTPS in spring boot 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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