Sunday, August 20th, 2006

Ajax Login with Acegi

Category: Java

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

Posted by Dion Almaer at 11:42 pm
6 Comments

+++--
3.4 rating from 45 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.