【发布时间】:2017-01-08 22:44:29
【问题描述】:
我在 Magento2 中创建了一个自定义 REST api。但是,如何使用内置的 Magento2 REST api 安全性来保护它?
就像 /index.php/rest/V1/customers/me 使用 Authorization 标头保护
【问题讨论】:
我在 Magento2 中创建了一个自定义 REST api。但是,如何使用内置的 Magento2 REST api 安全性来保护它?
就像 /index.php/rest/V1/customers/me 使用 Authorization 标头保护
【问题讨论】:
在 webapi.xml 中创建自定义 api 配置时输入 ref="self"
如果您像这样配置,您可以使用仅由 magento 2 提供的身份验证访问 API,例如 oauth、token、oauth2
在magento的管理面板中禁用api对匿名的访问
【讨论】:
通过应用自定义 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。
自我示例, 用户通过用户名和密码对他/她进行身份验证,然后将生成令牌作为响应,该令牌充当进一步处理的自我许可。
【讨论】:
在您的模块的etc/webapi.xml 中将<resource ref="anonymous"/> 替换为<resource ref="Venodr_Module::name_of_the_acl_entry"/>:
<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”的资源。
【讨论】: