【问题标题】:How can i bind my Angular App with Azure Active Directory?如何将我的 Angular 应用程序与 Azure Active Directory 绑定?
【发布时间】:2020-11-25 05:46:31
【问题描述】:

我创建了一个只有两个组件的 Angular 应用程序,登录和用户详细信息组件。只想显示登录的用户详细信息。每当我从应用程序单击登录按钮时,它都会将我重定向到 azure-client。在浏览器上获取以下重定向网址。请建议/推荐下一步?

app.module.ts

@NgModule({
  declarations: [
    LoginComponent,
    UserDetailComponent,
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    RouterModule,
    HttpClientModule,
    ReactiveFormsModule,
    AppRoutingModule,
    MsalModule.forRoot({
      auth: {
          clientId: '************************************', // This is your client ID
          authority: '********************************************', // This is your tenant info
          redirectUri: 'http://localhost:4200/' // This is your redirect URI
      },
      cache: {
          cacheLocation: 'localStorage'
      },
   }),
  ],
  providers: [UserService,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: MsalInterceptor,
      multi: true
  }],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { UserDetailComponent } from './user-detail/user-detail.component';

const routes: Routes = [
  { path: '', component: LoginComponent },
  { path: 'login', component: LoginComponent },
  { path: 'userDetail', component: UserDetailComponent },
  { path: '**', redirectTo: '' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

点击自定义UI的登录按钮会调用的登录方法

login(username, password) : Observable<any> {

    let headers: HttpHeaders = new HttpHeaders();
   
    headers = headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers = headers.append('Content-type', 'application/json');
    headers = headers.append('Content-type', 'text/html');
    headers = headers.append('Access-Control-Allow-Origin', '*');
    headers = headers.append('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    headers = headers.append('Access-Control-Allow-Headers', '*');
    headers = headers.append('Access-Control-Allow-Credentials', 'true');
    
     return this.http.post<any>('http://localhost:8082/check', { username, password }, {headers})
        .pipe(map(user => {
               return user;
        }));
    }

【问题讨论】:

标签: angular azure spring-boot spring-security active-directory


【解决方案1】:

要使用 MsalModule 登录 Azure,您必须调用

msalService.loginPopup();

或

 msalService.loginRedirect();

前者是建议的。

见https://github.com/Azure-Samples/active-directory-javascript-singlepageapp-angular/blob/master/src/app/app.component.ts

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多