使用AngularJS(资源)和Laravel进行基本身份验证

时间:2014-03-14 19:58:46

标签: angularjs laravel-4

我正在寻找将凭据传递给我的laravel api的方法。

使用Javascript:

app.factory( 'APIService', function( $resource, $http ) {

    http.defaults.headers.common['Authorization'] = 'Basic ' + 'email:password';

    var data = $resource( 'api/v1/:route', {route: "@route"} );

    return data;

});

Laravel:

Route::filter('auth.basic', function()
{

    return Auth::basic();

});

我的解决方案无效:(

这是POSTMAN输出:

Request URL:http://localhost:8888/laravel-emt/public/api/v1/publications
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Authorization:Basic cm9iZXJ0LndhbHRoZXJAdHUtaWxtZW5hdS5kZTpjb21tYW5kZXIxNw==
Cache-Control:no-cache
Connection:keep-alive
Cookie:laravel_session=eyJpdiI6IkhSOUVZcjk5NTJIemdvb1ErXC9FQ1g4TVdYSmp6UHpXejVIbjBodTdmcXlRPSIsInZhbHVlIjoiRTFBNWpHNGZ5OGRvQ3MxNFVkcm8ra2FINDBmdE01XC9RRWxJSitJMTBROE1tbFpuS1JIN1o1YjFyVnRPUHVSZFE2TUZEWHdZSFBsdzdnRkViVEt0cXd3PT0iLCJtYWMiOiI0MjFmYmNhZTM1NDliYzJiNTI5YTNhNTZkZGVlODE5OTg0MGZjOTdiYzJkM2I2YjkyMTQ4YjE2Y2ViMGY5N2FhIn0%3D
Host:localhost:8888
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Response Headersview source
Cache-Control:no-cache
Connection:Keep-Alive
Content-Type:application/json
Date:Sat, 15 Mar 2014 17:11:46 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.3
Set-Cookie:laravel_session=eyJpdiI6InpRVFwvZDliRWpsRE0xcmlDdnpoNm5wM25Md0QrM3l1NVFXaWtMZ0RIZmJZPSIsInZhbHVlIjoibkEyZDZ1b2REK1B6VlJXWVFVK1NlS1plbCtMNE1NUXZVU3BRWk5Da09qOGVLOTVSYkNLSmpJaldxM0F0clYxOW1kVUlUY1RNdDRqSFAydWQ1ZlF0ZkE9PSIsIm1hYyI6ImZiMDVlNTk1ZDYwNjQ0Nzg0ZTY0YTYyNWUwYTNmYWRlY2ZkY2JmNDYzYmJkYmZmMmQ0MGU0ZTQ5Y2U1MzhjM2EifQ%3D%3D; expires=Sat, 15-Mar-2014 19:11:46 GMT; Max-Age=7200; path=/; httponly
Transfer-Encoding:chunked
X-Frame-Options:SAMEORIGIN
X-Powered-By:PHP/5.5.3

这是AngularJS输出:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Cookie:laravel_session=eyJpdiI6IlYwUDNUM01oSmJrWVwvTHAzQWQycmJ6Z25oaEgzaEJKR0ZLbzNPTVFIRDk0PSIsInZhbHVlIjoiSmxzMG1URUNYTFBnZmRkNk9NcFQyb2JwNWJDaW5wVlU2YVZYeFo2bTQwbUc2cVM5OThtdEVSRmhPV25cL2dtOUZHbXZnekJ3NzNsS2dOWXNRTGJMZWNRPT0iLCJtYWMiOiIyYzZhOGNhNTM2MjJhNjVkMzkxZDM5NmYxOTIwYWQ1YmNhYzEwYjg4MmM3NjU1NzJhNGJhYmI0OTU2MzczMTNlIn0%3D
Host:localhost:8888
Referer:http://localhost:8888/laravel-emt/public/secadmin
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Response Headersview source
Cache-Control:no-cache
Connection:Keep-Alive
Content-Type:application/json
Date:Sat, 15 Mar 2014 19:17:02 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.3
Set-Cookie:laravel_session=eyJpdiI6IkZiNXJBUmpaSU1RTkxNUHhaXC9mdEN3cHJ5REQ5aE9DajdXWCthMnBKN3ZNPSIsInZhbHVlIjoibXZjVjZ4dm1cL1lTY01XeEFHQyt2YmFBWWlFeWl2NVwvZm5iTlI4eG5UdHJBS003dkpreFJ1WU1qZldqQThndTRMWTN6dWRXVXhJelZOYWRQb1V0XC9nc0E9PSIsIm1hYyI6IjM3ZDFlNzc3MjBmZjk0M2UxZDY5NzFiMTc1YzIzYmVhODE1ZWM1MmVkZjEyYTRmMWNlMTgwYTA5ZDk3ODllMTkifQ%3D%3D; expires=Sat, 15-Mar-2014 21:17:02 GMT; Max-Age=7200; path=/; httponly
Transfer-Encoding:chunked
X-Frame-Options:SAMEORIGIN
X-Powered-By:PHP/5.5.3

我被要求手动输入密码。

我希望这些是你需要帮助的东西; - )

最好的问候,罗伯特

2 个答案:

答案 0 :(得分:1)

看看你的代码,我猜这只是一个错字:

app.factory( 'APIService', function( $resource, $http ) {
        // should be $http.defaults
        http.defaults.headers.common['Authorization'] = 'Basic ' + 'email:password';
        var data = $resource( 'api/v1/:route', {route: "@route"} );
        return data;
    });

答案 1 :(得分:1)

如果您将'email:password'编码为Base64,则可以使用。