gpt4 book ai didi

angular - Keycloak 和 Angular - 无法读取未定义的属性 'resourceAccess'

转载 作者:行者123 更新时间:2023-12-03 15:49:09 37 4
gpt4 key购买 nike

我有 angular 应用程序,我尝试添加 keycloak。我设置了 keycloak,但是当我尝试运行 angular 应用程序时,我得到了ERROR Error: Uncaught (in promise): An error happened during access validation. Details:TypeError: Cannot read property 'resourceAccess' of undefined
Angular 版本:8.2.5
Keycloak- Angular :7.0.1
Keycloak-js:7.0.0

这是我的 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);
}
});
}
}

这是我的 environment.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
    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 应用程序,因此您必须:

  • 从 NgModule 定义中删除 bootstrap: [Array]。
  • 将 entryComponents: [AppComponent] 添加到 NgModule 定义中。然后 keycloak Init 方法将在 AuthGuard 需要之前实际执行。
  •    @NgModule({
    declarations: [AppComponent, HomeComponent, UsersComponent, NavComponent],
    imports: [BrowserModule, AppRoutingModule, KeycloakAngularModule],
    providers: [{
    provide: KeycloakService,
    useValue: keycloakService
    }],
    entryComponents: [AppComponent]
    })

    关于angular - Keycloak 和 Angular - 无法读取未定义的属性 'resourceAccess',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58057255/

    37 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com