本文介绍了Keycloak和Angular-无法读取未定义的属性'resourceAccess'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有棱角分明的应用程序,我尝试添加按键斗篷.我设置了密钥斗篷,但是当我尝试运行有角度的应用程序时,我得到了ERROR Error: Uncaught (in promise): An error happened during access validation. Details:TypeError: Cannot read property 'resourceAccess' of undefined

I have angular app and I try to add keycloak. I set up keycloak, but when I try to run angular app I getERROR Error: Uncaught (in promise): An error happened during access validation. Details:TypeError: Cannot read property 'resourceAccess' of undefined

角度版本:8.2.5键角角度:7.0.1Keycloak-js:7.0.0

Angular version: 8.2.5Keycloak-angular: 7.0.1Keycloak-js: 7.0.0

这是我的auth.guard.ts

Here is my auth.guard.ts

import { Injectable } from "@angular/core";
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router";
import { KeycloakService, KeycloakAuthGuard } from "keycloak-angular";

@Injectable()
export class AppAuthGuard extends KeycloakAuthGuard {
  constructor(protected router: Router, protected keycloakAngular: KeycloakService) {
    super(router, keycloakAngular);
  }

  isAccessAllowed(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
    return new Promise(async (resolve, reject) => {
      if (!this.authenticated) {
        this.keycloakAngular.login();
        return;
      }

      const requiredRoles = route.data.roles;
      if (!requiredRoles || requiredRoles.length === 0) {
        return resolve(true);
      } else {
        if (!this.roles || this.roles.length === 0) {
          resolve(false);
        }
        let granted: boolean = false;
        for (const requiredRole of requiredRoles) {
          if (this.roles.indexOf(requiredRole) > -1) {
            granted = true;
            break;
          }
        }
        resolve(granted);
      }
    });
  }
}

这是我的环境.ts

import { KeycloakConfig } from "keycloak-angular";

let keycloakConfig: KeycloakConfig = {
  realm: "realm5",
  url: "https://aaaaaa.net/auth",
  clientId: "admin"
};
export const environment = {
  production: false,
  keycloak: keycloakConfig
};

在app-routing.module.ts中我导入AppAuthGuard

import { NgModule } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";
import { AppAuthGuard } from "./auth/auth.guard";
import { HomeComponent } from "./home/home.component";
import { UsersComponent } from "./users/users.component";

const routes: Routes = [
{ path: "admin-app/home", component: HomeComponent, canActivate: 
[AppAuthGuard] },
{ path: "admin-app/users", component: UsersComponent, canActivate: 
[AppAuthGuard] },
{ path: "admin-app", redirectTo: "admin-app/home", canActivate: 
[AppAuthGuard] }
];

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

和在app.module.ts

and in app.module.ts

    import { KeycloakAngularModule, KeycloakService } from "keycloak-angular";
    const keycloakService = new KeycloakService();
    @NgModule({
      declarations: [AppComponent, HomeComponent, UsersComponent, NavComponent],
      imports: [BrowserModule, AppRoutingModule, KeycloakAngularModule],
      providers: [{
         provide: KeycloakService,
         useValue: keycloakService
      }],
     bootstrap: [AppComponent]
    })
   export class AppModule {ngDoBootstrap(app) {
     keycloakService
  .init()
  .then(() => {
    console.log("[ngDoBootstrap] bootstrap app");

    app.bootstrap(AppComponent);
  })
  .catch(error => console.error("[ngDoBootstrap] init Keycloak failed", error));

}}

推荐答案

您正在手动引导Angular App,因此您必须:

You are manually Bootstrapping the Angular App, so you have to:

  • 从NgModule定义中删除引导程序:[Array].

  • remove the bootstrap: [Array] from the NgModule definition.

将entryComponents:[AppComponent]添加到NgModule定义中.然后,将在AuthGuard中实际执行keycloak Init方法之前.

add entryComponents: [AppComponent] to the NgModule definition. Then the keycloak Init method will actually execute before needed in the AuthGuard.

   @NgModule({
     declarations: [AppComponent, HomeComponent, UsersComponent, NavComponent],
     imports: [BrowserModule, AppRoutingModule, KeycloakAngularModule],
     providers: [{
        provide: KeycloakService,
        useValue: keycloakService
     }],
    entryComponents: [AppComponent]
   })

这篇关于Keycloak和Angular-无法读取未定义的属性'resourceAccess'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 00:30