如何通过与角HTTP的身份验证凭据? [英] how to pass credentials with angular-http-auth?

查看:256
本文介绍了如何通过与角HTTP的身份验证凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,验证正在与角HTTP-auth的

Inside my app, authentication is working with angular-http-auth

当用户填写用户名/密码表格并提交,它从控制目标的登录()功能:

when user fill the username / password form and submit, it targets the login() function from the controller :

$scope.login = function() {
    var credentials = Base64.encode($scope.username + ':' + $scope.password);
    $http.defaults.headers.common['Authorization'] = 'Basic ' + credentials;

    User.login(                           // reference to the $resource service
        function() {                      // success callback
            authService.loginConfirmed(); // method of angular-http-auth
        }
    );

}

用户是$资源创建一个服务

User is a service created with $resource

factory('User', function($resource){
    return $resource('url/to/json', 
        {}, 
        {   'login': { method: 'GET', isArray:true }
        });
})

和Base64是从来到这里的加密服务

这是这样的工作,但有没有办法通过角HTTP的身份验证,而不是通过HTTP $默认设置标头中的凭据?

This is working like this, but is there a way to pass the credentials through angular-http-auth instead of setting default headers through $http ?

推荐答案

有这样的问题:

$http.defaults.headers.common['Authorization'] = 'Basic ' + credentials;

是,你现在设置的是由所有$ http请求的凭据。如果你不想做,你要直接通过资格证书,每个$资源调用,就可以了,这是什么样子你开始在那里做。

is that you're now setting the "credentials" for all $http requests that are made. If you don't want to do that, and you want to pass the "credentials" with each $resource call directly, you can, which is what it looks like you're starting to do there.

return $resource('url/to/json', 
    {}, 
    {   'login': {
             method: 'GET',
             headers: { 'Authorization': 'Basic ' + credentials }
         }
    });

我没有尝试这个非常的例子,但我有我类似code,其中的方法是POST,所以我返回之前在$资源的结果,调用save(),和登录关键是保存

I didn't try this very example, but I have I similar code where the method is a POST so I call save() on the result of $resource before returning it, and the 'login' key is 'save'

不过,因为你正在做一个GET,用定制的登录的方法,这应该工作。

But since you're doing a GET, with a custom "login" method, this should work.

这篇关于如何通过与角HTTP的身份验证凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