3

I am hitting Key cloak api http://localhost:8080/auth/realms/**/protocol/openid-connect/token with right credentials ,its working fine but with wrong credentials

enter image description here

And when I add the cross-origin-allow , it give me preflight error

can anyone please help me :) PS: everything is working fine with CORS plugin

The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
Nikhil Kapoor
  • 547
  • 1
  • 4
  • 15
  • Possible duplicate of [No 'Access-Control-Allow-Origin' header in Angular 2 app](https://stackoverflow.com/questions/36002493/no-access-control-allow-origin-header-in-angular-2-app) – Haseoh Jul 27 '17 at 10:33
  • Just set headers like `Header set Access-Control-Allow-Origin "*"` in .htaccess – The Hungry Dictator Jul 27 '17 at 10:36
  • Providing better question, will help to get better answers. Please see https://stackoverflow.com/help/how-to-ask – Christian Jul 31 '17 at 07:55

2 Answers2

5

You are requesting a resource at localhost:8080 from you angular app which runs at locahost:4200.

In order for this to work you need to enable CORS on the server side. Thus you need to add the header to the response. This can either be done using the .htaccess file which has been proposed already. A better solution (or maybe the best one) is to configure this in your keycloak server. You can set allowed origins on the realm in which you are working. Simply open your admin console, navigate to the realm settings and set the 'Web origins'. The hint already tells you that you can configure 'Allowed CORS origins.' here.

Configure allowed CORS origins

For testing purpos you should add http://localhost:4200

Tobias Amon
  • 275
  • 3
  • 5
1

In our setup we used a keycloak.json for the communication between karaf and keycloak in this scenario it was necessary to add

"enable-cors": true

to our keycloak.json file. See also here

Christian
  • 1,664
  • 1
  • 23
  • 43
  • 1
    thanks for replying as mentioned i am not using keycloak.js ,i am directly calling the api so there is no keycloak.json file in my directory – Nikhil Kapoor Jul 31 '17 at 09:39