1
我實現了一個作爲API應用程序在Azure中承載的Java API後端。我打開Active Directory的身份驗證。在我配置的api應用程序的CORS設置中,允許來自每個來源的調用僅將URL設置爲星號。使用Active Directory認證在JavaScript中對Azure API應用程序進行API調用
現在我想從本地AngularJS應用程序訪問API。我的角度代碼如下所示:
angular.module('demo', [])
.controller('GetDataController', function($scope, $http) {
$scope.login = function(user){
console.log("user: " + user, user);
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
$http.get('http://<myapiapp>.azurewebsites.net/api/contacts').
then(function(response) {
$scope.greeting = response.data;
console.log("$scope.greeting: " + $scope.greeting, $scope.greeting);
});
}
});
但我仍然收到控制檯中出現以下錯誤:
XMLHttpRequest cannot load https://login.windows.net/
12141bed-36f0-4fc6-b70c-43483f616eb7/oauth2/autho…
%2Fapi%2Fcontacts%23&nonce=ea6b31321cda45d6a4e881fbd0062974_20161026094547.
Redirect from 'https://login.windows.net/12141bed-36f0-4fc6-b70c-43483f616eb7/
oauth2/autho…%2Fapi%2Fcontacts%23&
nonce=ea6b31321cda45d6a4e881fbd0062974_20161026094547' to
'https://login.microsoftonline.com/12141bed-36f0-4fc6-b70c-43483f616eb7/
oaut…%2Fapi%2Fcontacts%23&nonce=ea6b31321cda45d6a4e881fbd0062974_20161026094547'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
header is present on the requested resource.
Origin 'null' is therefore not allowed access.
那麼如何從AngularJS代碼訪問?
這也與我的帖子here有關,我嘗試通過curl調用來訪問。
任何方式來做到這一點,而無需修改應用程序代碼?看起來,當我請求資源時,azure登錄攔截器不尊重CORS,與請求已經過身份驗證的事實無關。我的開發環境中只啓用了Azure AD身份驗證,以防止未經授權的訪問。我想保持開發和產品之間的代碼基地相同。 –
不,您可以參考https://docs.microsoft.com/en-us/azure/active-directory/active-directory-authentication-scenarios#single-page-application-spa以進一步瞭解授權grand流向AAD。 –