angular-oauth2-oidc实现(错误地)抛出CORS错误

xqkwcwgp  于 2024-01-06  发布在  Angular
关注(0)|答案(1)|浏览(168)

我正在尝试在我的angular应用中实现angular-oauth2-oidc身份验证。在服务器端,一切都配置正确(也是CORS)。
我的配置看起来像这样:

this.oauthService.configure(authCodeFlowConfig);
    this.oauthService.setStorage(localStorage);
    this.oauthService.setupAutomaticSilentRefresh();
    this.oauthService.tokenValidationHandler = new NullValidationHandler();
    this.oauthService.loadDiscoveryDocumentAndTryLogin().then(() => {
       console.log('Discovery document fetched successfully');
    });

字符串
我的URL看起来像这样:

https://some-identity-provider.com/OAuth/Authorize?client_id=xxxxxx&p=my_policy&redirect_uri=' + window.location.origin + '&scope=openid%20profile&response_type=id_token


这个网址绝对有效。当我直接在浏览器中打开它时,它会正确地将我重定向到登录页面。
然而,在我的应用程序中,它试图将我重定向到登录页面,但我得到了一个CORS错误:x1c 0d1x
当我查看参数时,它似乎将/.well-known/openid-configuration附加到最后一个参数。



不知何故,它似乎混淆了URL和它的查询参数.
有人能帮帮忙吗?

5jdjgkvh

5jdjgkvh1#

既然有些人问了,让我来写一下我是如何解决这个问题的:
问题出在AuthConfig上:

export const authConfig: AuthConfig = {
    // Url of the Identity Provider
    // The problem was here: What I had before (caused the bug):
    issuer: 'https://some-identity-provieder.com/.well-known',

    // What fixed the issue is: Providing only the base url here:
    // The lib takes care of appending all parameters if needed.
    issuer: 'https://some-identity-provieder.com',

    // URL of the SPA to redirect the user to after login
    redirectUri: window.location.origin,

    // The SPA's id. The SPA is registerd with this id at the auth-server
    // clientId: 'server.code',
    clientId: environment.clientId,

    responseType: 'code',

    logoutUrl: 'some-logout-page.com',

    scope: `openid profile offline_access ${environment.clientId}`,

    showDebugInformation: environment.isLocalEnvironment,

    // turn off validation that discovery document endpoints start with the issuer url defined above
    // https://manfredsteyer.github.io/angular-oauth2-oidc/docs/additional-documentation/using-an-id-provider-that-fails-discovery-document-validation.html
    strictDiscoveryDocumentValidation: false,
    skipIssuerCheck: true,
};

字符串
请查看issuer。也检查所有其他参数。这是我的工作配置。
值得一提的是在作用域中提供clientId,因为只有这样才能获得id_token
希望这对你有帮助:)

相关问题