Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Handling HTTPS Requests With Akka-HTTPS Server

DZone's Guide to

Handling HTTPS Requests With Akka-HTTPS Server

As security practices move toward HTTPS, you can use Akka-HTTPS and a bit of Scala to help handle the SSL handshake, certificates, and KeyStores.

· Security Zone
Free Resource

Discover how to protect your applications from known and unknown vulnerabilities.

In my last posts, 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 a certificate and KeyStore for handling HTTPS requests.

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  requests. Akka HTTP provides support for low-level and high-level HTTP server APIs. I’ve used a high-level API in this example.

Add the 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 that you 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 the 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 a remote connection should be trusted or not (whether a remote party is who it claims to be) and KeyManager decides which authentication credentials should be sent to the remote host for authentication during the 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!

Find out how Waratek’s award-winning virtualization platform can improve your web application security, development and operations without false positives, code changes or slowing your application.

Topics:
https ,akka ,scala ,security ,tutorial

Published at DZone with permission of Rishabh Verma, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}