如何在 Grails 中防范 XSRF?
如何在 Grails 中防范 XSRF 攻击。我看到表单支持 useToken 的概念(我认为应该足够了)。但是,remoteForm 或其他 AJAX 相关请求不支持此功能。
另外,有没有办法反转 useToken 的功能,以便始终使用它而不是根据具体情况启用它?
How does one protect against XSRF attacks in Grails. I see that forms support the notion of useToken which (I think should suffice). However, remoteForm or other AJAX related request don't support this feature.
Also, is there a way to invert the functionality of useToken so that it is always used rather than enabled on a case by case basis?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试查看的源代码 标签。它使用 SynchronizerToken 创建一个令牌并将其存储在会话中。根据此问题的解决方案,应该可以使用相同的令牌所有表格都在同一页上。我没有尝试这样做,但理论上您只需要在表单上手动创建一个隐藏字段并在该字段中生成令牌。
You could try looking at the source code of the <g:form> tag. It uses a SynchronizerToken to create a token and store it in the session. Based on the resolution of this issue it should be possible to use the same token for all forms on the same page. I did not try this, but theoretically you would just need to manually create a hidden field on the form and generate the token in that field.
我们在 before 过滤器中向请求对象注入一个隐藏值,并使用特定密钥加密该值。然后,我们将该 request.token 值注入到网站上的每个表单中,当我们收到 POST 时,我们有一个 before 过滤器来验证该隐藏字段是否存在并且其值可以通过相同的密钥进行解码。
如果该隐藏值不存在或者它已过时(我们使用时间戳作为有效负载),我们会给客户端一个错误状态。
这是上述方法的另一种方法,我们使用这种方法,因为我们不在我们的站点上使用会话来更容易地进行负载平衡。
We have inject a hidden value to the request object in a before filter and encrypt the value with a specific key. We then inject that request.token value to every form on the site and when we receive a POST we have a before filter to verify that that hidden field is present and its value can be decoded by that same secret key.
If that hidden value is not present or if it is stale -- we use a timestamp as payload -- we give the client an error status.
This is an alternative way to what was described above and we use this, because we do not use sessions on our sites to make it easier to load balance.