【问题标题】:How do I secure my custom Magento2 REST api如何保护我的自定义 Magento2 REST api
【发布时间】:2017-01-08 22:44:29
【问题描述】:

我在 Magento2 中创建了一个自定义 REST api。但是,如何使用内置的 Magento2 REST api 安全性来保护它?

就像 /index.php/rest/V1/customers/me 使用 Authorization 标头保护

【问题讨论】:

    标签: rest security magento2


    【解决方案1】:

    webapi.xml 中创建自定义 api 配置时输入 ref="self"

    如果您像这样配置,您可以使用仅由 magento 2 提供的身份验证访问 API,例如 oauth、token、oauth2

    在magento的管理面板中禁用api对匿名的访问

    【讨论】:

    • 您也可以使用customerId来实现客户的特定行为。查看 Magento/Customer 中的 webapi.xml 文件,看看他们是如何做到的。
    【解决方案2】:

    通过应用自定义 api 的访问安全性

    Magento 2 允许未经身份验证的(匿名)用户访问某些 Web API。要防止匿名用户访问,请定义调用者必须有权访问的资源。喜欢,

    <route url="/V1/techyrules/webservice/deleteAddressMine" method="PUT">
        <service class="techyrules\WebService\Api\AddressManagementInterface" method="deleteAddressMine"/>
            <resources>
                <resource ref="self"/>
            </resources>
    </route> 
    

    ref,有效值为 self、anonymous 或 Magento 资源,例如 Magento_Customer::group。

    自我示例, 用户通过用户名和密码对他/她进行身份验证,然后将生成令牌作为响应,该令牌充当进一步处理的自我许可。

    【讨论】:

      【解决方案3】:

      在您的模块的etc/webapi.xml 中将&lt;resource ref="anonymous"/&gt; 替换为&lt;resource ref="Venodr_Module::name_of_the_acl_entry"/&gt;

      <route url="/V1/customers/me" method="...">
          <service class="..." method="..."/>
          <resources>
              <resource ref="Vendor_Module::name_of_the_acl_entry"/>
              <!--<resource ref="anonymous"/>-->
          </resources>
      </route>
      

      并在etc/acl.xml中设置ACL:

      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
          <acl>
              <resources>
                  <resource id="Magento_Backend::admin">
                      <resource id="Vendor_Module::name_of_the_acl_entry" title="Human readable title"/>
                  </resource>
              </resources>
          </acl>
      </config>
      

      然后在“System / Permissions / User Roles”中为具体的后端用户授予访问权限,选择Role,选项卡“Role Resources”和“资源访问”。选择“All”或选择“Custom”并检查名为“Human readable title”的资源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 2019-08-21
        • 2019-04-02
        • 2021-05-16
        • 2019-06-03
        • 2017-04-15
        相关资源
        最近更新 更多