http - 使用 http4s 从 http 更改为 https

标签 http https http4s

有没有办法使用 http4s 库将 http 服务器更改为 https? ( https://http4s.org/ )

最佳答案

我发现自己面临同样的问题,但我设法解决了它,事情是这样的:

  1. 您需要寻找构建服务器的时机,可能是使用 BlazeServerBuilder。

  2. BlazeServerBuilder 有方法“withSslContext(sslContext: SSLContext)”来启用 SSL。因此,您需要做的就是创建一个 SSLContext 对象并将其传递给服务器构建器。

请记住,在使用 SSL 证书之前,您可能必须使用 Java 的 keytool 实用程序将 SSL 证书存储在 keystore 中。

SSL 上下文和 SSL 证书

如何使用 SSL 证书创建 SSL 上下文是另一个问题,但这里有一篇有趣的文章,介绍了从 Let's Encrypt 获取免费证书、将其存储在 keystore 中并从 Java 应用程序使用它的过程:Using Let's Encrypt certificates in Java applications - Ken Coenen — Ordina JWorks Tech Blog

这是我用于在 Scala 中创建 SSLContext 的代码:

val keyStorePassword: String   = your_keystore_password
val keyManagerPassword: String = your_certificate_password
val keyStorePath: String       = your_keystore_location

val keyStore = KeyStore.getInstance(KeyStore.getDefaultType)

val in = new FileInputStream(keyStorePath)
keyStore.load(in, keyStorePassword.toCharArray)

val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm)
keyManagerFactory.init(keyStore, keyStorePassword.toCharArray)

val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
trustManagerFactory.init(keyStore)

val sslContext = SSLContext.getInstance("TLS")
sslContext.init(keyManagerFactory.getKeyManagers, trustManagerFactory.getTrustManagers, new SecureRandom())
sslContext

关于http - 使用 http4s 从 http 更改为 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61278058/

相关文章:

android - Volley 库和 HTTPS 请求

http4s - 如何增加http4s的请求超时

Angular2 http.post 执行了两次

http - HTTP 状态代码 308 ("Permanent Redirect") 是在 HTTP 2 中引入的吗?

PHP - 如何打开正在按文件类型在页面上流式传输的文件

Scala:http4s 对于在curl/requests 中工作的相同请求给出 401 Unauthorized

scala - 将json正文添加到http4s请求

java - 在 Java Servlet 中限制 HTTP 请求

Android:发出 Https 请求

ssl - Heroku:Python Flask 应用程序——自动从 https 重定向到 http