@@ -2,42 +2,30 @@ package com.example.helloworld
22
33// #import
44
5-
6- import java .security .KeyStore
7- import java .security .SecureRandom
8- import java .security .cert .Certificate
9- import java .security .cert .CertificateFactory
10-
11- import scala .io .Source
12-
135import akka .actor .typed .ActorSystem
146import akka .actor .typed .scaladsl .Behaviors
157import akka .http .scaladsl .ConnectionContext
168import akka .http .scaladsl .Http
17- import akka .http .scaladsl .HttpsConnectionContext
9+ import akka .http .scaladsl .common . SSLContextFactory
1810import akka .http .scaladsl .model .HttpRequest
1911import akka .http .scaladsl .model .HttpResponse
20- import akka .pki .pem .DERPrivateKeyLoader
21- import akka .pki .pem .PEMDecoder
2212import com .typesafe .config .ConfigFactory
23- import javax .net .ssl .KeyManagerFactory
24- import javax .net .ssl .SSLContext
2513
14+ import java .nio .file .Paths
2615import scala .concurrent .ExecutionContext
2716import scala .concurrent .Future
17+ import scala .concurrent .duration ._
2818import scala .util .Failure
2919import scala .util .Success
30- import scala .concurrent .duration ._
3120// #import
3221
33-
3422// #server
3523object GreeterServer {
3624
3725 def main (args : Array [String ]): Unit = {
3826 // important to enable HTTP/2 in ActorSystem's config
39- val conf = ConfigFactory .parseString( " akka.http.server.enable-http2 = on " )
40- .withFallback(ConfigFactory .defaultApplication())
27+ val conf =
28+ ConfigFactory .parseString( " akka.http.server.enable-http2 = on " ) .withFallback(ConfigFactory .defaultApplication())
4129 val system = ActorSystem [Nothing ](Behaviors .empty[Nothing ], " GreeterServer" , conf)
4230 new GreeterServer (system).run()
4331 }
@@ -52,6 +40,12 @@ class GreeterServer(system: ActorSystem[_]) {
5240 val service : HttpRequest => Future [HttpResponse ] =
5341 GreeterServiceHandler (new GreeterServiceImpl (system))
5442
43+ val serverHttpContext = ConnectionContext .httpsServer(
44+ SSLContextFactory .createSSLContextFromPem(
45+ // Note: filesystem paths, not classpath
46+ Paths .get(" src/main/resources/certs/server1.pem" ),
47+ Paths .get(" src/main/resources/certs/server1.key" )))
48+
5549 val bound : Future [Http .ServerBinding ] = Http ()(system)
5650 .newServerAt(interface = " 127.0.0.1" , port = 8080 )
5751 .enableHttps(serverHttpContext)
@@ -70,34 +64,5 @@ class GreeterServer(system: ActorSystem[_]) {
7064
7165 bound
7266 }
73- // #server
74-
75-
76- private def serverHttpContext : HttpsConnectionContext = {
77- val privateKey =
78- DERPrivateKeyLoader .load(PEMDecoder .decode(readPrivateKeyPem()))
79- val fact = CertificateFactory .getInstance(" X.509" )
80- val cer = fact.generateCertificate(
81- classOf [GreeterServer ].getResourceAsStream(" /certs/server1.pem" )
82- )
83- val ks = KeyStore .getInstance(" PKCS12" )
84- ks.load(null )
85- ks.setKeyEntry(
86- " private" ,
87- privateKey,
88- new Array [Char ](0 ),
89- Array [Certificate ](cer)
90- )
91- val keyManagerFactory = KeyManagerFactory .getInstance(" SunX509" )
92- keyManagerFactory.init(ks, null )
93- val context = SSLContext .getInstance(" TLS" )
94- context.init(keyManagerFactory.getKeyManagers, null , new SecureRandom )
95- ConnectionContext .httpsServer(context)
96- }
97-
98- private def readPrivateKeyPem (): String =
99- Source .fromResource(" certs/server1.key" ).mkString
100- // #server
101-
10267}
10368// #server
0 commit comments