Tuesday, March 15, 2011

Getting started with Spring Security 3 with JBoss and MySQL

Hi Folks,

Lets get started.

You need to download,

1. Spring Security Module 3
2. MySQL 5.1
3. JBoss 4.2 GA


I'm using eclipse as the IDE.

Create a simple Web Project in eclipse IDE,

Among the jar files of the Spring Security module you will see a 2 WAR files. open the WAR file spring-security-samples-tutorial-3.0.5.RELEASE.war with WinZIP or WinRAR like tool and copy all the JAR files there to the lib folder of the project.

Add all the copied JAR files to buildpath.

First of all you need to create a login page for your web application. create a simple JSP file and name it as login.jsp and paste the following code fragment there.


<form action="/test/j_spring_security_check" method="post"> 
Username : <input type="text" name="j_username" id="j_username">
Password : <input type="password" name="j_password" id="j_password">
<input type="submit" value="Submit">
</form>

Remember that,

1. j_spring_security_check is the URL you need to call as the action (/test is the context root of your web application).

2. Name of your username and the password fields should be j_username and j_password

Once the user been authenticated, we need to forward the user to a different page. so lets create another jsp file for that and name it as user_authenticated.jsp

You are going to authorize the user according to their user roles, so here I have created separate JSPs to show some user role level authorization. my user roles are,

1. ROLE_SUPERVISOR (supervisor.jsp)
2. ROLE_USER (user.jsp)
3. ROLE_TELLER (teller.jsp)

Now let's configure Spring.

You will need 2 xml files to hold Spring Security info and the Datasource info to the MySQL DB. See below,

1. applicationContext-security.xml (Your Spring Security configuration file)
2. applicationContext-db.xml (Your datasource configuration file)

Create above 2 files in your WEB-INF directory.

applicationContext-security.xml

Simply you can copy the same file from the tutorial war file that I have mentioned above, and modify its contents as I have described below.

<http use-expressions="true">
<intercept-url pattern="/user_authenticated.jsp" access="isAuthenticated()" />
<intercept-url pattern="/supervisor.jsp" access="hasRole('ROLE_SUPERVISOR')" />
<intercept-url pattern="/teller.jsp" access="hasRole('ROLE_TELLER')" />
<intercept-url pattern="/user.jsp" access="hasRole('ROLE_USER')" />
<intercept-url pattern="/**"  access="permitAll" />
<form-login login-page='/login.jsp'/>
<logout />
<remember-me />
<!--

Uncomment to enable X509 client authentication support

<x509 />

-->

<!-- Uncomment to limit the number of sessions a user can have -->
<session-management invalid-session-url="/timeout.jsp">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
</session-management>
</http> 

<authentication-manager>
<authentication-provider>
<password-encoder hash="md5"/>
<jdbc-user-service data-source-ref="mysqlds"/>
</authentication-provider>
</authentication-manager>

<intercept-url pattern="/user_authenticated.jsp" access="isAuthenticated()" />

This is the JSP (user_authenticated.jsp) that we want to forward the user after getting authenticated, that will tell by this: access="isAuthenticated().

<intercept-url pattern="/supervisor.jsp" access="hasRole('ROLE_SUPERVISOR')" />

This will tell the Spring security module to authorize this JSP page access only to the users who are having ROLE_SUPERVISOR role.


Within the <authentication-manager> tag you will enter all the details for authenticating a particular user.

Passwords will be encoded with md5 algorithm. (<password-encoder hash="md5"/>).

This will point the datasource to the DB where the user credentials are stored:

<jdbc-user-service data-source-ref="mysqlds"/>

mysqlds is the datasource name.

applicationContext-db.xml

This Spring configuration file is to configure the datasource lookup. (here im assuming that your JBoss installed on your local machine)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">


<bean id="mysqlds" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/mysqlds</value>
</property>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
<prop key="java.naming.provider.url">jnp://localhost:1099</prop>
</props>
</property>
</bean>
</beans>

Since you are going to deploy the application JBoss application server, you need to create another jboss-web.xml file with datasource information.

jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<resource-ref>
<res-ref-name>jdbc/mysqlds</res-ref-name>
<jndi-name>java:/mysqlds</jndi-name>
</resource-ref>
</jboss-web>

