Sunday, April 22, 2012

WS Security with Apache Rampart - Part 2/3 (Generating Stub for the Service)

Overview
This is part TWO of an article series that looks at how you can engage Apache Rampart to secure Web Service communication using Username Authentication. This post will focus on how we can generate the Web Service stub to access the secured Web Service.

Also Refer to,
WS Security with Apache Rampart - Part 1/3 (Securing the Service) and,
WS Security with Apache Rampart - Part 3/3 (Securing the Client)

Sample code for this post can be found here.

This article applies to:
Version 1.6.0
Apache Axis2 1.6.0 and Apache Rampart 1.6.0

Version 1.6.1
Apache Axis2 1.6.1 and Apache Rampart 1.6.1

Project Structure: Following is the structure I have used for this post.


Step 1: Maven Dependencies (pom.xml)

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
   <modelVersion>4.0.0</modelVersion>  
   <groupId>org.fazlan</groupId>  
   <artifactId>org.fazlan.secureservice.stub</artifactId>  
   <version>1.0.0</version>  
   <packaging>jar</packaging>  
   <name>org.fazlan.secureservice.stub</name>  
   <url>http://maven.apache.org</url>  
   <properties>  
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
     <axis2.version>1.6.1</axis2.version>  
     <axiom.version>1.2.12</axiom.version>  
   </properties>  
   <build>  
     <plugins>  
       <plugin>  
         <groupId>org.apache.axis2</groupId>  
         <artifactId>axis2-wsdl2code-maven-plugin</artifactId>  
         <version>${axis2.version}</version>  
         <executions>  
           <execution>  
             <goals>  
               <goal>wsdl2code</goal>  
             </goals>  
           </execution>  
         </executions>  
         <configuration>  
           <packageName>org.fazlan.secureservice.stub</packageName>  
           <wsdlFile>src/main/resources/EmployeeSecureService.wsdl</wsdlFile>  
           <namespaceToPackages>http://service.secureservice.fazlan.org=org.fazlan.secureservice.service</namespaceToPackages>  
           <unpackClasses>true</unpackClasses>  
           <unwrap>true</unwrap>  
           <syncMode>sync</syncMode>  
         </configuration>  
       </plugin>  
     </plugins>  
   </build>  
   <dependencies>  
     <dependency>  
       <groupId>org.apache.axis2</groupId>  
       <artifactId>axis2</artifactId>  
       <version>${axis2.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.ws.commons.axiom</groupId>  
       <artifactId>axiom-api</artifactId>  
       <version>${axiom.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.ws.commons.axiom</groupId>  
       <artifactId>axiom-impl</artifactId>  
       <version>${axiom.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>wsdl4j</groupId>  
       <artifactId>wsdl4j</artifactId>  
       <version>1.6.2</version>  
     </dependency>
   </dependencies>  
 </project>  

Step 2: Generating the Service Stub
You can access the service via http://localhost:8080/axis2/services/EmployeeSecureService?wsdl

And you should see the policy attached to the WSDL.
Save this WSDL file as EmployeeSecureService.wsdl under the resources folder
 e.i: org.fazlan.secureservice.stub/src/main/resources/EmployeeSecureService.wsdl Employee.java


Step 3: Building the Project
 mvn clean install

After executing the above goals, the service stub jar will be created under the target folder.
e.i: org.fazlan.secureservice.stub-1.0.0.jar

Summary:
This article looked at how you can secure your web service using Apache Rampart using username authentication.


Also Refer to,
WS Security with Apache Rampart - Part 1/3 (Securing the Service) and,
WS Security with Apache Rampart - Part 3/3 (Securing the Client)

Sample code for this post can be found here.

1 comment:

  1. I've imported your code directly into eclipse and i got 1 error in pom.xml. Only thing that i have changed is is this 1.6.2
    1.2.13

    I get error :

    Plugin execution not covered by lifecycle configuration: org.apache.axis2:axis2-wsdl2code-maven-plugin:1.3:wsdl2code (execution: default, phase: generate-sources).

    How can i resolve this, pls

    ReplyDelete