CakePHP 2.x - 使用基本HTTP身份验证创建Web服务

时间:2015-02-12 17:30:42

标签: web-services cakephp authentication

好的,所以我想创建一个网址服务,其网址如下:

http://www.website.com/webservice/update/

然后我希望能够使用id和一些身份验证信息发布到此URL。因此,提供身份验证成功后,id将被传递给将更新我的模型的函数。

我已经阅读了文档,似乎我需要实现基本的HTTP身份验证。我只是不确定最好的方法。我是否需要为身份验证设置单独的控制器?或者我可以通过我的webservice控制器处理它吗?

如何将身份验证数据传递给我的控制器,是通过$ request吗?

获得数据后进行身份验证的最佳方式是什么?

更新

好的,所以我在这里,我已经为控制器设置了身份验证,但允许我需要访问的视图。

authtest方法,设置HTTP套接字以模拟浏览器连接。然后更新方法应该检查用户名并传递在我已设置的用户模型的请求中传递的用户名。

我收到了以下错误:

调用未定义的方法AuthComponent :: _ findUser()

我做错了什么?

    <?php

    App::uses('AuthComponent', 'Controller/Component/');

    class PropertyController extends AppController {

      public $scaffold;
      public $components = array(
        'Auth' => array(
            'authenticate' => array('Basic')
        )
      );

      public function beforeFilter() {

        AuthComponent::$sessionKey = false;
        $this->Auth->allow('index','authtest','update');
        $this->Auth->unauthorizedRedirect = false;

      }
      public function update() {

        // Prevent header and footer from loading
        $this->layout = null;

        // Get Basic Auth - user and pass  
        $username = env('PHP_AUTH_USER');
        $pass = env('PHP_AUTH_PW');

        if (empty($username) || empty($pass)) {
           return false;
        }

        if( $this->Auth->_findUser($username, $pass) ) {
            echo "all good";
         }

        }
        public function authtest() {

            // Prevent header and footer from loading
            $this->layout = null;
            App::uses('HttpSocket', 'Network/Http');

            // Create HTTP Socket
            $HttpSocket = new HttpSocket();
            $HttpSocket->configAuth('Basic', 'Eddy', 'test');

            // Add post data
            $results = $HttpSocket->post(
                'http://local.webeservice.com/property/update/',
                'id=1992'
            );
            echo '<pre>';
            echo $results;
            echo '</pre>';

        }
        public function updated() {

        }   
    }
    ?>

2 个答案:

答案 0 :(得分:0)

看看这里: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

public function login() {
if ($this->request->is('post')) {
    if ($this->Auth->login()) {
        //put your code here
        return $this->redirect($this->Auth->redirectUrl());
    }
    $this->Session->setFlash(__('Invalid username or password, try again'));
}
}

您可以访问正常的请求数据

答案 1 :(得分:0)

使用cake PHP HTTP套接字

这是一个示例代码:

function update_stock_price(){
    $header = array(
        'header' => array(
            'X-EBAY-API-COMPATIBILITY-LEVEL'=>'903',
            'X-EBAY-API-DEV-NAME'=>'7994d18f-1c2b-46c5-bed8-decc651ef186',
            'X-EBAY-API-APP-NAME'=>'Directio-7da2-4081-b403-4aee455c2de8',
            'X-EBAY-API-CERT-NAME'=>'ee22b583-b57c-4bef-8eab-52b2e045d0e7',
            'X-EBAY-API-SITEID'=>'0',
            'X-EBAY-API-CALL-NAME'=>'ReviseInventoryStatus'
        )
    );
    $HttpSocket = new HttpSocket();
    $final_array = array(
        'ReviseInventoryStatusRequest'=>array(
            '@xmlns' =>'urn:ebay:apis:eBLBaseComponents',
            'RequesterCredentials'=>array(
                'eBayAuthToken'=>'AgAAAA**AQAAAA**aAAAAA**+8N9VA**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6wFk4GhDZCGoA+dj6x9nY+seQ**GiUDAA**AAMAAA**zLpuUI+5ja6pG7M72xGplK7ar9QSLwGnN8e/4CHUJPoUJlz5c+otf5Muf3QJLfa0q9eS410S2Lxzs/wBnBz3pLjRbnVSBLsAvpKL0SNax1aowBkzOh3c5nGkV1DeC2l3yTUgfWWAYoOLeDKH3cQsRKd54MJ2MdT1QDsUEW3gSXbXoaToAGcLM+hOjCkF+r2ozM5tzDGC26WG0DgcSGDIDJjw7JRKRoRYBDKj3s3YrzDdxzIZk2T8nqHOPmLNEhtuiYIDT6MOwejumaOO6f3WsEvAE2aYKfF1oTmiewMXQHQUWJE2kSp0KQYdwJlj1CbXAleprd4yU5f04hQQq4NWZsSYnLcUhpQICL/9wdUXMCAAKxz6mbeYlJo/ABwSCwMRcGspzzmB1bwTepmnUdpVwlE4Dh/F/ZxCQeEwSLwvii1XkZSjVj/xOMYx58G6Y/r+emHxYDHDD+ZI2P0suZ2oDHbAGquUEjAs4GSmRPCARDt129s7xrWGjj96MhqHZjtCzadmcEEpXfCyOjGP+gYhdUXzaebQxj3n/EDWNY9jiqBOGIpaQiR7zU8h9xt1B6UX7mBzLAp2HDxMxcBVN2/v2TNfMypU55nF5vQV7Ta1Tuf8YQ+LEYoYQz6SkmJQ8aJ5WsbmBKEitZUyGn9CC+UKN2hh93UL/txTmV9tVGWliZyJdM9fzcHrYRuYjGRBqEvf9nWfzEEkGwyfH3ErXbv/dNLbO8ARF9lvl+NVblE6k0KvxXG+hNtDqrYA7FADCZv7'
            ),
            'WarningLevel'=>'High',
            'InventoryStatus'=>array(
                'SKU'=>'S2730964',
                'ItemID'=>'110155417843',
                'Quantity'=>'6',
                'StartPrice'=>'200'
            )

        )
    );
    $xmlObject = Xml::fromArray($final_array);
    $xmlString = $xmlObject->asXML();
    $results = $HttpSocket-post('https://api.sandbox.ebay.com/ws/api.dll',$xmlString,$header);
    $xmlArray = Xml::toArray(Xml::build($results->body));
    pr($xmlArray);
}