admin管理员组

文章数量:1332107

I am trying to use Keycloak Javascript adapter in my React application, however after redirected from login page, it doesn't authenticate me, instead its giving me CORS error

Access to XMLHttpRequest at '/auth/realms/sso/protocol/openid-connect/token' from origin 'http://192.168.0.5:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

this is my Javascript page

import React from 'react';
import './App.css';
import Keycloak from 'keycloak-js';

const initKeycloak = async () => {
    const keycloak = new Keycloak({
        realm: 'sso',
        url: '/auth/',
        clientId: 'sso-management',
    });

    try {
        const isAuthorised = await keycloak.init({ onLoad: 'login-required' });
        console.log(isAuthorised);
    } catch (error) {
        console.log(error);
    }
};

function App() {
    initKeycloak();
    return (
        <div className="App">
            hello world
        </div>
    );
}

export default App;

and this is my keycloak client configuration

Am I missing something?

I'm using nginx reverse proxy on my local machine for the keycloak server if that makes any difference

I am trying to use Keycloak Javascript adapter in my React application, however after redirected from login page, it doesn't authenticate me, instead its giving me CORS error

Access to XMLHttpRequest at 'http://auth.keycloak.local/auth/realms/sso/protocol/openid-connect/token' from origin 'http://192.168.0.5:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

this is my Javascript page

import React from 'react';
import './App.css';
import Keycloak from 'keycloak-js';

const initKeycloak = async () => {
    const keycloak = new Keycloak({
        realm: 'sso',
        url: 'http://auth.keycloak.local/auth/',
        clientId: 'sso-management',
    });

    try {
        const isAuthorised = await keycloak.init({ onLoad: 'login-required' });
        console.log(isAuthorised);
    } catch (error) {
        console.log(error);
    }
};

function App() {
    initKeycloak();
    return (
        <div className="App">
            hello world
        </div>
    );
}

export default App;

and this is my keycloak client configuration

Am I missing something?

I'm using nginx reverse proxy on my local machine for the keycloak server if that makes any difference

Share Improve this question edited Jul 27, 2020 at 16:19 morgan9999 asked Jul 27, 2020 at 15:38 morgan9999morgan9999 8012 gold badges13 silver badges33 bronze badges 1
  • No 'Access-Control-Allow-Origin' header is present on the requested resource.. Maybe nginx do not pass all headers from keycloak? Can you check original response? – matejko219 Commented Jul 28, 2020 at 11:42
Add a ment  | 

1 Answer 1

Reset to default 3

Apparently in this particular case, I need to pass client secret when connecting to keycloak server since the client "access type" is "confidential".

To be able to make the code above work is to change the "access type" field to "public" and that will solve it.

I have no idea why the error response to the browser is CORS error which is absolutely unrelated on first glance from my perspective.

I figured it out after trying to make a request using Postman to http://auth.keycloak.local/auth/realms/sso/protocol/openid-connect/token with the same body message, and in postman it returns more relevant error which is asking me to provide client secret

本文标签: javascriptKeycloak authentication blocked by CORSStack Overflow