In this recipe, we will explore how to secure API endpoints using the HTTP basic authentication scheme with Play 2.0. We will use the Apache Commons Codec library for Base64 encoding and decoding for this recipe. This dependency is implicitly imported by Play and we will not need to explicitly declare it to our library dependencies in build.sbt
.
For Java, we need to perform the following steps:
Run the
foo_java
application with Hot-Reloading enabled:activator "~run"
Create a new
play.mvc.Security.Authenticator
implementation class infoo_java/app/controllers/BasicAuthenticator.java
with the following content:package controllers; import org.apache.commons.codec.binary.Base64; import play.mvc.Http; import play.mvc.Result; import play.mvc.Security; public class BasicAuthenticator extends Security.Authenticator { private static final String AUTHORIZATION = "authorization"; private...