我最近开始使用KeyCloak和Angular。我的KeyCloak Docker运行在端口8080上,我的Angular应用程序运行在4200上。现在当我通过浏览器访问我的应用时,我会被重定向到合适的重定向网址下的“auth/realms/...” 。到目前为止,这是预期的行为。然而,在KeyCloak登录页面加载之前,我甚至被重定向回了旧的“auth/realms/...”,但这次我的重定向网址却是刚刚设置过的旧的“auth/realms/...”。
问题似乎不是KeyCloak本身,而是我的Angular实现,因为即使我禁用了KeyCloak客户端或访问错误的客户端,我也会得到相同的行为。
现在来看一些代码。我已经按照以下方式设置了我的KeyCloakOptions:
然而,我刚刚意识到在我的响应主体中,我得到了我的应用程序的
问题似乎不是KeyCloak本身,而是我的Angular实现,因为即使我禁用了KeyCloak客户端或访问错误的客户端,我也会得到相同的行为。
现在来看一些代码。我已经按照以下方式设置了我的KeyCloakOptions:
export const keycloakOptions: KeycloakOptions = {
config: {
url: '/auth',
realm: 'bookstore',
clientId: 'bookstore-front-end'
},
initOptions: {
onLoad: 'login-required',
checkLoginIframe: false
},
enableBearerInterceptor: true,
bearerExcludedUrls: ['/assets']
};
服务的初始化如下:
export class AppModule implements DoBootstrap {
public ngDoBootstrap(appRef: ApplicationRef): void {
keycloakService
.init(keycloakOptions)
.then(() => {
console.log('[ngDoBootstrap] bootstrap app');
appRef.bootstrap(AppComponent);
})
.catch(error => console.error('[ngDoBootstrap] init Keycloak failed', error));
}
}
我怀疑我的应用程序路由与问题有关。它看起来像这样:
const routes: Routes = [
// home route protected by auth guard
{path: 'books', component: BooksComponent, canActivate: [AuthGuard]},
// otherwise redirect to home
{path: '', redirectTo: 'books', pathMatch: 'full'}
];
如果您需要任何进一步的代码,请告诉我,非常感谢您的帮助。
编辑:
我的第一个请求如下:
http://localhost:4200/auth/realms/bookstore/protocol/openid-connect/auth?client_id=bookstore-front-end&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2F&state=c530f55a-b21e-43c3-8e1c-a3beb134c9ee&response_mode=fragment&response_type=code&scope=openid&nonce=0c359787-92e7-4d6d-9578-a65da686b046
然而,我刚刚意识到在我的响应主体中,我得到了我的应用程序的
index.html
文件,这很奇怪。