Handling HTTPS requests with Akka-HTTPS Server

Table of contents
Reading Time: 2 minutes

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"
  Seq(
    "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
.getResourceAsStream("keystore.pkcs12")

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.
getInstance("SunX509")
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 🙂

Reference:

http://doc.akka.io/docs/akka/2.4.8/scala/http/server-side-https-support.html

KNOLDUS-advt-sticker

Written by 

Joseph Ross is a Principal Consultant at Knoldus Inc. having more than 10 years of experience. Joseph has a passion for identifying challenges and give impactful solutions to the clients. He is a football fan and loves to watch TV series. Joseph has a cross-functional business operations and technology consulting experience. Joseph is familiar with programming languages such as Scala, C++, Java, CSS and HTML.

4 thoughts on “Handling HTTPS requests with Akka-HTTPS Server2 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading