gpt4 book ai didi

scala - 带有非 http TCP 服务器的 Lightbend ssl-config

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:56 25 4
gpt4 key购买 nike

我找到了一些资源,它们提供了有关在 application.conf 文件中配置 ssl-config 选项的详细信息,并且我已经确定了如何使用 AkkaSSLConfig.get() 访问这些配置。我已经看到可以使用 AkkaSSLConfig 对象作为 ConnectionContext.https() 的参数来创建 https 上下文。

是否可以将其用于非 http 服务器?上下文是否以某种方式返回特定于 http?我正在尝试利用 ssl-config,但我不清楚它是否为非 http 服务器提供了任何优势,而且我看不到任何从 ssl-config 定义构建上下文的方便方法,在在这种情况下,我似乎也可以手动定义上下文。

最后,很难找到任何为非 http 服务器构建上下文的示例。看起来这个过程可能与 http 服务器相同,但我发现示例通常包括使用名称中带有“http”的类/方法。如果有人知道一个很好的例子,我将不胜感激。

最佳答案

import java.io.{File, FileInputStream}
import java.security.{KeyStore, SecureRandom}
import akka.actor.ActorSystem
import akka.http.scaladsl.Http.ServerBinding
import akka.http.scaladsl.model.{HttpResponse, StatusCodes}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.Directives.pathSingleSlash
import akka.http.scaladsl.{ConnectionContext, Http}
import akka.stream.{ActorMaterializer, TLSClientAuth}
import com.typesafe.sslconfig.akka.AkkaSSLConfig
import com.typesafe.sslconfig.ssl.{KeyManagerConfig, KeyManagerFactoryWrapper, KeyStoreConfig, SSLConfigFactory, SSLConfigSettings}
import javax.net.ssl.{SSLContext, TrustManagerFactory}

import scala.concurrent.{ExecutionContext, Future}

object Test extends App{

implicit val actorSystem: ActorSystem = ActorSystem("test")
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val executionContext: ExecutionContext = actorSystem.dispatcher

val ksConfig: KeyStoreConfig = KeyStoreConfig.apply(data = None,
filePath = Some("/Users/mshaik/testApp/src/main/resources/keystore/localhost.p12")
).withPassword(Some("test"))

val kmConfig: KeyManagerConfig = KeyManagerConfig().withKeyStoreConfigs(List(ksConfig))

val sslConfigSettings: SSLConfigSettings = SSLConfigFactory.defaultConfig.withKeyManagerConfig(kmConfig)

val akkaSSLConfig: AkkaSSLConfig = AkkaSSLConfig.get(actorSystem).withSettings(sslConfigSettings)

val ks: KeyStore = KeyStore.getInstance("PKCS12")
ks.load(new FileInputStream(new File(ksConfig.filePath.get)), ksConfig.password.get.toCharArray)

val kmf: KeyManagerFactoryWrapper = akkaSSLConfig.buildKeyManagerFactory(sslConfigSettings)
kmf.init(ks, ksConfig.password.get.toCharArray)

val tmf: TrustManagerFactory = TrustManagerFactory.getInstance("SunX509")
tmf.init(ks)

val sslContext: SSLContext = SSLContext.getInstance("TLS")
sslContext.init(kmf.getKeyManagers, tmf.getTrustManagers, new SecureRandom)

val ctx: ConnectionContext = ConnectionContext.https(sslContext,
sslConfig = Some(akkaSSLConfig),
clientAuth = Some(TLSClientAuth.Want)
)

var bindingFuture: Future[ServerBinding] = _

Http().setDefaultServerHttpContext(ctx)

val route: Route = pathSingleSlash {
get {
complete(HttpResponse(StatusCodes.OK, entity = "Welcome to base path!"))
}
}

try{
bindingFuture = Http().bindAndHandle(route, "localhost", 8085, connectionContext = ctx)
println( s"Server online at https://localhost:8085/")
} catch {
case ex: Exception =>
println(this.getClass, ex.getMessage, ex)
materializer.shutdown()
actorSystem.terminate()
}
}

关于scala - 带有非 http TCP 服务器的 Lightbend ssl-config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52050862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com