Handling HTTPS requests with Akka-HTTPS Server

Hi guys,

In my last blogs I explained how one can create a self-signed certificate and KeyStore in PKCS12. You can go through the previous blog, as we’ll be needing certificate and keystore  for handling HTTPS requests.

  1. https://blog.knoldus.com/2016/10/18/create-a-self-signed-ssl-certificate-using-openssl/
  2. https://blog.knoldus.com/2016/10/26/how-to-create-a-keystore-in-pkcs12-format/

Akka-HTTP provides both Server-Side and Client-Side HTTPS support.

In this blog I’ll be covering the Server-Side HTTPS support.

Let’s start with “why do we need server-side HTTPS support?”

If we want the communication between the browser and the server to be encrypted we need to handle HTTPS request.  HTTPS is often used to protect highly confidential online transactions like online banking and online shopping order forms.

Akka-HTTP supports TLS(Transport Layer Security).

For handling the HTTPS request we need to have the SSL certificate and the KeyStore. Once you have generated both you can go through the example.

In this example, you will see how easily you can handle HTTPS  request. Akka HTTP provides support for low level and high level HTTP server APIs. I’ve used high level API in this example.

Add following dependencies in your build.sbt file:

libraryDependencies ++= {
  val AkkaHttpVersion   = "2.4.8"
    "com.typesafe.akka" %% "akka-http-testkit" % "2.4.8",
    "com.typesafe.akka" %% "akka-http-experimental" % "2.4.8", 
    "org.scalatest" %% "scalatest" % "2.2.6" % "test"

You can add the generated KeyStore in the resource package of your project and pass the name of the keyStore in Boot.scala.

val keystore: InputStream = getClass.getClassLoader

You would also  need to provide the password which you have used for generating the KeyStore.

val password: Array[Char] = "akka-https".toCharArray
ks.load(keystore, password)

It is recommended not to store passwords in code, you can read it from somewhere safe like application.conf .

In my Boot.scala you’ll find following line:

val keyManagerFactory: KeyManagerFactory = KeyManagerFactory.
val tmf: TrustManagerFactory = TrustManagerFactory.getInstance("SunX509")

We need KeyManagerFactory and TrustManagerFactory to initialize  SSLContext.

TrustManager determines whether remote connection should be trusted or not i.e. whether remote party is who it claims to and KeyManager decides which authentication credentials should be sent to the remote host for authentication during SSL handshake.

You can download Postman or any other rest client for making the request on your server.

You can find the complete example  here .

Enjoy 🙂




This entry was posted in Akka, akka-http, Scala, Security, Security Audit, Security Checklist, Security Controls, Security Guidelines and tagged , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Handling HTTPS requests with Akka-HTTPS Server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s