!htc #20371 allows for simple HTTPS usage from Java, fixes typo in APIs (#20373)

This commit is contained in:
Konrad Malawski 2016-04-25 12:01:03 +02:00
parent b983f19c1f
commit 0e11ec2057
28 changed files with 674 additions and 120 deletions

View file

@ -0,0 +1,72 @@
/*
* Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
*/
package docs.http.scaladsl.server
//#imports
import java.io.InputStream
import java.security.{ SecureRandom, KeyStore }
import javax.net.ssl.{ SSLContext, TrustManagerFactory, KeyManagerFactory }
import akka.actor.ActorSystem
import akka.http.scaladsl.server.{ RouteResult, Route, Directives }
import akka.http.scaladsl.{ ConnectionContext, HttpsConnectionContext, Http }
import akka.stream.ActorMaterializer
import com.typesafe.sslconfig.akka.AkkaSSLConfig
//#
import docs.CompileOnlySpec
import org.scalatest.{ Matchers, WordSpec }
abstract class HttpsServerExampleSpec extends WordSpec with Matchers
with Directives with CompileOnlySpec {
class HowToObtainSSLConfig {
//#akka-ssl-config
implicit val system = ActorSystem()
val sslConfig = AkkaSSLConfig()
//#
}
"low level api" in compileOnlySpec {
//#low-level-default
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()
implicit val dispatcher = system.dispatcher
// Manual HTTPS configuration
val password: Array[Char] = ??? // do not store passwords in code, read them from somewhere safe!
val ks: KeyStore = KeyStore.getInstance("PKCS12")
val keystore: InputStream = getClass.getClassLoader.getResourceAsStream("server.p12")
require(keystore != null, "Keystore required!")
ks.load(keystore, password)
val keyManagerFactory: KeyManagerFactory = KeyManagerFactory.getInstance("SunX509")
keyManagerFactory.init(ks, password)
val tmf: TrustManagerFactory = TrustManagerFactory.getInstance("SunX509")
tmf.init(ks)
val sslContext: SSLContext = SSLContext.getInstance("TLS")
sslContext.init(keyManagerFactory.getKeyManagers, tmf.getTrustManagers, SecureRandom.getInstanceStrong)
val https: HttpsConnectionContext = ConnectionContext.https(sslContext)
// sets default context to HTTPS all Http() bound servers for this ActorSystem will use HTTPS from now on
Http().setDefaultServerHttpContext(https)
//#
//#bind-low-level-context
Http().bind("127.0.0.1", connectionContext = https)
// or using the high level routing DSL:
val routes: Route = get { complete("Hello world!") }
Http().bindAndHandle(routes, "127.0.0.1", 8080, connectionContext = https)
//#
}
}