【发布时间】:2016-03-27 06:16:26
【问题描述】:
我正在尝试使用自签名证书在我的 akka-http 服务器 (localhost) 上获得安全连接。我没有成功。
关于 ssl 的 Akka-http 文档有点模糊。我试图从各地收集点点滴滴的信息,但仍然无法正常工作。
我创建了一个如下所示的 ssl 特征:
trait MySslConfiguration {
def serverContext: HttpsContext = {
val password = "password".toCharArray
val context = SSLContext.getInstance("TLSv1.2")
val keyStore = KeyStore.getInstance("jks")
val keyStoreResource = "/scruples_keystore.jks"
val keyManagerFactory = KeyManagerFactory.getInstance("SunX509")
val trustManagerFactory = TrustManagerFactory.getInstance("SunX509")
keyStore.load(getClass.getClassLoader.getResourceAsStream(keyStoreResource), password)
keyManagerFactory.init(keyStore, password)
trustManagerFactory.init(keyStore)
context.init(keyManagerFactory.getKeyManagers, trustManagerFactory.getManagers, new SecureRandom())
val sslParams = context.getDefaultSSLParameters
sslParams.setEndpointIdentificationAlgorithm("HTTPS")
HttpsContext(sslContext = context
sslParameters = Some(sslParams),
enabledProtocols = Some(List("TLSv1.2", "TLSv1.1", "TLSv1")))
}
}
我将 trait 添加到我的服务器启动中,如下所示:
object Main extends App with Core with RestInterface with MySslConfiguration {
val metricRegistry = new com.codahale.metrics.MetricRegistry()
override implicit val injector = GlobalInjector.getInjector
override implicit val system: ActorSystem = injector.instance[ActorSystem]
override def config: Config = injector.instance[Config]
override implicit def executor: ExecutionContextExecutor = system.dispatcher
override val logger: LoggingAdapter = Logging(system, getClass)
override implicit val materializer: Materializer = ActorMaterializer()
val routes = allRoutes
Http().bindAndHandle(routes, config.getString("http.host"), config.getInt("http.port"), httpsContext = Some(serverContext))
自签名 jks 密钥库创建如下:
keytool -genkey -keyalg RSA -keysize 2048 -keystore scruples_keystore.jks -alias myalias
有人可以帮忙吗?
我做错了什么?我确定存在配置问题,但似乎无法解决。
另一个问题是,如果我使用来自 CA 的适当证书会发生什么变化?
非常感谢
【问题讨论】:
-
Akka Http 使用 ssl-config 库,该库有自己的大量文档:typesafehub.github.io/ssl-config(它取自 Play-WS 并提取为独立库,现在也被 Akka-Http 使用) .另见项目github.com/typesafehub/ssl-config
-
感谢您为我指明了正确的方向。我已经添加了您在上面发布的文档中描述的配置,但是仍然无法使其正常工作。您能否发布自签名证书的示例代码/配置。我是否要创建配置并调用
SSLContext.getDefault,以便在启动服务器时在绑定方法中配置HTTPContext?我什至尝试设置ssl-config-ssl.default = true,但仍然没有让服务器响应https请求
标签: scala ssl akka self-signed akka-http