Home > Sencha Touch, Spring Security > Integrating Sencha Touch Login Form with Spring Security

Integrating Sencha Touch Login Form with Spring Security

Previously, I wrote a tutorial on integrating Spring Security and ExtJS login form. Now I am integrating the same Spring Security code with the Sencha Touch login form. Here I will cover the implementation of the form only and 2 use cases; login & logout. The sample code is based on Sencha Touch 1.1.0 and the foloder structure is following the Sencha Touch MVC.

First, lets see the login form and controller. The form has 2 fields, login id & password. When you click the login button, the form is submitted to http://<ownurl>/j_spring_security_check (the defLoginUrl in line 4 is defined in login.jsp).

 Just a note, if you are using Sencha Touch 1.0 then you will need to implement a listener within the form to catch the “submit” and “exception” event. This is because there is a minor defect in the Ext.form.FormPanel submit function and it caused the form to ignore “success” & “failure” function in the submit.

test.views.LoginForm = Ext.extend(Ext.form.FormPanel, {
   scroll: 'vertical',
   fullscreen: true,
   url: defLoginUrl,
   items: [{
      xtype: 'textfield',
      label: 'Login',
      name: 'j_username'
   },{
      xtype: 'passwordfield',
      label: 'Password',
      name: 'j_password'
   }],
   dockedItems: [{
      xtype: 'toolbar',
      dock: 'bottom',
      items: [{
         text: 'Reset',
         handler: function() {
            test.views.loginForm.reset();
         }
      },{
         text: 'Login',
         ui: 'confirm',
            handler: function() {
               Ext.dispatch({
                  controller: test.controllers.loginController,
                  action: 'submit',
                  data: test.views.loginForm
               });
             }
      }]
   }],
   initComponent: function() {
      test.views.LoginForm.superclass.initComponent.apply(this, arguments);
   }
});

 

test.controllers.loginController = new Ext.Controller({
   submit: function(options) {
      var theForm = options.data;
      test.views.loginForm.submit({
         method: 'POST',
         waitMsg: {
            message: 'Processing',
            cls : 'demos-loading'
         },
         scope: this,
         success: function(form, response) {
            window.location = '/';
         },
         failure: function(form, response) {
            Ext.Msg.alert('Warning', response.errorMessage);
         }
      });
   }
});

Depending on the result of the authentication the server side will return a JSON data. The format is

{"username":null,"errorMessage":"Login failed. Try again.","loggedIn":false,"success":false}

Once authenticated, you will see the main page with a logout button. The code is shown below.

test.views.Dashboard = Ext.extend(Ext.Panel, {
   fullscreen: true,
   dockedItems: [{
      xtype: 'toolbar',
      title: 'Dashboard',
      dock: 'top',
      items: [{
         xtype: 'button',
         text: 'Logout',
         ui: 'confirm',
         handler: function() {
            Ext.dispatch({
               controller: test.controllers.dashboardController,
               action: 'logout'
            });
         },
         scope: this
      }]
   }],
   html: '<p>Welcome!</p>',
   initComponent: function() {
      test.views.Dashboard.superclass.initComponent.apply(this, arguments);
   }
});

 

test.controllers.dashboardController = new Ext.Controller({
   logout: function(options) {
      window.location = '/j_spring_security_logout';
   }
});

Hope that is useful. Happy coding!

view demodownload source 

References:

Advertisement
  1. Jason
    July 17, 2011 at 5:54 pm | #1

    Very nice example. Thanks for all of your posts! They have been the best examples I’ve found while trying to learn Sencha Touch. I’m not familiar with Spring Security at all so could you elaborate on what is need to test your example code either locally or on my server?

    • wowi
      July 18, 2011 at 12:00 pm | #2

      thanks. to run locally you will need a java appserver. for simplicity, I suggest you download the spring sts (http://www.springsource.com/products/eclipse-downloads).
      After you install, import the code to the ide. File -> Import -> select ‘Existing Projects into Workspace’. Once the project loaded into the workspace, follow the Step 4 & 5 in http://wp.me/pIacA-7u to run it.

      If you are not running on java, you can just extract the javascript. it is pretty independent of the spring security. anyway let me know if you have any problem running it on ur desktop.

  2. Jason
    July 20, 2011 at 2:35 pm | #3

    Thanks. I got it (locally anyway). I just need to learn more about spring security.

  3. kprk
    July 22, 2011 at 2:51 pm | #4

    Hi wowi..!!
    Thanska mill for your such a efforts. I did my same login screen in a different way, but this is totally new to me//!!

  4. August 6, 2011 at 2:27 pm | #5

    Hi, thanks a lot! great article. Do you know what happen when you try to use this form localy and submit to a https login url? It is not working and that is very dificult to fix… could you help me? thanks a lot!

  5. Prachi
    August 22, 2011 at 11:04 am | #6

    I downloded the example and tried but I am getting required resource not found error. so can you please give complete demo for this application.

    • wowi
      August 22, 2011 at 1:00 pm | #7

      do u hv the detail error? is it compile time or runtime? the code is available at https://github.com/wswijaya/spring-mvc-mlogin , by right maven will take care of the dependency. let me know if you still unable to get it run. i will zip my local project instead.

  6. imwill
    September 19, 2011 at 8:45 pm | #8

    How could I solve this problem if I want to access from another domain?
    XMLHttpRequest cannot load http://dev.domain.tld/login.php. Origin http://192.168.178.31 is not allowed by Access-Control-Allow-Origin

    • wowi
      September 19, 2011 at 11:16 pm | #9

      Hi, the error you encountered related to cross-domain Ajax. You can try JSOP for cross-domain. Which part of the function are you trying to cross-domain?

      • alex
        September 20, 2011 at 8:45 pm | #10

        Is it possible to do a cross-domain login using the Sencha Touch and Spring Security?

  7. October 7, 2011 at 6:43 pm | #11

    How would you place the username on the page, once you are logged in? For example is there some conditional logic you can put on a Ext.Panel that would put the username, logout button there only if you are logged in?

    • wowi
      October 8, 2011 at 1:09 am | #12

      Hi,
      you can put the logout and name and the conditional logic in a DIV tag

      ... ...

      then in the Ext.Panel specify the contentEl. Ext.Panel ({ contentEl: 'logout-panel' ... ... });

  8. Hectorlongarte
    December 2, 2011 at 3:46 pm | #13

    Hey man.
    Great example but I am new to this….

    Can you please tell me where is the user/password for login successfully?
    Thanks

    • wowi
      December 3, 2011 at 1:34 am | #14

      hi, it is in the applicationContext-security.xml. the handler is LoginSuccessHandler.java. the pwd should be password.

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.