|
||||||||||||||||||||||||||||||
|
Article
In this article we will explore the options that you have when you want to add security to your web
services. With a reasonable amount of work, we will transform a simple service into an acceptably secure one.
And with security, we will even go further and think about real business.
User Authentication
This is the most basic, and simple form of 'security'. Most web sites that ask for a username and
login to customize your welcome page rely on this feature. In fact, it is not secure at all, but in
most cases, it will do the job.
It is possible to send username and password information in the HTTP header of a SOAP request.
To do this, the Apache-SOAP API allows setting a SOAPHTTPConnection object with username and password.
These values are included in the HTTP header in the form of a simple string
"Authorization: Basic username:password" that is Base64 encoded. This gives the following HTTP
header for an 'authenticated request'.
And that's all there is to it. So, why is it so unsafe?
Well, anybody can use the TcpTunnelGUI tool that is provided with the Apache
SOAP toolkit to view HTTP headers. And it takes just a few lines of code to decode a Base64 encoded string,
as we will see now in the Server changes.
Server Changes
To make your Apache-SOAP server read this username and password information, you will have to use a
custom provider instead of the standard provider that does the job for Java classes. If you look into
the Apache SOAP toolkit classes, there is a class, org.apache.soap.providers.RPCJavaProvider. This is
the class that Apache uses as a provider whenever you deploy a java class as a service. The toolkit
also includes a class named org.apache.soap.providers.TemplateProvider that you can use as a base to
write your own provider. The complete code for our provider is available in
Listing 1. Any provider in
Apache SOAP must implement the Provider interface, which defines two methods, locate and invoke. To do our
authentication checks, we will modify the locate method of our provider, and extract the Authorization
header. The code required to decode username and passwords is very simple, as shown below.
Now that we have written the service provider, we just need to deploy the service and specify our
custom provider. The figure below shows you how to do that with the Web Deployer of Apache SOAP. Look at the
'User-Defined' value for provider type and the classname of our provider just below. Of course you
need to make the class available in Tomcat's CLASSPATH by dropping the jar in the lib/ directory.
Client Changes The code changes to add basic authentication on the client side are simple:
And now you're done. Of course, the implementation of our custom provider is dumb and will not check anything for you. You need to modify the code to add your own authentication verification. It can be implemented using a database application containing user profiles and passwords, or any other system that you may need. It is up to you. Now let's move on to serious things. HTTP over SSL and Digital Certificates The first thing to do is to setup the Web Server/Servlet Container to use SSL. In our case, the documentation that explains how to do that for Tomcat is fine, with the exception of the clientAuth parameter on Tomcat's server file that we set to false instead of true. The most painful part of this exercise is really the certificates part. Certificates are required for both the client and the server and should be verified and trusted to get things to work. The prerequisites are to get JSSE (Java Secure Socket Extension), for the server and the client. Let's go, step by step.
Your data is now protected. At this point,if you try to use the TcpTunnelGUI tool to see how the HTTP request looks like, you will not be able to get anything from it. So that's it, you're done! Of course, security has a cost... performance. We have found that using SSL was increasing response times by 50%. If you have a profiler tool like JProbe (www.sitraka.com) or OptimizeIt (www.vmgear.com), you can profile your client application and look at the difference, just for curiosity. Establishing SSL connections takes time because of the handshaking protocol, plus random number generation and so on. Anyway, if you need security, you don't really have a choice, do you ? With such security in place, you can now move on to real serious things, like providing services with real added value, to a very restricted set of people. And if you want to do business, you can probably start billing for access to your service. This is the free gift of the day... There are two options in this case:
Conclusion So, you see, that was not so difficult. Even if the SOAP specification does not say anything about security (this may change though), adding security to your services is easy. There are technologies that would cause you much more of a headache before achieving the same result. Have fun with SOAP. [ Sylvain Benoist ] Copyright © 2001-2007 Nicholas Quaine. All rights reserved. |
|||||||||||||||||||||||||||||