【问题标题】:Yii2 AccessControl for action to be accessed by certain websiteYii2 AccessControl 用于特定网站访问的操作
【发布时间】:2016-01-29 05:20:15
【问题描述】:

我的 ssl 服务器上有一个后端项目,例如 ssl.mybackend.com,具有以下内容:

class FormController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [                    
                    [
                        'actions' => ['index', 'delete', 'view', 'create'],
                        'allow' => true,
                        'roles' => ['@'], //only authorized users
                    ],
                    [
                        'actions'=> ['create-order'],
                        'allow'=>true   //change all users to "myfrontend.com"                   
                    ]
                ],
            ],

        ];
    }

我只需要向我的前端网站授予对create-order 操作的访问权限。 我不确定是否可以使用AccessControl,如果您能提出其他解决方案,我将不胜感激。

【问题讨论】:

标签: php ssl yii2 access-control crossdomain-request.js


【解决方案1】:

如果您想在另一个域上使用来自前端的 ajax 调用,您应该改用corsFilter。文档中的示例:

public function behaviors()
{
    return [
        'corsFilter' => [
            'class' => \yii\filters\Cors::className(),
            'cors' => [
                // restrict access to
                'Origin' => ['http://www.myserver.com', 'https://www.myserver.com'],
                'Access-Control-Request-Method' => ['POST', 'PUT'],
                // Allow only POST and PUT methods
                'Access-Control-Request-Headers' => ['X-Wsse'],
                // Allow only headers 'X-Wsse'
                'Access-Control-Allow-Credentials' => true,
                // Allow OPTIONS caching
                'Access-Control-Max-Age' => 3600,
                // Allow the X-Pagination-Current-Page header to be exposed to the browser.
                'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
            ],

        ],
    ];
}

Cross Origin Resource Sharing in Yii2

【讨论】:

  • 它似乎不起作用,因为它允许我从本地主机发出 POST 请求。
猜你喜欢
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
相关资源
最近更新 更多