I recently implemented the use of Keycloak for authentication in my Django app following the steps outlined here. Now, I am faced with the challenge of integrating a Keycloak-protected microservice into the same setup without requiring users to log in again. My approach involves utilizing the JavaScript adapter and configuring it as shown below:
<script>
var keycloak = Keycloak({
url: "{{Keycloakurl}}/auth",
realm: 'myrealm',
clientId: 'myclient'
});
keycloak.init({ onLoad: 'login-required' }).success(function(authenticated) {
alert(authenticated ? 'authenticated' : 'not authenticated');
}).error(function() {
alert('failed to initialize');
});
</script>
However, upon loading the page, I encounter errors such as:
Failed to load http://keycloak.FOO.com/auth/realms/toxhq/protocol/openid-connect/token: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://myapp.Foo.com' is therefore not allowed access.
This issue seems to be related to the same-origin policy. I'm unsure of the exact cause, but I suspect this might be at play.
How can I achieve the desired functionality of having Keycloak-protected microservices share a single Keycloak authentication process?