簡體   English   中英

使用angular.js中的OAuth2創建的令牌管理身份驗證

[英]manage authentication with tokens created with OAuth2 in angular.js

我的消息基於此消息http://stackoverflow.com/questions/26748106/how-to-manage-authentication-with-token-in-angular-js我有一個PHP5后端(Symfony2),一個前端(angularJS)我已經成功地使用后端創建了令牌,但是我遇到了angularjs的問題,我必須從URL中獲取令牌並將其放入每個請求中,以獲取每個示例的客戶端列表,這就是我所做的:

控制器:signin.js

'use strict';
  // signin controller
app.controller('SigninFormController', ['$scope','$http', '$state','$localStorage','authorizationInterceptor', function($scope, $http, $state,$localStorage,authorizationInterceptor) {
    $scope.user = {};
    $scope.authError = null;
    $scope.login = function() {
    $scope.authError = null;
      // Try to login
    $http({method: 'POST', url: 'myURL/oauth/v2/token?client_id=clientID&client_secret=clientSecret&grant_type=client_credentials'})
   .success(function(response){
     if (response.data.user) {  
     $scope.errors.splice(0, $scope.errors.length); 
     $scope.msgs.splice(0, $scope.msgs.length);
     $scope.posts = data; // response data 
     var token = this.$window.sessionStorage($scope.posts.access_token);
     console.log(""+token);
     console.log("success");
     $state.go('app.home');}
   })
   .error(function(data, status, headers, config) {
    $scope.authError = 'Email or Password not right';
    console.log("data error ...");
  });

    };

和我的服務:access-services.js

.factory('Security', ['$http', function ($http) {
        var token;
        function login(email, password) {
            return $http.post('/auth/login', {email: email, password: password})
                .then(function (response) {

                    if (response.data.token) {
                        token=response.data.token;
                    }
                });
        }

        function getToken(){
            return token;
        }

        return {
            login:login,
            token:getToken
        }; }])
.factory('authorizationInterceptor', ['Security', function (Security) {
        return {
            request: function (config) {
                var token=Security.getToken();
                config.headers = config.headers || {};
                if (token) {
                    config.headers.Authorization = 'Bearer ' + token;
                }
                return config;} }; }]);

但這是我在控制台中得到的: 在此處輸入圖片說明

感謝幫助

使用這樣的令牌攔截器:

app.factory('TokenInterceptor', function($q, $window) {
  return {
    request: function(config) {
      config.headers = config.headers || {};
      if ($window.sessionStorage.token) {
        config.headers['X-Access-Token'] = $window.sessionStorage.token;
        config.headers['X-Key'] = $window.sessionStorage.user;
        config.headers['Content-Type'] = "application/json";
      }
      return config || $q.when(config);
    },

    response: function(response) {
      return response || $q.when(response);
    }
  };
});

然后像這樣在您的app.config中使用它

$httpProvider.interceptors.push('TokenInterceptor');

您必須一定要使用令牌攔截器。 這可以在應用配置階段完成

yourApp.config(function($httpProvider) {
    $httpProvider.interceptors.push("authorizationInterceptor");
});

我建議您閱讀以下系列文章。 盡管它們是針對ASP.net MVC編寫的,但所提供的概念是通用的

http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM