加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 身份验证标头

发布时间:2020-12-17 17:38:01 所属栏目:安全 来源:网络整理
导读:我正在尝试从头文件中使用登录名和密码从服务器获取 JSON. 请求中没有凭据. var app = angular.module("app",[]);app.factory('AuthService',function() { var currentUser; var login = 'hello@example.com'; var password = 'hello'; return { //login: fu
我正在尝试从头文件中使用登录名和密码从服务器获取 JSON.

请求中没有凭据.

var app = angular.module("app",[]);

app.factory('AuthService',function() {
  var currentUser;
  var login = 'hello@example.com';
  var password = 'hello';

  return {
    //login: function() {

    //},//logout: function() {

    //},login: login,password: password,isLoggedIn: function() {
      return currentUser != null;
    },currentUser: function() {
      return currentUser;
    }
  };
});

app.run(['$http','AuthService',function($http,AuthService) {
  /* Using btoa to do Base64 */
  $http.defaults.headers.common['Authorization'] = 'Basic ' + btoa(AuthService.login + ':' + AuthService.password);
}]);

app.controller('LabController',function($scope,$http){
  $scope.labs = $http.get('http://127.0.0.1:5000/api/v1.0/labs').error(function(data,status,headers,config) {
    //console.log("Data");
    console.log(data);
    //console.log("Headers");
    //console.log(headers());
  });
  window.labs = $scope.labs;
});

这是我在请求标头中得到的内容.

Host: 127.0.0.1:5000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip,deflate
Origin: http://localhost:5000
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization
Connection: keep-alive
Cache-Control: max-age=0

解决方法

我更喜欢这样写我的代码..

//This is a web service service which includes get and post type calls and also adds the required authentication headers to all the requests
myApp.factory('webService',function ($http,loadingActivity) {
    return{
        postRequest: function (requestUrl,requestData,contentType,successCallbackFn,errorCallbackFn,showLoading) {
            var httpRequest = {method: "POST",url: requestUrl,headers: {'userName':userName,'password':password,'Content-Type': contentType},data: requestData };
            $http(httpRequest).success(function (data,status) {
                    loadingActivity.hide();
                    successCallbackFn(data,status);
            }).error(function (data,status) {
                    loadingActivity.hide();
                    errorCallbackFn(data,status);
            });
        },getRequest:function(requestUrl,showLoading){
            var httpRequest = {method: "GET",'Content-Type': contentType}};
            $http(httpRequest).success(function (data,status);
            });
         }
    }
});

在这里,您可以使用Authorization标头替换userName和password.此代码经过了充分测试,我在生产环境中使用它.
在你的控制器simpy注入webService然后你可以这样调用它..

webService.getRequest(preferences.loginURL+data,"application/json",function(data,status){

},status){
     showAlert(messages.requestFailedError,"Login Failed!");
},true);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读