Sunday, July 28, 2013

How to integrate Spring Security to an existing web application?

Let see how we can integrate a Spring Security to an existing web application. Download the Spring Security from here, from the download add the following jar spring-security-core-3.2.0.M2.jar and spring-security-config-3.2.0.M2.jar from \spring-security-3.2.0.M2\dist, to your project lib folder.

Since this is the existing project, i assume the all the url pattern will be routed through Spring Dispatcher Servlet. Now let's modify our web.xml to add the filter, since spring security uses filter approach, this provides a hook into the Spring Security web infrastructure.





Next we need to create applicationContext-security.xml under WEB-INF





The element http in the applicationContext-security.xml, says that we want all URLs within our application to be secured, requiring the role ROLE_USER to access them. Thus we are forcing to authenticate ourselves when visiting our pages in the site.

Now to add some test users for a rapid development, we have configured two users here ramanujam and user1, with their password and roles using the authentication-provider and user-service. That's all you have integrated a simple security to your web application. Now if you access any of your web pages, you will redirecting to a default spring login pages.

Happy Programming...!!!

Saturday, July 13, 2013

Remot Sort : How to override SortParameters in Ext-Js Grid Panel?

When you sort using the gird, typically clicking on the column header, the dataIndex of the column will be passed as the sort properties, but not always the server will understand the grid name. for example, lets take below example.

Below is the Snap shot of the Activity Model
Ext.define('Activity', {
    extend: '',
    uses: [ActivityType, ActivityStatus]
    fields: [
        {name: 'activityTypeId', type: 'int'},
        {name: 'activityStatusTypeId', type: 'int'},
        {name: 'accountId', type: 'int'},
        {name: 'dueDate', type: 'date', dateFormat: 'm/d/Y'},

The Relevant Java Model for the Activity
public class Activity {
  @ManyToOne(Fetch = FetchType.EAGAR)
  @JoinColumn(name = "activity_type_id")
  private ActivityType activityType;

  @ManyToOne(Fetch = FetchType.EAGAR)
  @JoinColumn(name = "activity_status_type_id")
  private ActivityStatusType activityStatusType;

  @ManyToOne(Fetch = FetchType.EAGAR)
  @JoinColumn(name = "account_id")
  private Account account;

  @DateTimeFormat(pattern = "MM/dd/yyyy")
  private Date dueDate;

If you look at the variable names in Java Bean and Ext Model, you can see the difference. So when you are passing the sort parameters to your service call through, it needs to know the exact column name for example, Lets take activityStatusTypeId. If you click on the Column for Sort based on the activityStatsTypeId, the grid will pass on the dataIndex name as the property to the server. If you look at the FireBug console for the parameters, you see like this.

sort [{"property":"activityStatusTypeId", "direction":"DESC"}]

When you pass activityStatusTypeId to the service, really my server doesn't understand what activityStatusTypeId to the activity table. So we need to pass the extact column name as the sort property to make my service understand. To override the default parameters, we need to override getSortParam() in the colum config.

Ext.define('ActivitiesGridPanel', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.activitiesGridPanel,
    initComponent: function() {
       var me = this;
       Ext.applyIf(me, {
           { xtype: 'gridcolum',
             dataIndex: 'activityStatusTypeId',
             text: 'Status',
             getSortParam: function() {
                 return 'activity_status_type_id';
           { xtype: 'datecolumn',
             dataIndex: 'activityStatusTypeId',
             text: 'Due Date'
           { xtype: 'gridcolum',
             dataIndex: 'activityTypeId',
             text: 'Type'

After overriding the sortParameter for the column using getSortParam() function, if you click on the column in the gird, instead of passing the dataIndex name, it will sent out the overridden string as the sort parameter. You see the parameter in FirBug

sort [{"property":"activity_status_type_id", "direction":"DESC"}]

Happy Programming ...!!!