我正在尝试为使用嵌入式Jetty实例的WebSocket服务器配置安全连接。这就是我配置的方式:
import org.eclipse.jetty.http.HttpVersion
import org.eclipse.jetty.server.*
import org.eclipse.jetty.servlet.ServletContextHandler
import org.eclipse.jetty.util.ssl.SslContextFactory
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer
import org.slf4j.LoggerFactory
import java.io.*
import java.net.InetAddress
import java.security.KeyStore
import javax.net.ssl.KeyManagerFactory
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManagerFactory
import javax.websocket.*
import javax.websocket.server.ServerEndpoint
(...)
@Throws(Exception::class)
private fun getServerConnector(): ServerConnector {
val connector: ServerConnector
if (USE_SSL) {
LOG.info("** Using SSL for all websocket connections **")
val contextFactory = SslContextFactory()
contextFactory.sslContext = this.buildSSLContext()
val sslConnectionFactory = SslConnectionFactory(contextFactory, HttpVersion.HTTP_1_1.toString())
val config = HttpConfiguration()
config.secureScheme = "https"
config.securePort = settings.getPort()
config.outputBufferSize = Int.MAX_VALUE
config.requestHeaderSize = 8192
config.responseHeaderSize = 8192
val sslConfiguration = HttpConfiguration(config)
sslConfiguration.addCustomizer(SecureRequestCustomizer())
val httpConnectionFactory = HttpConnectionFactory(sslConfiguration)
connector = ServerConnector(this.server, sslConnectionFactory, httpConnectionFactory)
} else {
val config = HttpConfiguration()
config.securePort = settings.getPort()
config.outputBufferSize = Int.MAX_VALUE
config.requestHeaderSize = 8192
config.responseHeaderSize = 8192
val httpConnectionFactory = HttpConnectionFactory()
connector = ServerConnector(this.server, httpConnectionFactory)
}
connector.port = settings.getPort()
return connector
}
@Throws(Exception::class)
private fun buildSSLContext(): SSLContext {
val ks = KeyStore.getInstance("JKS")
val kf = File(KEYSTORE_FILE)
ks.load(FileInputStream(kf), KEYSTORE_PASSWORD.toCharArray())
val kmf = KeyManagerFactory.getInstance("SunX509")
kmf.init(ks, KEY_PASSWORD.toCharArray())
val tmf = TrustManagerFactory.getInstance("SunX509")
tmf.init(ks)
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(kmf.keyManagers, tmf.trustManagers, null)
return sslContext
}
如果我尝试连接到此服务器,我会立即断开连接,但客户端和服务器中都不会显示错误消息。
我错过了什么?
PS。我使用的语言是Kotlin,但它是一种JVM语言,所以如果你了解Java就知道这个,库是相同的。