Book Image

Play Framework Cookbook - Second Edition

By : Alexander Reelsen, Giancarlo Inductivo
Book Image

Play Framework Cookbook - Second Edition

By: Alexander Reelsen, Giancarlo Inductivo

Overview of this book

<p>As web and mobile systems become more sophisticated, anchoring systems in a mature, solid framework has become increasingly important. Play 2 provides developers with the necessary tools to build robust web applications.</p> <p>This book is a compilation of useful recipes aimed at helping developers discover the power of Play 2. The introductory section serves as a primer to Play Framework, wherein all the fundamentals of the framework are covered extensively. It then explains the usage of controllers and how modules can be leveraged for optimal performance. Next, the book walks you through creating and using APIs, followed by extensive real-world applications. Finally, you will learn to manage applications post production.</p>
Table of Contents (15 chapters)
Play Framework Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Securing API endpoints with HTTP basic authentication


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.

How to do it…

For Java, we need to perform the following steps:

  1. Run the foo_java application with Hot-Reloading enabled:

        activator "~run"
    
  2. Create a new play.mvc.Security.Authenticator implementation class in foo_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...