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.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题