Monday, 30 December 2013

Adding “remember me” functionality to Oracle ADF login

 

The following example based on Jdeveloper 11g R2 (11.1.2.3)

There are many ways to implement login page in Oracle ADF. One can create custom login logic or use build in security mechanism. This is covered in many blogs and also in official Oracle documentation and I am not going to touch it in this post.

  I do want to explain how to add a functionality for “remember me”, so if user already logged in and checked “remember me” option, next time he logs into the application he will not be asked to enter the credentials again

I started by creating some login form with “remember” check box

image

Definition of the check box button

image 

When I press the login button I check that the check box is checked and if it checked I save the credentials in the cookies

image

I created some other  dummy page that I expect to see if user logged in with Cookies

Here my adfc-config.xml file

image

“AfterLogin” page contains only text “some other page”

Now I want my cookies to be checked when user enters the login page (“LoginTest” page in my application)

I will do it with JavaScript that will check the cookies after the page is loaded

Note!!! I tried to do it with all other methods like “afterPhase” or by using “router” in the Task-Flow. It didn’t work

image

When the page is loaded eventually I call “goToDashBoard” method in my backing bean

public void goToDashBoard(ClientEvent clientEvent) {
   checkCookies();
}

 

public String checkCookies() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, Object> cookies = externalContext.getRequestCookieMap();
    Cookie user= (Cookie)cookies.get("nam.user.cookie");
    Cookie pass= (Cookie)cookies.get("nam.pass.cookie");
    Cookie domain = (Cookie)cookies.get("nam.domain.cookie");
    if (user!=null &&

         pass!=null &&

         domain!=null &&  

         user.getValue()!=null &&

         pass.getValue()!=null &&

         domain.getValue()!=null)

      {
        this._username=user.getValue();
        this._password =  pass.getValue();
        this._domain = domain.getValue();
        NavigationHandler  nvHndlr = FacesContext.getCurrentInstance().getApplication().getNavigationHandler();
        nvHndlr.handleNavigation(FacesContext.getCurrentInstance(), null, "AfterLogin");

     }

}

All I do here is reading the information from Cookies and if credentials are found to be stored in the Cookies I programmatically execute “AfterLogin” control case (see the print screen of adfc-config.xml)

No comments:

Post a Comment