Friday, 23 August 2013

Spring MVC - Order of parameters in request with content-type application/x-www-form-urlencoded

Spring MVC - Order of parameters in request with content-type
application/x-www-form-urlencoded

I am working on webservice which consumes requests of type:
application/x-www-form-urlencoded
Example of such request body:
loginId=tester&action=add&requestId=987654321&data=somedata
The request is signed (SHA1withRSA) by the client and the signature is
sent in http header.
The thing is that I always get the parameters in different order e.g.
action=add&loginId=tester&requestId=987654321&data=somedata
Therefor the signature always fails.
Interesting is that this occurs only if the Content-Type of such header is
application/x-www-form-urlencoded. If i switch the Content-Type to
text/plain in the request then everything works perfectly.
I've used different types of clients (even monitored the traffic using TCP
monitor) and I am sure that the issue is not caused by client app.
Here is part of my custom message converter (Please note that i am
directly printing the incoming request to console):
@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage
inputMessage) throws IOException,
HttpMessageNotReadableException {
log.debug("> readInternal - message to Object");
InputStream inputStream = inputMessage.getBody();
byte[] bytes = IOUtils.toByteArray(inputStream);
String body = new String(bytes, charset);
log.debug("Body: {}", body);
And my springMVC configuration:
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
/>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<array>
<bean class="converter.NvpHttpMessageConverter">
<property name="charset" value="UTF-8" />
<property name="nvpConverter" ref="nvpConverter" />
</bean>
</array>
</property>
</bean>
I believe that spring somehow detects that content type is
application/x-www-form-urlencoded and uses somekind of preprocessing.
I use tomcat7.
Is my assumption correct? Can this be turned off?

No comments:

Post a Comment