dongzhan1383 2016-01-29 05:20
浏览 139

Yii2 AccessControl用于某些网站访问的操作

I have a backend project on my ssl server, like ssl.mybackend.com, with following:

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"                   
                    ]
                ],
            ],

        ];
    }

I need to grant an access to create-order action only to my frontend website. I am not sure if it's possible to do with AccessControl and appreciate if you could advise other solutions.

  • 写回答

1条回答 默认 最新

  • doudao9915 2016-01-29 07:59
    关注

    If you want to use ajax calls from frontend on another domain, you should use corsFilter instead. Example from documentation:

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集