Java CORS策略拒绝访问azure托管API应用程序
我们是一个致力于azure web服务的团队。我们想要实现的是拥有一个JavaScript前端,它可以与Azure中托管的Java API应用程序进行通信 我们正在使用Active Directory进行身份验证。我们已经在Azure门户中配置了CORS 我们已经使用Swagger Editor创建了一个Java后端,如中所述。我们刚刚改进了这个示例以支持我们自己的数据模型。因此,Java CORS策略拒绝访问azure托管API应用程序,java,azure,active-directory,cors,azure-active-directory,Java,Azure,Active Directory,Cors,Azure Active Directory,我们是一个致力于azure web服务的团队。我们想要实现的是拥有一个JavaScript前端,它可以与Azure中托管的Java API应用程序进行通信 我们正在使用Active Directory进行身份验证。我们已经在Azure门户中配置了CORS 我们已经使用Swagger Editor创建了一个Java后端,如中所述。我们刚刚改进了这个示例以支持我们自己的数据模型。因此,aporiginfilter类仍然保持不变: @javax.annotation.Generated(value =
aporiginfilter
类仍然保持不变:
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-11-08T15:40:34.550Z")
public class ApiOriginFilter implements javax.servlet.Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) response;
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
chain.doFilter(request, response);
}
public void destroy() {}
public void init(FilterConfig filterConfig) throws ServletException {}
}
我们的前端在本地PC上运行。因此,我们在azure门户的CORS中添加了我们的源代码“”
前端JavaScript代码如下所示:
var app = angular.module('demo', ['AdalAngular', 'ngRoute'])
.controller('GetDataController', ['$scope', '$http', '$timeout', 'adalAuthenticationService', function($scope, $http, $timeout, adalAuthenticationService) {
$scope.loggedIn = "Logged out";
$scope.responseData = "No Data";
$scope.loading = "";
$scope.loginAdal = function(){
adalAuthenticationService.login();
}
$scope.getData = function(){
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
$scope.loading = "loading...";
var erg = $http.get('http://<our-backend>.azurewebsites.net/api/contacts')
.success(function (data, status, headers, config) {
console.log("SUCCESS");
$scope.loading = "Succeeded";
$scope.loggedIn = "LOGGED IN";
$scope.responseData = data;
})
.error(function (data, status, header, config) {
console.log("ERROR");
$scope.loading = "Error";
console.log("data: ", data);
});
}
}]);
app.config(['$locationProvider', 'adalAuthenticationServiceProvider', '$httpProvider', '$routeProvider', function($locationProvider, adalAuthenticationServiceProvider, $httpProvider, $routeProvider) {
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
hashPrefix: '!'
});
var theEndpoints = {
"https://<our-backend>.azurewebsites.net/api/contacts": "f0f91e4a-ad53-4a4a-ac91-431dd152f386",
};
adalAuthenticationServiceProvider.init(
{
anonymousEndpoints: [],
instance: 'https://login.microsoftonline.com/',
tenant: "<our-tenant>.onmicrosoft.com",
clientId: "f6a7ea99-f13b-4673-90b8-ef0c5de9822f",
endpoints: theEndpoints
},
$httpProvider
);
}]);
并从AporiginFilter中删除这三个字段:
res.addHeader("Access-Control-Allow-Origin", "*");
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
现在,如果我们在本地运行后端,我们会在Chrome的开发者控制台的第二个过滤器中看到所有这些头文件
但是,一旦我们将后端部署到azure,我们就会以某种方式释放此头,并且在从前端访问api时再次出现相同的错误:
无法加载XMLHttpRequest。从“”重定向到
'
43483f616eb7/oauth2/autho…
%2Fapi%2Fapi%23和nonce=7658B7C8155B4B27F2F1447D4B77E47_2016115124144'
已被CORS策略阻止:无“访问控制允许来源”
请求的资源上存在标头。原点“null”为空
因此不允许访问
编辑:正如我写的,有两个XMLHttpRequest。第二个是https。所以如果你在排队的话
var erg = $http.get('http://<our.backend>.azurewebsites.net/api/contacts')
var erg=$http.get('http://.azurewebsites.net/api/contacts')
我将http更改为https,因为我们运行了error()
回调函数。在控制台中,我们有输出:
数据:需要用户登录
但正如我以前写的那样。我们已经登录到我们的租户。那么到底出了什么问题
var erg=$http.get('http://.azurewebsites.net/api/contacts")
根据代码,您可以使用HTTP协议请求服务。当我使用HTTP请求受Azure AD保护的web API时,我能够重现这个问题。Azure广告似乎会将HTTP重定向到HTTPS
在我用HTTPS更改服务URL后,问题得到了解决。请告诉我是否有用。谢谢。但正如我所写:“我们正在使用Active Directory进行身份验证。我们已经在Azure门户中配置了CORS。”因此,我们已经设置了您需要的设置,但仍然遇到上述问题。我已经更新了帖子,请检查它是否有用。不,抱歉。没有帮助。我们也在尝试https。请注意我们的最新帖子:根据错误消息,请求未经授权,被重定向到不支持跨域的授权端点。请检查令牌是否正确或已过期。我还可以在发送带有过期令牌的请求时重现此问题。您可以通过在门户上启用允许匿名请求(无操作)来验证此问题。
var erg = $http.get('http://<our.backend>.azurewebsites.net/api/contacts')