Activate your free membership today | Log-in

Sunday, August 20th, 2006

Ajax Login with Acegi

Category: Java

<p>Sanjiv Jivan has written about how he has integrated an ajax based login form to the Acegi Spring module for Auth and Auth.

His implementation contains a ServletFilter in J2EE land, and JavaScript in the client:

JAVASCRIPT:
  1.  
  2. function ajaxLogin() {
  3.     Element.update('loginMessage', 'Sending request ...');
  4.     Element.show('loginMessage');
  5.     var opt = {
  6.  
  7.         method: 'post',
  8.  
  9.         postBody: Form.serialize($('loginForm')) + '&ajax=true',
  10.  
  11.  
  12.         onSuccess: function(response) {
  13.             var msg = response.responseText;
  14.             if ("error:" == msg.substr(0, 6)) {
  15.                 var fp = "<font color='red'>" + msg.substring(6, msg.length) + '</font><br /><a href="forgetPwd.html">Forget Password?</a>';
  16.                 Element.update('loginMessage', fp);
  17.             } else if ("url:" == msg.substr(0, 4)) {
  18.                 location.href = msg.substring(4, msg.length);
  19.             } else if ("message:" == msg.substr(0, 8)) {
  20.                 Element.update('loginMessage', msg.substring(8, msg.length));
  21.             }
  22.         }
  23.     }
  24.     new Ajax.Request('j_acegi_security_check', opt);
  25. }
  26.  

Related Content:

  • Ajax Learning Guide
    Chances are, you've been doing JavaScript and XML developer work in Lotus Domino for quite some time. This old/new approach is causing quite a stir in...
  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...
  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...
  • Professional Java Development with the Spring Framework, Chapter 10: Acegi Security System for Sprin
    Think you know all there is to know about using the Acegi Security System for Spring? We challenge you to test your skills by taking our latest quiz....
  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...

Posted by Dion Almaer at 11:42 pm
6 Comments

+++--
3.4 rating from 40 votes

6 Comments »

Comments feed TrackBack URI

FONT tags? Please. Aren’t they illegal yet?

Comment by henrah — August 21, 2006

and this is special why…

Comment by exilian — August 21, 2006

I’m somewhat disappointed by this client-side code. I guess it’s just an example, but font tags and use of substr as opposed to returning JSON or XML formatted data seems kind of weak. I guess it may not be what’s of primary importance, but I figure if you’re gonna show an example, try to do everything as elegantly as possible in all parts of the code.

Comment by Thomas Messier — August 21, 2006

I agree that the client side code is not the most elegant or complete, but the point of the blog entry was to demonstrate how to implement an Ajax login when web containers redirect on failed or successful login which normally forces a browser refresh. The emphasis was on the server side handling and leave it up to the user to implement a fontend that meets their needs.

I wanted to keep the client code as simple as possible leaving it up to the user to use the techniques illustrated to build their own login screen. The sample also uses a simple hidden div to show the login dialog, something which I would never use for a real app. I would rather use a Dojo dialog for instance. Also the user can in their client code make another Ajax request and refresh a “body div” instead of simply doing location.href = msg.substring(4, msg.length);

Comment by Sanjiv Jivan — August 21, 2006

I agree that the client side code is not the most elegant or complete, but the point of the blog entry was to demonstrate how to implement an Ajax login when web containers redirect on failed or successful login which normally forces a browser refresh. The emphasis was on the server side handling and leave it up to the user to implement a fontend that meets their needs.

I wanted to keep the client code as simple as possible leaving it up to the user to use the techniques illustrated to build their own login screen. The sample also uses a simple hidden div to show the login dialog, something which I would never use for a real app. I would rather use a Dojo dialog for instance. Also the user can in their client code make another Ajax request and refresh a “body div” instead of simply doing location.href = new url.

Comment by Sanjiv Jivan — August 21, 2006

“Ajax login when web containers redirect on failed or successful login which normally forces a browser refresh.”

And i thought ajax had something to do with apple pie.

Comment by rudy — August 21, 2006

Leave a comment

You must be logged in to post a comment.