Angularjs 如何删除角度中的基本身份验证?

Angularjs 如何删除角度中的基本身份验证?,angularjs,cordova,ionic-framework,ionic,Angularjs,Cordova,Ionic Framework,Ionic,Hi-am使用基本身份验证进行WebService集成 $http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode(Username + ':' + password); $http({method: 'GET', url: 'http:url.com'}). success(function(data) { if(data=='' ||

Hi-am使用基本身份验证进行WebService集成

$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode(Username + ':' + password);
        $http({method: 'GET', url: 'http:url.com'}).
            success(function(data) {
                if(data=='' || data==null || data=='undefined'){
                    var alertPopup = $ionicPopup.alert({
                        title: 'Info!',
                        template: 'Invalid Password, or no user found with this Email Address'
                    });
                    alertPopup.then(function(res) {
                        console.log('Invalid Password, or no user found with this Email Address ');
                    });

                }

这段代码对我来说运行良好。但我的问题是,如果一个用户使用用户名和密码登录。然后在另一个用户尝试使用不同的用户名和密码登录后注销,则前一个用户将登录。如何清除标头身份验证数据?

您可以使用

$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode(newUsername + ':' + newPassword)
看来,, 您在以下情况下未替换
授权
标题:

  • 用户已注销
  • 以其他用户身份登录
  • 登录-每次用户登录时调用此选项

    function setCredentials(username, password) {
        $http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode(username + ':' + password); 
    };
    
    注销-当用户注销时调用此选项

    function clearCredentials() {
        $http.defaults.headers.common.Authorization = 'Basic ';
    };
    
    以上代码片段简化自本教程-。我建议你读一读


    此外 请注意,使用基本http身份验证是不安全的,因为在每个请求上传递的密码没有加密

    作为替代方案,您可以使用会话将基本http身份验证更改为服务器端身份验证(注释您使用的服务器,我将把您链接到示例)


    如果您仍然决定保留基本http身份验证,请至少使用
    HTTPS

    我发现此解决方案有效:

    在启动注销时,首先尝试向假用户发出错误请求,以丢弃当前缓存的凭据

    我让这个函数处理请求(它使用jquery的
    $.ajax
    和禁用的异步调用):

    因此,当我尝试注销用户时,会发生以下情况:

    //This will send a request with a non-existant user.
    //The purpose is to overwrite the cached data with something else
    accountServices.authenticateUser('logout','logout');
    
    //Since setting headers.common.Authorization = '' will still send some
    //kind of auth data, I've redefined the headers.common object to get
    //rid of the Authorization property
    $http.defaults.headers.common = {Accept: "application/json, text/plain, */*"};
    

    您好,我改进了标题,但它仍然显示了您可以在$HttpProvider中获取的相同用户,要检查标题授权是否在那里定义,还可以在拦截器中重新定义,您还可以尝试grep-RC2授权。在项目的根目录下检查它的使用位置,并查看它的重新定义位置抱歉打扰您,我是angularjs的新手,您如何重新定义httpprovider?您可以在应用程序中的应用程序中定义配置块,例如app.config(['$httpprovider',function($httpprovider){$httpprovider.defaults.headers.common['Autentification']='etc…}),但我建议您在重新定义代码的地方签入代码,搜索关键字身份验证…另一个注释是write,您应该定义一个要记录的方法和一个要卸载的方法,在这里您可以重新定义头
    //This will send a request with a non-existant user.
    //The purpose is to overwrite the cached data with something else
    accountServices.authenticateUser('logout','logout');
    
    //Since setting headers.common.Authorization = '' will still send some
    //kind of auth data, I've redefined the headers.common object to get
    //rid of the Authorization property
    $http.defaults.headers.common = {Accept: "application/json, text/plain, */*"};