Lift web framework accommodate a no. of conspicuous features that are implemented in a unique and solitary way as compared to other frameworks. Here we’ll discuss about the basic “Http authentication” mechanism for security in Lift.

What is ‘LiftRules’ singleton ?
Nearby all configuration parameters for the http requests and response are defined in LiftRules singleton. The important thing to notice is that we can only change the configuration parameteres of LiftRules only during boot but not at other times. You can find more about LiftRules Here.

Now for our purpose of Http authentication what all you have to do , is to add some Lines of code using the LiftRules singleton present in lift, in to your Boot.scala File and you’ll find all gets done for a basic http authentication.

Let us have a look to the steps for doing it ::

*Firstly in your Boot.scala file make the class extends Loggable(part of “net.liftweb.common”) as :

class Boot extends Loggable {
  def boot {
} }

*Now you have to proceed with the ‘prepend’ method(from LiftRules package) implementation in your Boot(def Boot) method & defining the name of html pages on which you want to implement the authentication as follows:

      case (Req("html_page_name":: Nil,_,_)) => Full(AuthRole("admin"))
      case (Req("securepage":: Nil,_,_)) => Full(AuthRole("admin"))
// Just mention the above code for page on which you
// want to implement the http authentication.   


*Call the authentication(have root in ‘HttpAuthentication.class’) from the LiftRules and mention the username and password for the authentication purpose as :

LiftRules.authentication = HttpBasicAuthentication("Authenticate Yourself") {      
      case("user_name","password",req) => {"Authentication Succeeded...Welcome!!")

* Now each time when user will try to access the page,browser will pop up for the http authentication like: 🙂

Http Authentication in Lift

Note:- Don’t forgot the following basic imports:

import common.{Loggable, Full}
import http._
import auth.{userRoles, HttpBasicAuthentication, AuthRole}

::: Want to get start with Lift ? come Here.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.