-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SSL with support for http as well and forced redirection from h…
…ttp to https
- Loading branch information
Patrice Laplante
committed
Feb 22, 2021
1 parent
2ad16e7
commit 83a1187
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,6 @@ hs_err_pid* | |
target | ||
|
||
*.iml | ||
*.factorypath | ||
*.factorypath | ||
|
||
ssl |
61 changes: 61 additions & 0 deletions
61
src/main/java/io/kidsfirst/config/HttpConnectorConfig.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.kidsfirst.config; | ||
|
||
import org.apache.catalina.Context; | ||
import org.apache.catalina.connector.Connector; | ||
import org.apache.coyote.http11.Http11NioProtocol; | ||
import org.apache.tomcat.util.descriptor.web.SecurityCollection; | ||
import org.apache.tomcat.util.descriptor.web.SecurityConstraint; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; | ||
import org.springframework.boot.web.servlet.server.ServletWebServerFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class HttpConnectorConfig { | ||
@Value("${server.port}") | ||
private int httpsPort; | ||
|
||
@Value("${server.http.port}") | ||
private int httpPort; | ||
|
||
@Value("${server.http.force-ssl}") | ||
private boolean forceSSL; | ||
|
||
@Bean | ||
public ServletWebServerFactory servletContainer() { | ||
TomcatServletWebServerFactory tomcat = | ||
forceSSL ? 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); | ||
} | ||
} : | ||
new TomcatServletWebServerFactory(); | ||
|
||
tomcat.addAdditionalTomcatConnectors(createHttpConnector()); | ||
return tomcat; | ||
} | ||
|
||
private Connector createHttpConnector() { | ||
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); | ||
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler(); | ||
try { | ||
connector.setScheme("http"); | ||
connector.setSecure(false); | ||
connector.setPort(httpPort); | ||
protocol.setSSLEnabled(false); | ||
connector.setRedirectPort(httpsPort); | ||
|
||
return connector; | ||
} catch (Exception ex) { | ||
throw new IllegalStateException("Fail to create http connector", ex); | ||
} | ||
} | ||
|
||
} |
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