简体   繁体   English

AngularJS 组件和 ADAL(用于 JS 的 Active Directory Azure 库)

[英]AngularJS Components and ADAL (Active Directory Azure Library for JS)

I have built an angular 1.6 application in AngularJS with TypeScript and the Component Router, using the Component pattern.我使用组件模式在 AngularJS 中使用 TypeScript 和组件路由器构建了一个 angular 1.6 应用程序。

Now I am trying to authenticate into AzureAD using the ADAL library and a secured API which accepts a bearer token.现在我正在尝试使用 ADAL 库和接受不记名令牌的安全 API 对 AzureAD 进行身份验证。

In one of the current apps I built in AngularJS 1.3, I use ADAL in a pretty standard way, and I authenticate into the resource (API) with the StateProvider's RequireAdLogin attribute:在我在 AngularJS 1.3 中构建的当前应用程序之一中,我以非常标准的方式使用 ADAL,并使用 StateProvider 的 RequireAdLogin 属性对资源 (API) 进行身份验证:

  var homeState = {
     url: "/",
     requireADLogin: true
  };

This works fine, but now I am using the component router with angular-ui-router (with @Types/angular-ui-router ):这工作正常,但现在我使用带有angular-ui-router的组件路由器(带有@Types/angular-ui-router ):

import * as angular from 'angular';
import { State, StateProvider } from 'angular-ui-router';
import { MyComponent } from './my.component';
import { NavService, NavItem } from './../../common/nav/nav.service';
import { MediaService } from "./MediaService .service";

function routeConfig($stateProvider: StateProvider) {
  "ngInject";

  $stateProvider
    .state('app.media', {
        url: '/media',
        component: 'media'
    });

The problem is, I can not get the API to receive the token.问题是,我无法通过 API 来接收令牌。 The first sign of trouble is when I noticed the requireADLogin attribute is missing from the .IComponentOptions typing and the state type, so I can not do this:麻烦的第一个迹象是当我注意到 .IComponentOptions 类型和状态类型中缺少requireADLogin属性时,所以我不能这样做:

.state('app.media', {
    url: '/media',
    component: 'media',
    requireADLogin: true // THIS DOES NOT COMPILE!
});

I then did some research, and came across this rather depressing post:然后我做了一些研究,发现了这个相当令人沮丧的帖子:

GitHub ADAL Discussion: GitHub ADAL 讨论:

https://github.com/AzureAD/azure-activedirectory-library-for-js/issues/283 https://github.com/AzureAD/azure-activedirectory-library-for-js/issues/283

Do not go down this path.不要走这条路。 Component router is Deprecated https://docs.angularjs.org/guide/component-router组件路由器已弃用https://docs.angularjs.org/guide/component-router

Doh!哦! Well, this is not great news for me, to say the least, as it seems now I am blocked.嗯,这对我来说不是什么好消息,至少可以说,因为现在我似乎被阻止了。 I feel like there should be some work around, but I can't see it.我觉得应该有一些解决方法,但我看不到。

I think writing a custom HTTP Interceptor might be a solution, as I just need to feed the bearer token stored in storage to the API's HTTP Header.我认为编写自定义 HTTP 拦截器可能是一种解决方案,因为我只需要将存储在存储中的承载令牌提供给 API 的 HTTP 标头。 Also, I have been looking at MS Authentication Library for JS:另外,我一直在查看 JS 的 MS 身份验证库:

https://github.com/AzureAD/microsoft-authentication-library-for-js https://github.com/AzureAD/microsoft-authentication-library-for-js

But this seems too low level for my needs.但这对于我的需求来说似乎太低了。

I should mention, it was fairly difficult to get ADAL's Angular library for AngularJS to work with TypeScript and this new component pattern.我应该提到,让 AngularJS 的 ADAL Angular 库与 TypeScript 和这种新的组件模式一起工作是相当困难的。 It seems like the preferred path is Angular 2.0, but a rewrite at this point in NG2 is not viable as this project is time boxed.似乎首选路径是 Angular 2.0,但此时在 NG2 中重写是不可行的,因为该项目是有时间限制的。

I have solved this issue by manually extracting the bearer token from the storage mechanism (in my case browser session storage) and adding it every request on my web services:我已经通过从存储机制(在我的例子中是浏览器会话存储)中手动提取不记名令牌并将其添加到我的 Web 服务上的每个请求来解决这个问题:

var accessToken = sessionStorage.getItem("adal.access.token.key" +
         sessionStorage.getItem("adal.token.keys").replace("|", ""));

 configObj.headers = { 'Authorization': 'Bearer ' + accessToken }

I then pass that configuration object into each request, and the token is there - the API accepts it and everyone is happy.然后我将该配置对象传递到每个请求中,令牌就在那里 - API 接受它,每个人都很高兴。

this.$http.get(url, configObj).then(resolveData, errorCall);

Also as an alternate solution perhaps for someone that has not yet implemented your workaround:对于尚未实施您的解决方法的人,也可以作为替代解决方案:

We noticed a similar problem where one web app would include the security header with bearer token but a different web app would not even though the configuration was the same.我们注意到一个类似的问题,其中一个 Web 应用程序将包含带有不记名令牌的安全标头,但即使配置相同,另一个 Web 应用程序也不会。 The only difference in the end was the version of the ADAL for js.最后唯一的区别是 js 的 ADAL 版本。

when using :使用时:

https://secure.aadcdn.microsoftonline-p.com/lib/1.0.16/js/adal.min.js

we would not get the security token automatically attached to API requests我们不会将安全令牌自动附加到 API 请求

when using :使用时:

https://secure.aadcdn.microsoftonline-p.com/lib/1.0.12/js/adal.min.js

we would get the proper bearer token attached to all API requests made to a secure endpoint.我们将获得附加到向安全端点发出的所有 API 请求的正确不记名令牌。

We realised the breaking occured in version 1.0.14我们意识到在 1.0.14 版本中发生了中断

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM