HOWTO do CSRF protection in Struts2 application for AJAX requests

时间秒杀一切 提交于 2019-12-01 11:14:57

Currently I have resolved the issue by generating tokens for AJAX requests and sending it with the normal response like so -

    Map<String, String> tokenInfo = Maps.newHashMap();
    tokenInfo.put("struts.token.name", TokenHelper.getTokenName());
    tokenInfo.put(TokenHelper.getTokenName(), TokenHelper.setToken());

I will abstract out a util method out of this & have the Actions that are token-activated to return this as part of response for actions which will be executed repeatedly without refresh of the page.

I am still looking for an elegant solution to this though.

Andrea Ligios

Can't you generate a Token and use it as parameter in the ajax call ?

For the subsequent AJAX calls, you can use a per-user Token,

not necessarily a per-request Token,

as explained in this SO answer: Do I need a CSRF token for jQuery .ajax()?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!