I am using a HMAC Authentication filter. In the filter when I access my POST Request Body, I am able to get the XML in it. When I try to access the XML in the controller I am get a blank string. The xmlString
in the filter is giving the proper XML but the xmlString
in the controller is giving blank string. I am using Spring MVC.
My filter is:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String authorisation = httpRequest.getHeader("authorization");
String accessKeyId = authorisation.split(":")[0].split(" ")[1];
String signature = authorisation.split(":")[1];
String secretAccessKey = getSecretAccessKey(accessKeyId);
InputStream xmlStream = httpRequest.getInputStream();
String xmlString = IOUtils.toString(xmlStream, "UTF-8");
String encodedXml = new String();
try {
encodedXml = HMACEncoder.calculateHMAC(xmlString, secretAccessKey);
} catch (SignatureException e) {
e.printStackTrace();
}
if (!signature.equals(encodedXml))
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
else
chain.doFilter(request, response);
}
and my controller is:
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String fetchUserString(HttpServletRequest request) {
InputStream xml = null;
String xmlString = null;
try {
xml = request.getInputStream();
xmlString = IOUtils.toString(xml, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return xmlString;
}