Finally you need to wire all the things with web.xml

web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-security.xml
/WEB-INF/applicationContext-db.xml
</param-value>
</context-param>

Here we need to specify where are our Spring security configuration file and the datasource configuration file.

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class> org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Finally with the datasource information,

<resource-ref>
<res-ref-name>jdbc/mysqlds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


Spring assumes that your database contains particular tables in default. so you need to create those tables in order to follow the default way of Spring.

Create those tables from below script.
create table users(
      username varchar(50) not null primary key,
      password varchar(50) not null,
      enabled boolean not null);

  create table authorities (
      username varchar(50) not null,
      authority varchar(50) not null,
      constraint fk_authorities_users foreign key(username) references users(username));
      create unique index ix_auth_username on authorities (username,authority);

Now you need to create the datasource file in JBoss and configure it.

Go to JBOSS_HOME\server\default\deploy. Get a copy of the existing hsqldb-ds.xml file and name it as mysqldb-ds.xml and modify its contents as below.

mysqldb-ds.xml


   
      
      mysqlds

      jdbc:mysql://localhost:3306/test 
      com.mysql.jdbc.Driver
      root
      root
      5
      20
      0

   



save the file to the deploy folder of the JBoss and restart the server. (dont forget to put some values in the tables, since we are encoding passwords with md5 you need to put the encrypted passwords in the table.

Use this site to do that.

http://md5.gromweb.com/

You are done now.

Right click on the eclipse project and export it as a WAR file and copy it to the JBoss deploy folder, and access the URL http://localhost:8080/test/user_authenticated.jsp (i have used test as the WAR file name)

Now your eclipse project structure should look like this,

Tuesday, March 1, 2011

Hello World with Java Architecture for XML Binding - JAXB

What is JAXB?

JAXB (Java Architecture for XML Binding) facilitates you to read or write to a XML file. since XML involves with Java everywhere, It will be very useful when working with Java. Here I'm assuming you are having some knowledge on XML.

What you should have

1. JAXB 

Download it and add its bin  folder to your classpath.

I have used eclipse as the IDE.

Let's start...

1. Create a project in eclipse called JAXB.
2. Create the library  directory and copy all the JAXB jars there. (lets call its "lib")
3. Add lib to the build path.
4. You should have a XML schema definition file, so let's create one. Let's name it as GSCProfile.xsd
5. Copy following content to the schema file.

GSCProfile.xsd


<schema xmlns="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.example.org/GSCProfile" 
xmlns:tns="http://www.example.org/GSCProfile" elementFormDefault="qualified">


<complexType name="GSC">
<sequence>
<element name="Name" type="string"></element>
<element name="Rating" type="int"></element>
</sequence>
</complexType>

<complexType name="Profile">
<sequence>
<element name="ProfileName" type="string"></element>
<element name="GSCElements" type="tns:GSC" maxOccurs="14" minOccurs="14">
</element>
</sequence>
</complexType>

<complexType name="GSCProfiles">
<sequence>
<element name="Profile" type="tns:Profile" maxOccurs="unbounded" minOccurs="1">
</element>
</sequence>
</complexType>

</schema>
There are 3 complex types called,
1.GSCProfiles
2.Profile
3.GSC
GSCProfiles is the root of all. here how is the relationship goes, 

GSCProfiles  ---< Profile ---< GSC

Once you have added the bin folder to the classpath,  go to the src folder of your project from command line and issue the following command,

xjc -d binding -p com.jaxb.gsc GSCProfiles.xsd


This will generates several classes in the above mentioned package (com.jaxb.gsc) one class for each complex type and an object factory and a package info class.

create another class called JAXBTest  in a diffrernt package, lets name that as com.jaxb.test

JAXBTest.java

Create this variable at class level. and variable to hold the xml file name.
private static Map<String, Map<String, String>> gscProfileMap = new HashMap<String, Map<String, String>>();
/**
* Output xml file name.
*/
private static File xmlFile = new File("xml/GSCProfile.xml");
 
1. create a method called saveGSCProfiles and add following code fragments there. this method will save the content for it.
public static void saveGSCProfiles() {
try {
Map<String, String> gscValueMap = new HashMap<String, String>();

// Adding GSC Elements

gscValueMap.put("Data Communications", "0");
gscValueMap.put("Distributed Data Processing", "0");
gscValueMap.put("Performance", "0");
gscValueMap.put("Heavily Used Configuration", "0");
gscValueMap.put("Transaction Rate", "0");
gscValueMap.put("Online Data Entry", "0");
gscValueMap.put("End User Efficiency", "0");
gscValueMap.put("Online Update", "0");
gscValueMap.put("Complex Processing", "0");
gscValueMap.put("Reusability", "0");
gscValueMap.put("Installation Ease", "0");
gscValueMap.put("Operational Ease", "0");
gscValueMap.put("Multiple Sites", "0");
gscValueMap.put("Facilitate Change", "0");
gscProfileMap.put("ProfileName", gscValueMap);
Set<String> pofileNames = gscProfileMap.keySet();

ObjectFactory factory = new ObjectFactory();

GSCProfiles gscProfiles = factory.createGSCProfiles();

for (String profileKey : pofileNames) {

Profile profile = factory.createProfile();

// Setting profile name.

profile.setProfileName(profileKey);
gscValueMap = gscProfileMap.get(profileKey);
Set<String> gscSet = gscValueMap.keySet();

for (String gscKey : gscSet) {

GSC gsc = factory.createGSC();
gsc.setName(gscKey);
gsc.setRating(Integer.parseInt(gscValueMap.get(gscKey)));
profile.getGSCElements().add(gsc);
}

gscProfiles.getProfile().add(profile);

}

JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.gsc");

// Creating marshaller.
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

FileOutputStream outputStream = new FileOutputStream(xmlFile);

// Marshalling object in to the XML file.

marshaller.marshal(gscProfiles, outputStream);

} catch (JAXBException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
 
 
 2. Create another method to read from the XML file. let's name this as loadGSCProfiles

public static void loadGSCProfiles() {
try {

JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.gsc");

// Create unmarshaller.
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 

GSCProfiles gscProfiles = (GSCProfiles) unmarshaller.unmarshal(xmlFile);

// Retrieving list of profiles.
List<Profile> profile = gscProfiles.getProfile();

for (Profile profile2 : profile) {

Map<String, String> gscValueMap = new HashMap<String, String>();

List<GSC> gsc = profile2.getGSCElements();

for (GSC gsc2 : gsc) {
gscValueMap.put(gsc2.getName(), gsc2.getRating() + "");
}

gscProfileMap.put(profile2.getProfileName(), gscValueMap);
}
} catch (JAXBException e) {
e.printStackTrace();
}

Create a directory inside your eclipse project and name it as xml, and create a xml file called  GSCProfile.xml

Write the main method in JAXBTest class and execute,

   
 public static void   main(String[] args)   {
             loadGSCProfiles();

             saveGSCProfiles();
    }

Open your GSCProfile.xml file and see... :)

Finally your eclipse project directory structure should like this,


Tuesday, February 22, 2011

Hello World with Apache Commons Digester

This example will shows how to get started with Apache Commons Digester.

What is Apache Commons Digester?

Simply, you can use this to read a XML file against a particular pattern (say, particular tag in XML file) and if it found, you can trigger some actions (say, a method, so that method will be called if that pattern is matched)

So Let's start then,

You shoud have JDK 1.5 installed and need to download several libraries, which are,

1. Apache Commons Digester Library.
2. Apache Commons Logging.
3. Apache Commons BeanUtils.

I'm using eclipse as the IDE, so here we go,

Create a Java project from eclipse and add those downloaded libraries to that and set the build path correctly.
 
Create a XML file with following contents,




    
        honda
        HR-V
        1590CC
        vtec
        5
        silver
    
    
        toyota
        Prado
        3000CC
        vvt-i
        5
        silver
    


Create a Java class with a main method, Let's call this as DigesterTest.java

add following method there,
    
/**
     * Action method. This will be called if there any matched content.
     *
     * @param vendor
     *            Value of the tag "vendor".
     * @param model
     *            Value of the tag "model".
     * @param enginecapacity
     *            Value of the tag "enginecapacity".
     * @param enginetype
     *            Value of the tag "enginetype".
     * @param doors
     *            Value of the tag "doors".
     * @param color
     *            Value of the tag "color".
     */
    public void getVehicleInfo(String vendor, String model,
        String enginecapacity, String enginetype, String doors,
        String color) {
    System.out.println("Vendor : " + vendor);
    System.out.println("Model : " + model);
    System.out.println("Engine Capacity : " + enginecapacity);
    System.out.println("Engine Type : " + enginetype);
    System.out.println("Doors: " + doors);
    System.out.println("Color : " + color);
    }



   
/**
     * Creates certain patterns to check against with XML file, if there a
     * match, action method will be called.
     *
     * @throws SAXException
     * @throws IOException
     */
    public void run() throws IOException, SAXException {

    Digester digester = new Digester();
    digester.push(new DigesterTest());

    digester.addCallMethod("vehicles/suv", "getVehicleInfo", 6);
    digester.addCallParam("vehicles/suv/vendor", 0);
    digester.addCallParam("vehicles/suv/model", 1);
    digester.addCallParam("vehicles/suv/enginecapacity", 2);
    digester.addCallParam("vehicles/suv/enginetype", 3);
    digester.addCallParam("vehicles/suv/doors", 4);
    digester.addCallParam("vehicles/suv/color", 5);

    InputStream inputStream = DigesterTest.class.getClassLoader()
        .getResourceAsStream("vehicles.xml");
    digester.parse(inputStream);

    }

    /**
     * Main method.
     *
     * @param args
     * @throws IOException
     * @throws SAXException
     */
    public static void main(String[] args) throws IOException, SAXException {
    DigesterTest digesterTest = new DigesterTest();
    digesterTest.run();

    }


Here is the directory structure of my eclipse project,


Right click on DigesterTest.java and point to Run as >  Java Application.

Wednesday, December 8, 2010

Securing Web Services using Apache Rampart - Transport Level Security

Note that you will find many articles about this topic, but I'm going to show you how to do this in WebSphere Application Server 6.1.0.27.

Apache Rampart
Apache Rampart acts as the security module for Axis2. You can use Rampart for securing web services by enforcing authentication, integrity and confidentiality.
You can download it from here.
Download it and copy it's JAR file to the WEB-INF/libs  directory of your web application and copy the modules directory in the Rampart distribution to WEB-INF directory of you web application.
We will create a simple Web Service and secure it using Apache Rampart. Let's write the service implemetation class.
service class
package com.test;
public class Calculator {
 public int addService(final int a, final int b) {
  return a + b;
 }
}
Now, Let's write the services.xml, which tells Axis what is your service class and what are the methods you are going to expose through the web service.

services.xml

    
        com.test.Calculator
    
    
 



Password callback class 

Since we are going to use password callback class to verify the client provided username and the password, we should implement a password callback class.

package com.test;
import org.apache.ws.security.WSPasswordCallback;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.io.IOException;
public class PWCBHandler implements CallbackHandler {
    public void handle(Callback[] callbacks) throws IOException,
            UnsupportedCallbackException {
        for (int i = 0; i < callbacks.length; i++) {
            //When the server side need to authenticate the user
            WSPasswordCallback pwcb = (WSPasswordCallback)callbacks[i];
            // For plain text password scenarios
            if (pwcb.getUsage() == WSPasswordCallback.USERNAME_TOKEN_UNKNOWN) {
                if (pwcb.getIdentifier().equals("kushanxp")
                        && pwcb.getPassword().equals("kushan"))
                {
                    //If authentication successful, simply return
                    return;
                } else {
                    throw new UnsupportedCallbackException(callbacks[i], "check failed");
                }
            // For digested password scenarios
            } else if (pwcb.getUsage() == WSPasswordCallback.USERNAME_TOKEN ) {
                if (pwcb.getIdentifier().equals("kushanxp"))
                {
                    //We simply set the password, Username Token processor verifies the digest
                    pwcb.setPassword("kushan");
                    return;
                }
            }
        }
    }
}
Now we have to add policy based information to the services.xml,
services.xml



    
    com.ws.service.Calculator
    
    
       
    
    
        
            
                
                    
                        
                            
                                
                            
                        
                       
                            
                                
                            
                        
                        
                            
                                
                            
                        
                        
                    
                

                
                    
                       
                    
                

                
                     com.test.PWCBHandler
                    
                
            
        
    









Note that you can find the policy based configurations from    <wsp:Policy> to </wsp:Policy> and you have to mention your password callback class in the                  <ramp:passwordCallbackClass>      com.test.PWCBHandler </ramp:passwordCallbackClass> Engaging Rampart You may notice that I have added <module ref="rampart"/> in top of the services.xml file, This will tell Axis2 that we going to engage Rampart with our web service. Note that this is local to this service, if you want to engage Rampart globally to all of your services, then you have to add <module ref="rampart"/> this line to axis2.xml file. Now you are almost complete, Let's create a WAR file with our service and deploy in on WebSphere Application Server. Your WAR file directory structure should be as follows, (Assume your WAR file name is calculator.war) calculator.war  Now we have done all the coding for the service level, now it's time to move to client end. Client Implementation  

client.java
 

package com.ws.client;

import java.io.FileNotFoundException;
import java.rmi.RemoteException;

import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.neethi.Policy;
import org.apache.neethi.PolicyEngine;
import org.apache.rampart.RampartMessageData;

public class Client {

    /** * @param args */
    public static void main(String[] args) {
 try {
     System.setProperty("javax.net.ssl.trustStore", "keys/service.jks");
     System.setProperty("javax.net.ssl.trustStorePassword", "kushan");
     ConfigurationContext ctx = ConfigurationContextFactory
      .createConfigurationContextFromFileSystem("repo", null);
     CalculatorStub stub = new CalculatorStub(ctx,
      "https://localhost:8443/axis2/services/Calculator");
     ServiceClient client = stub._getServiceClient();
     client.engageModule("rampart");
     Options options = client.getOptions();
     options.setUserName("kushanxp");
     options.setPassword("kushan");

     options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,
      loadPolicy("resources/policy.xml"));

     int a = 3;
     int b = 4;
     int result = stub.add(a, b);
     System.out.println(a + " + " + b + " = " + result);
 } catch (AxisFault e) {
     e.printStackTrace();
 } catch (RemoteException e) {
     e.printStackTrace();
 }
    }

    /**
     *
     * Loads the policy.
     *
     *
     *
     * @param xmlPath
     *
     * @return
     */

    private static Policy loadPolicy(String xmlPath) {

 StAXOMBuilder builder = null;

 try {

     builder = new StAXOMBuilder(xmlPath);

 } catch (FileNotFoundException e) {

     e.printStackTrace();

 } catch (XMLStreamException e) {

     e.printStackTrace();

 }

 return PolicyEngine.getPolicy(builder.getDocumentElement());

    }
}

Tuesday, December 7, 2010

Embedding Axis2 into an Existing Application

Many of you may came up with implementing Web Services for existing application, here I have done it using Apache Axis 2, So here what you need to do,

You should edit your web.xml file found in WEB-INF directory, and there you have to configure Axis specific information, (You must configure AxisServlet there, so when the application deploying server will identify that Axis2 is plugged in to this application)

web.xml

    
        Apache-Axis Servlet
        AxisServlet
        
            org.apache.axis2.transport.http.AxisServlet
        
        1
    

Now add servlet mapping,
    
        AxisServlet
        /services/*
    

This will tell the Axis Servlet that all your Web Services will include in a directory called services, So the service URL's must have services text within their URL's

If you do not wish to add your web services in a directory called services, you can change this to something else,

i.e :
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/serviceapps/*</url-pattern>
    </servlet-mapping>


And you need to tell axis2.xml that where are your services,

axis2.xml
serviceapps
note

Axis2 assumes that all your service implementations are in a directory called services, in WEB-INF folder. so if you are not changing that, you don't have to modify axis2.xml file.

Thats all you have to do, Remember to copy all the JAR files in Axis2 distribution to your WEB-INF/lib directory :)

So when the application deploying in the server, it will initialize the AxisServlet and loads the web service implementations from default locations,

Default Service Location

WEB-INF/services

Default Module Location

WEB-INF/modules

Default Axis2 XML Location

WEB-INF/conf

Directory structure.