question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access to keycloak from origin http://localhost:4200 has been blocked by CORS policy

See original GitHub issue

"keycloak-angular": "^7.3.1", "keycloak-js": "^10.0.2", "@angular/core": "~9.1.11",

After ng serve, it takes to login page and after login it redirects to localhost:4200 with a blank page and gives the following error:

Access to XMLHttpRequest at 'https://keycloak41-dev.mydomain.com/auth/realms/myrealm/protocol/openid-connect/token' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The above error occurs only in angular9 I’m upgrading my angular7 project to angular9

With angular7 and keycloak-angular6, the same realm has no problems. It is working fine with the following combination: keycloak-angular : 6.1.0 @angular/core: 7.1.4

No config has been changed in keycloak admin. This error occurs only with angular9 and keycloak-angular7 combination.

My questions:

What should I need to do to get rid of cors error? Can I clear realm cache and user cache in keycloak admin, if so, is it safe to clear the cache?

Here is the code for both angular7 and angular9 projects, both projects has the same http://localhost:4200 url, but only angular9 gives the error.

keycloak.init({
    config: {
        realm: _REALM,
        url: _URL,
        clientId: _CLIENT_ID,
    },
    initOptions: {
        onLoad: 'login-required',
        checkLoginIframe: false
    },
    enableBearerInterceptor: true,
    bearerExcludedUrls: ['/assets', '/clients/public']
})

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13

github_iconTop GitHub Comments

3reactions
IsaacHubcommented, Jun 26, 2020

I have changed the Access Type from Confidential to Public and it worked. I want this to work with access type confidential as well.

0reactions
alpha-mocommented, Nov 30, 2022

Closing this issue as it seems to be resolved

how does it seem to be solved??

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keycloak angular No 'Access-Control-Allow-Origin' header is ...
If you enter your server as WebOrigin, the trouble begins. When you call keycloak.init in JavaScript, keycloak does not generate CORS headers, ...
Read more >
Access-Control-Allow-Origin header missing
I'm running Keycloak in a Docker Container. Do I have to enable CORS somewhere alltogether? Where? Is it possible with the Web-GUI?
Read more >
How to Use Keycloak with a CORS-enabled API-gateway
This article describes how you can use Keycloak behind an API-gateway that considers CORS requests, which by default does not work.
Read more >
JITSI-KEYCLOAK - CORS policy: No 'Access-Control-Allow ...
MYDOMAIN.com/api/config') from origin 'https://jk.MYDOMAIN.com' has been blocked by CORS policy: Response to preflight request doesn't pass ...
Read more >
Cross-Origin Request Blocked for userinfo endpoint - Red Hat
All resources, which I found are recommending to configure Keycloak CORS header Access-Control-Allow-Origin: *, which is not a secure option:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found