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.
. Maybenginx
do not pass all headers fromkeycloak
? Can you check original response? – matejko219 Commented Jul 28, 2020 at 11:42
1 Answer
Reset to default 3Apparently 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
版权声明:本文标题:javascript - Keycloak authentication blocked by CORS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742257944a2441972.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论