DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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.

Rishabh Verma user avatar by
Rishabh Verma
·
Jan. 21, 17 · Tutorial
Like (2)
Save
Tweet
Share
6.58K Views

Join the DZone community and get the full member experience.

Join For Free

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!

HTTPS Requests

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Discovery With Eureka
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • What Is a Kubernetes CI/CD Pipeline?
  • Deploying Java Serverless Functions as AWS Lambda

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: