dpf56454 2015-06-10 10:45
浏览 89
已采纳

Slim PHP Framework中间件自定义URL过滤器

I am creating API using Slim Framework. I need to filter requests required authentication and route them to the specific auth handler. Or it would be better to say that I need to filter URI that don't require auth (public information).

I have created following middleware skileton

class TokenAuth extends \Slim\Middleware {
    private $auth;
    public function __construct($userEmail,$accesToken,$appSecret) {

    }

    /**
     * Deny Access
     *
     */
    public function deny_access() {
        $res = $this->app->response();
        $res->status(401);
    }


    public function authenticate($token) {
        ....
    }

    /**
     * Call
     *
     */
    public function call() {
        //Get the token sent from jquery

        $tokenAuth = $app->request->headers->get('Authorization');

        //Check if our token is valid
        if ($this->authenticate($tokenAuth)) {
        ....
        } else {
            $this->deny_access();
        }
    }

}

In this case I cannot access any URI without token, how to solve this problem, allowing access to the public resources.
I would be grateful for any help. Thx in advance.

展开全部

  • 写回答

2条回答 默认 最新

  • dousi4950 2015-06-11 02:08
    关注

    You have mainly two ways of doing it :

    Global middleware

    One way consist in adding an OAuth middleware to your API so you can check if user is authenticated or not and setup a flag, then inside each route you can do a simple check if user is authenticated or not.

    <?php
    $app = new \Slim\Slim();
    $app-authenticated = false;
    $app->add(new MyOAuthMiddleware());
    

    Then your MyOAuthMiddleware :

    <?php
     class MyOAuthMiddleware extends \Slim\Middleware {
      public function call() {
       //Do your OAUTH check stuff here
       $this->app-authenticated = true;
      }
    }
    

    Now you can check in all your routes :

    <?php
    $app->get('/hello/:name', function ($name) {
       $app = \Slim\Slim::getInstance();
       if($app->authenticated === true){
        echo "Hello, $name";
       } else {
        echo "You need to login";
       }
    });
    

    Specific route middleware

    You can follow Slim documentation and choose to add your Middleware directly on each declaration :

    <?php
    $authenticateForRole = function ( $role = 'member' ) {
        return function () use ( $role ) {
            $user = User::fetchFromDatabaseSomehow();
            if ( $user->belongsToRole($role) === false ) {
                $app = \Slim\Slim::getInstance();
                $app->flash('error', 'Login required');
                $app->redirect('/login');
            }
        };
    };
    $app = new \Slim\Slim();
    $app->get('/foo', $authenticateForRole('admin'), function () {
        //Display admin control panel
    });
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部