Tag Archives: spring-ws

Spring WS Security on both client and server

Recently, I have been playing with Spring WS with WS-Security. I just want to write down how it works. Do not except anything special, just simple example of basic security operations.

The example

We want to implement both client and server side. The client will sign the message, encrypt some part of it and add a timestamp. To make it more complex and real-life like we will sign the message using private key with alias “client” and encrypt the message using public key called “server”. Server will validate that the request is valid and will just sign the response using his key called “server”. Please note that I have picked Wss4j implementation because the configuration seemed to be easier than Xws.

Client

It’s easy to do configure client interceptor like this.


    
        
            
        
    
    ...



    
    
    
    
    
    
    
    
    

    
    
    



    
    

As you can see, there is nothing special. We just define which actions to take and properties. The only confusing part is, that key alias is defined as “securementUsername”.

Whit this configuration we will get following SOAP message.


    
        
            
                
                
                    
                        
                            
                                CN=Test Server,OU=Test
                                1275904530
                            
                        
                    
                
                
                    fwFM7ShJ1xd7dTGrkh0410sTmW92OPB1q1fpzB21XFIe36siDDJWGgbw5B94yjmGK2YaPOWLb7cpVTYPzc9VUDs7Jc42CtrhT2H6eZ7CDiA60Ugz+qi2UyyfMDK6Vrdj9J68rij5P12AiBeTnd2wlhI29+71XbUpD5weHDHjMtQ=
                    
                
                
                    
                
            
            
                
                    
                    
                    
                        
                            
                        
                        
                        AU9utUgz5RylYCRDUAO0JWM48kM=
                    
                
                
                    NHjjgpb9/alUOq50CqPKLcdYrp7edYdKJDNvIhh+2OAhYdDvZmD1qGsVKd1H9oKPF17uaF2Sv3aY
                    0le6BrvzVx3n2+nYYlHwAWlzBk7wsBt4vLll6q6juLCP+siupTIb1PeZDf3WrAbHUQh5oqjD6cZB
                    Sc89pDspWRABQ8wPxYE=

                
                    
                        
                            
                                CN=Lukas Krecan,OU=Test
                                1275900789
                            
                        
                    
                
            
            
                2010-06-07T11:03:35.749Z
                2010-06-07T11:08:35.749Z
            
        
    
    
        
            
                
                    
                    
                        
                            
                        
                    
                    
                        81TEtUhHXo6iZeAmYrtYlm2ObAqOBpjfzf2VOVUg4Hs=
                        
                    
                
            
            2
        
    

Server config

To configure server, you have to define Spring WS server interceptor like this (full example).


    
        
            
        
    



    
    
    
    
    
        
            
                
                    
                
            
            
         
    
    
    
    
    
    



    
    

No surprise here neither. The response will look like this.


    
        
            
                
                    
                    
                    
                        
                            
                        
                        
                        hEdDfxM6Nfs62Jxe8EOsELCDtUk=
                    
                    
                        
                            
                        
                        
                        TTSRri5KJqXeMJfjzXyVmUewPxc=
                    
                
                
                    V5by3bOoGQNajfs7i9xQ+cbAqIkI0NS9N9FQlLb/dAuQfguE7jKRP9iypOeRLHCPr7g3BNg+NCrX
                    6YcgDQ0TfXNhdL00AmoEfDmWSNvIVNE49kZEn3Ji/RW4VtdEiV79VD7Vuay0YAYGo9DSQvzq3FP6
                    YEhfzfMqvfbWMdEKcO8=

                
                    
                        
                            
                                CN=Test Server,OU=Test
                                1275904530
                            
                        
                    
                
            
            
        
    
    
        
            3
        
    

As we have seen it’s possible to configure WS-Security without much hassle. To learn more, visit the official Spring WS reference. You can download full example here.

Spring WS Test

Last few weeks I have been working on one of my pet projects. Its name is Spring WS Test. As the name implies, its main purpose is to simplify Spring WS tests.

Again, I am scratching my own itch. I am quite test infected and I have needed something that allows me to write functional tests of my application without having to depend on an external server. Until now, you basically had two options. This first one is to test WS client application using plain old JUnit together with a library like EasyMock. But usually this test are quite ugly and hard to read. Moreover this type of tests does not test your configuration. The second option is to create a functional test that calls an external mock service. But this solution requires you to have two JVM, its configuration is complicated and error prone.

Classical WS test

I have been looking for something in between, for something that would allow me to write functional tests using JUnit and would be able to run in the same JVM as the test. Unfortunately I have not been able to find anything similar.

Spring WS Test test

That’s the reason why I have created Spring WS Test project. It’s quite simple and easy even though I had to spent lot of my evenings getting it into a publishable state.

Basic configuration looks like this


  
  

  
  

  
  
     
         
            
            
         
     
     
         
             
                 concat(local-name(//soapenv:Body/*[1]),'/default-response.xml')
             
         
                  
 
   

Here we have MockWebServiceMessageSender that replaces standard Spring WebServiceMessageSender. The replacement is done by MockMessageSenderInjector. The only other thing you have to do is to define ResponseGenerator. It’s main purpose is to look for files in you test classpath and return them as mock responses.

Of course it has to decide, which file to use. By default a XPath expression is used to determine the resource name. In our example it is concat(local-name(//soapenv:Body/*[1]),'/default-response.xml'). It takes name of the payload (first soap:Body child) and uses it as a directory name. File “default-response.xml” from this directory is used as the mock response. Simple isn’t it?

Of course you can define more complicated XPaths, you can use XSLT templates to generate your responses, you can validate your requests etc. More details can be found in the documentation.

Now I am looking for some end-user feedback. So please, if you are using Spring WS on the client side do not hesitate and test it. It should be stable enough to be used although there might be a bug here and there.

Spring WS fault detail

Spring WS project provides nice and versatile exception handling tools. But in some scenarios predefined Exception Resolvers are not sufficient. For example if you want to provide additional error info in the soap:fault detail like in this example:


   
SOAP-ENV:Client Something wrong happened Error BigTrouble

Fortunately it is quite easy to add similar behavior using Spring WS. You can easily extend existing SoapFaultMappingExceptionResolver and customize the fault (please note that EndpointExeption is project specific exception that provides necessary data):

 public class EndpointExceptionResolver extends SoapFaultMappingExceptionResolver {
    private static final QName CODE = new QName("code");
    private static final QName SUB_CODE = new QName("sub-code");

    @Override
    protected void customizeFault(Object endpoint, Exception ex, SoapFault fault) {
        logger.warn("Exception processed ",ex);
        if (ex instanceof EndpointException) {
            EndpointException ee = (EndpointException) ex;
            SoapFaultDetail detail = fault.addFaultDetail();
            detail.addFaultDetailElement(CODE).addText(ee.getCode());
            detail.addFaultDetailElement(SUB_CODE).addText(ee.getSubCode());
        }
    }
}