[Download] | [Documentation Home] | [Release Note]
When deploying a data service, we may have the need to secure it, by requiring user authentication, and maybe encryption and the usage of signatures. This can be fullfilled using WSO2 Data Services Server by applying security properties directly to the data service. In this sample, we will be showing how a service client is used to access a secured data service.
The sample data service "SecureDataService" should be deployed as per the instructions mentioned in Deploying Samples section.
From the service list in the console, select the "SecureDataService" data service and goto the service dashboard. Under the "Quality of Service Configuration", click on "Security". In the next screen, set "enable security" to "true" and select "UsernameToken" in the basic scenario. Here we are simply enabling username/password based authentication for the data service. In the next page, select "everyone" as the user group, and finish.
The sample service can be run using the "tryit" tool, which is bundled withWSO2 Data Services Server, or a code-generated java client sample using this service is demonstrated in the Data Services Clients Sample section.
This service contains a single query/opertion "showAllOffices", which returns all the office branches in a company.
When using a service client to access a secured data service, it must follow special steps in creating a secured connection to the service. The following code snippet is taken from the Axis2 service client used to access our sample secure data service.
String epr = "https://" + HOST_IP + ":" + HOST_HTTPS_PORT + "/services/SecureDataService"; System.setProperty("javax.net.ssl.trustStore", (new File(CLIENT_JKS_PATH)).getAbsolutePath()); ConfigurationContext ctx = ConfigurationContextFactory. createConfigurationContextFromFileSystem(null, null); SecureDataServiceStub stub = new SecureDataServiceStub(ctx, epr); ServiceClient client = stub._getServiceClient(); Options options = client.getOptions(); client.engageModule("rampart"); options.setUserName("admin"); options.setPassword("admin"); options.setProperty(RampartMessageData.KEY_RAMPART_POLICY, loadPolicy(SECURITY_POLICY_PATH));
The command line application is used here to present the functionality of the secured
data service. As shown inData Services Clients Sample, run
the command "ant secure_sample", to run the sample. The output will resemble Figure 1.
Figure 1: Secure Data Service invocation using service client.
So the above demonstration shows how a Java service client can be used in accessing a secured data service.