CSRF和RESTful API(symfony2,php)

编程入门 行业动态 更新时间:2024-10-27 19:18:34
本文介绍了CSRF和RESTful API(symfony2,php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用php/symfony2开发RESTful API.

I'm developing RESTful API using php/symfony2.

Symfony2具有开箱即用的CSRF保护功能,并且在使用常规表单数据时可以正常工作(它将CSRF令牌传递到表单,并且在回发时它期望嵌入在表单中的令牌相同).

Symfony2 comes with CSRF protection out of the box and it works fine when using normal form data (it passes CSRF token to the form and when posted back it expects the same token which is embeded in the form).

但是,如果您开发RESTful API,则此解决方案不适合目标用途,因为我的后端<->前端之间的通信完全基于JSON.因此,我禁用了CSRF.

Nonetheless this solution is not fit for purpose if you develop RESTful API, where my communication between backend<->frontend is purely JSON based. Because of that I disabled CSRF.

我知道没有CSRF令牌是不安全的,所以我想知道使用带有RESTful API的CSRF的最佳方法是什么.

I'm aware not having CSRF token is not safe, so I'm wondering what's the most optimal way to have CSRF with RESTful API.

一个主意是拥有特定的网址,例如/api/generate/csrf,可以由前端调用,然后将令牌附加到json请求.听起来并不是最安全的方法,因为技术上任何人都可以生成令牌.

One idea in mind is to have specific URL e.g. /api/generate/csrf, which can be called by frontend then append token to json request. It doesn't sound as the safest way as token technically could be generated by anyone.

开发RESTful API时解决CSRF问题的最佳方法是什么.

What's the best way to approach CSRF problem when developing RESTful APIs.

干杯, 理查德

推荐答案

请记住,只有当REST客户端使用基于会话的身份验证时,才需要CSRF保护,否则CSRF保护将无济于事.

Remember that you only need CSRF protection when your REST client is using session based authentication, otherwise CSRF protection won't help you.

如果您的请求确实使用基于会话的身份验证,则将CSRF令牌作为标头包含在内.像这样:

If your requests DO use session based authentication, I would include the CSRF token as a header. Something like:

CSRF-Token: dfsa0jr3n2io20a;

更多推荐

CSRF和RESTful API(symfony2,php)

本文发布于:2023-10-10 22:54:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RESTful   CSRF   API   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!