PDA

View Full Version : SpringSecurity Failed. Return False to JS



funnyfox
16 Oct 2012, 9:38 AM
I am using Spring security. I have a ext-js method which calls Java method.

Js method

testMethod: function(idpass, cb, scope){
this.onAjaxResponse = Ext.bind(this.onAjaxResponse, scope || window, [cb, scope], true);
Ext.Ajax.request({
url : 'remote/employee/testMethod/'+idpass,
success: this.onAjaxResponse,
failure: this.onAjaxResponse
});
app.loadMask.show();
}


The above method is called from a DetailPanel when a button is pressed.
DetailPanel

submitClicked: function(dat){
this.clearMessage();
app.employeeService.testMethod('name:bill', this.onSubmitClicked, this);
}
},


onSubmitClicked: function(data){
if(!data.success){
this.updateError(data.errorMessage);
}else{
this.updateMessage('Action Successful');
}
}


Java File.


@PreAuthorize("hasAnyRole('ADMIN',)")
@RequestMapping(value = "/testMethod/{name}", method = {RequestMethod.POST,RequestMethod.GET })
public String testMethod(@PathVariable("name") String name, HttpSession session, ModelMap model){
//some logic..
return returnJson(model, response);
}



Now the Java Method is not executed for USER role.
How can I capture this and display an Alert to user that Access is Denied for the current user ?

mitchellsimoens
22 Oct 2012, 8:58 AM
What is onAjaxResponse doing? Are you passing the callback to onSubmitClicked or wherever you want to handle the failure?

If you are getting a failure in Java you can change the http status code to some fail code like 500 or something and that will trigger the failure callback

funnyfox
22 Oct 2012, 10:22 AM
I figured it out, such an easy way, I got it done myself.
Anyhow Thanks for your time.

this.onAjaxResponse. I wrote an alert in this method. Access Denied.
Thats cool.

Thank you.