IMHO, there is nothing like that out of the box. What I would do in your case, is to have a hierarchy of URL for example rooted at /api
that would be exempt of csrf. It is easy to configure. In the XML config, you have a normal <http>
block including <csrf/>
, you just duplicate it and modify the first block like that
<http pattern="/api/**">
...
<!-- csrf -->
</http>
As it is first, it will be triggered for any request to /api
hierachy without using csrf, and all other requests will use it.
In the normal part of the application, you never use the /api/**
url, and reserve them to non-browser usages.
Then in your controllers, you map them to both their normal url and a copy under /api
:
@Controller
@RequestMapping({ "/rootcontrollerurl", "/api/rootcontrollerurl"})
class XController {
@RequestMapping(value = "/request_part_url", ...)
public ModelAndView method() {
...
}
}
(of course, rootcontrollerurl
and request_part_url
may be blank ...)
But you must analyze the security implication of allowing non csrf controlled requests, and eventually exclude controllers from the /api
hierarchy.