Skip to content

Commit

Permalink
Update Kotlin example for Tomcat connector customization
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Nov 23, 2022
1 parent 9af2a57 commit 2170941
Showing 1 changed file with 5 additions and 23 deletions.
Expand Up @@ -17,45 +17,27 @@
package org.springframework.boot.docs.howto.webserver.enablemultipleconnectorsintomcat

import org.apache.catalina.connector.Connector
import org.apache.coyote.http11.Http11NioProtocol
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
import org.springframework.boot.web.server.WebServerFactoryCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.util.ResourceUtils
import java.io.IOException

@Configuration(proxyBeanMethods = false)
class MyTomcatConfiguration {

@Bean
fun sslConnectorCustomizer(): WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
fun connectorCustomizer(): WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
return WebServerFactoryCustomizer { tomcat: TomcatServletWebServerFactory ->
tomcat.addAdditionalTomcatConnectors(
createSslConnector()
createConnector()
)
}
}

private fun createSslConnector(): Connector {
private fun createConnector(): Connector {
val connector = Connector("org.apache.coyote.http11.Http11NioProtocol")
val protocol = connector.protocolHandler as Http11NioProtocol
return try {
val keystore = ResourceUtils.getURL("keystore")
val truststore = ResourceUtils.getURL("truststore")
connector.scheme = "https"
connector.secure = true
connector.port = 8443
protocol.isSSLEnabled = true
protocol.keystoreFile = keystore.toString()
protocol.keystorePass = "changeit"
protocol.truststoreFile = truststore.toString()
protocol.truststorePass = "changeit"
protocol.keyAlias = "apitester"
connector
} catch (ex: IOException) {
throw IllegalStateException("Fail to create ssl connector", ex)
}
connector.port = 8081
return connector
}

}

0 comments on commit 2170941

Please sign in to comment.