加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Cakephp 2表单中的安全性

发布时间:2020-12-13 17:20:48 所属栏目:PHP教程 来源:网络整理
导读:我有一个关于cakephp2表单安全性的问题.假设我们已启用安全组件,并且已经构建了用户身份验证,权限和产品管理系统. 我们需要创建一个Offer Offer功能,允许用户询问特定产品的报价. 用户已登录并单击“询问”并转至/ offer_requests / add / product_id 场景1
我有一个关于cakephp2表单安全性的问题.假设我们已启用安全组件,并且已经构建了用户身份验证,权限和产品管理系统.

我们需要创建一个Offer Offer功能,允许用户询问特定产品的报价.

用户已登录并单击“询问”并转至/ offer_requests / add / product_id

场景1:

在/Views/OfferRequests/add.ctp中:

<?php 
echo $this->Form->create('OfferRequest');
echo $this->Form->input('user_id',array('value' => $this->Session->read('Auth.User.id'),'type' => 'hidden' ));
echo $this->Form->input('product_id');
echo $this->Form->input('quantity');
echo $this->Form->end(__('Submit'));
?>

场景2:

在/Views/OfferRequests/add.ctp中:

<?php 
echo $this->Form->create('OfferRequest');
echo $this->Form->input('product_id');
echo $this->Form->input('quantity');
echo $this->Form->end(__('Submit'));
?>

在OfferRequestsController中添加():

<?php
$this->request->data['OfferRequest']['user_id'] = $this->Session->read('Auth.User.id');
?>

我的问题是哪种情况更安全,例如反对以其他用户身份提出虚假请求.对于方案1,安全组件是否允许通过Firebug或其他软件操作输入值?

解决方法

是的,安全组件添加了自动防止表单篡改:

从docs:

By using the Security Component you automatically get CSRF and form
tampering protection. Hidden token fields will automatically be
inserted into forms and checked by the Security component. Among other
things,a form submission will not be accepted after a certain period
of inactivity,which is controlled by the csrfExpires time.

如另一个答案中所述,您可以在保存数据时使用fieldsList选项.但是,使用安全组件,您可以将user_id添加为隐藏字段(方案1),而不必担心其值被篡改.这将防止在控制器中设置它的必要性(方案2).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读