dongpo0409 2017-06-02 10:18
浏览 45

使用Backbone + Slim + Tuupola基本身份验证时未经授权的错误

I’m trying to connect my Backbone app with my server api (using Slim) and I’m using Tuppola / Basic Auth Middleware.

The example is very simple, I’m just trying to get it work. I can achieve this in my server acting directly in the browser. I get the popup window, type the user and password and I get the data.

However when I try to do the same thing using my Backbone app I get all the time the same 401 Unauthorized error. This is my php code. As I said, works fine when using the browser directly.

My class Container

$container["auth"] = function ()
{
    return new \Slim\Middleware\HttpBasicAuthentication([
        "path" => "/app_rutas",
        "realm" => "Protected",
        "users" => [
            "test" => "123",
        ],
        "environment" => "REDIRECT_HTTP_AUTHORIZATION"
    ]);
};

My class Routes

class Routes
{
    public static function callbacks($app)
    {
        $app->add(                                       \Controller::class . ':middleware');
        $app->add('auth');
        $app->get('/app_rutas/clients',                  \ClientController::class . ':selectAllClients');
        $app->get('/app_rutas/client/{type}/{values}',   \ClientController::class . ':selectClient');
        $app->put('/app_rutas/client',                   \ClientController::class . ':newClient');
    }
}

And this is my js code where I suppose the problem is that I'm not being able to pass correctly the params to the middleware. I've tried many ways a none works. I thought this should be the one it isn't.

fetchData() {
    if (!_.isEmpty(this.clients_id)) {
        this.url = this.apiUrl('client', this.clients_id);

        this.fetch({ headers: {'Authorization':'Basic test:123'} });
    }
},

Thanks for all comments in advance.

  • 写回答

1条回答 默认 最新

  • duanchen6423 2017-06-02 17:06
    关注

    This was the correct way to pass the username:password using Backbone:

    this.fetch({ headers: {'Authorization':'Basic ' + btoa('test:123')} })
    

    Needed a base64 encode function. Now it finally works.

    评论

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)