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.
- Login
- Login – Error
- Login – Success
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!
References:



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?
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.
Thanks. I got it (locally anyway). I just need to learn more about spring security.
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//!!
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!
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.
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.
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
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?
Is it possible to do a cross-domain login using the Sencha Touch and Spring Security?
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?
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' ... ... });Hey man.
Great example but I am new to this….
Can you please tell me where is the user/password for login successfully?
Thanks
hi, it is in the applicationContext-security.xml. the handler is LoginSuccessHandler.java. the pwd should be password.
Hi, how do you validate that the user has not autenticated in the dashboard panel. In example if I access directly to the url of the dashboard?
Never mind I just found that your are using security framework in spring
”
“