I have a jsp containing a jquery post to a servlet on my tomcat server which creates a HttpServletRequest. I would like to ensure that only my jsp’s calls to my servlet are processed and any requests originating from a source other than my jsp are ignored.
Is there a guaranteed way to see what is the referring page calling my server? I have read that using request.getHeader("referer") can be spoofed so I know I can’t rely on that.
I have a jsp containing a jquery post to a servlet on my tomcat
Share
Generate an unique string as token, store it in the session and embed it as a hidden input value in the POST form of the JSP and finally check in the servlet if the token is valid.
Basically:
On session creation (in
HttpSessionListener#sessionCreated(), for example):On preprocessing of the JSP request (in
HttpServlet#doGet(), for example):On processing the JSP itself:
On postprocessing of the form submit (in
HttpServlet#doPost(), for example):I of course assume that your
jQuery.post()functions are written in an unobtrusive way as in$.post(form.action, form.serialize(), callback)so that it simulates exactly the normal synchronous request (in other words, your forms works perfectly fine with JS disabled).