Home > News > Basic Http authentication in Lift.

Basic Http authentication in Lift.

December 16, 2011

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 {
   LiftRules.addToPackages("XYZ")
.....
.....
} }

*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:

 LiftRules.httpAuthProtectedResource.prepend{
      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) => {
        logger.info("Authentication Succeeded...Welcome!!")
         userRoles(AuthRole("admin"))
         true
      }
    }



* 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.

About these ads
Categories: News
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: