I am trying to implement the Content-Security-Policy-Report-Only Header for my website.
In order to do that, i need a controller that will accept the browsers POST-Request - which will send data about the violation in form of JSON. This request, however, seems to specify the Content-Type as application/csp-report
instead of application/json
(side note: Why the hell??).
This apparently causes Spring to refuse the request - it seems like the usage of @RequestBody
makes spring only accept requests that are of Content-Type "application/json"
.
Even when i specifically set the value consumes
of the @RequestMapping
annotation to application/csp-report
it still does not accept the request.
I have gone as far as using a filter to wrap the request with HttpServletRequestWrapper
- which seems to be the common way of modifying the behavior of a request.
I have overwritten all of these methods: getContentType()
, getHeader(String)
, getHeaders(String)
to return "application/json".
But the request still does not go through.
According documentation on @RequestBody
annotation,
The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request
What that means is Spring Framework defines a specialized API for defining how certain MediaTypes are converted into certain Java types when used as parameters to REST endpoints.
Here is a pretty good article showcasing these capabilities.
There are a great deal of builtin Spring converters that you may be able to just configure and use if your media format can be mapped to their respective media formats. Specifically for your case, you should look at one of the converters available in spring.converter.json package. In simplest case, making it work should be as simple as:
HttpMessageConverter converter = new <JsonConverterOfYourChoice>(JsonMapper);
converter.getSupportedMediaTypes().add(new MediaType("application", "csp-report"));
And then registering such converter into a spring's configuration as you do.
Other available converter types include:
Finally, if none of the above does not apply for you, you can make and register your own HttpMessageConverter implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With