Over a million developers have joined DZone.

Authentication Using Actions in Play Framework

DZone's Guide to

Authentication Using Actions in Play Framework

Actions play an important role when you're using the Play Framework, particularly when determining controller behavior. Read on for some helpful code snippets to help you master them.

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

Actions plays an important role in Play Framework, the requests received by a Play application are handled by an Action. Action composition is an incredibly powerful way to enhance or restrict controller behaviour. In Play Framework controllers consist of methods that create Action objects to handle the incoming requests.

play.api.mvc.Action is basically a (play.api.mvc.Request => play.api.mvc.Result) function that handles a request and generates a result to be sent to the client. 

We can provide authentication to our application by using the ActionBuilder trait. To implement ActionBuilder we need to implement the invokeBlock method, which takes the current request and a block of code as arguments.

def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result])

Customizing Your ActionBuilder

Suppose in your application you want some operations to be performed on every request, for that you can create your own ActionBuilder and perform the operations in it.

Firstly you need to extend the ActionBuilder and provide implementation for the invokeBlock method.

In our use case, suppose we want every request with a parameter  username  in the request header, set to either ‘Jake’, ‘Alex’, ‘Ryan’, or ‘Nicholas’. Only they will be able to access our application, then we can write our code as:

case class UserRequest[A](val userName: String, val request: Request[A])
  extends WrappedRequest[A](request)
object SecuredAction extends ActionBuilder[UserRequest] {

  override def invokeBlock[A](request: Request[A],
      block: (UserRequest[A]) => Future[Result]): Future[Result] = {
    val userName = request.headers.get("username").fold("")(identity)
    if (UserService.getAllUsers().contains(User(userName))) {
      block(UserRequest(userName, request))
    } else {
      Future.successful(Results.Unauthorized("Unauthorized access !!"))

All the other requests with different username in header are given an ‘Unauthorized access’ message. Using this, only some of the authorized users will be able to access our application, response to other requests will be sent back from the invokeBlock itself and your controller code will not be executed.

Here is a simple application demonstration: Play Authentication.

Happy Learning !! 

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

java ,play ,framework

Published at DZone with permission of Geetika Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}