在我的react应用程序中,我使用了keycloak作为身份提供者。我已经在使用npm的react应用程序中安装了keycloak react依赖项。
下面是依赖的键盘斗篷反应npm模块的版本:
@react keycloak/web“:"2.1.4",
“钥匙斗篷”:"^11.0.2",
我提供了键盘斗篷配置如下:
const keycloak = new Keycloak({
realm: "temp-local",
url: " http://localhost:8090/auth/",
clientId: "temp-local-client"
});
每次我刷新页面时,它都会使用查询参数state、session_state和code再次刷新(使用回调url)。
例如:当我转到页面:http://localhost:8080/test时,它会去那里,但是它会再次刷新页面下面的url。
如何避免钥匙斗篷刷新?有人知道这件事吗?
发布于 2020-12-07 14:12:13
同样的问题,对我来说,我是这样修正的:
在主要组成部分中:
const App = () => {
//here i'm using redux, btw i'm initializing this auth component
useEffect(() => auth.init(store.dispatch), [])
第四部分:
import Keycloak from 'keycloak-js';
const keycloak = Keycloak('/keycloak.json');
import axios from 'axios'
import { LocalStore } from '../utils/local_store.js'
const store_token = (token) => {
axios.defaults.headers.Authorization = `Bearer ${token}`;
LocalStore.set('token', token);
}
const auth = {
init(dispatch) {
keycloak.init({ onLoad: 'login-required', checkLoginIframe: false, }).then(() => {
store_token(keycloak.token)
dispatch(authenticateResponse(keycloak.token)).then(
() => {
//auth logic
}
);
//this is my fix
setInterval(() => {
keycloak.updateToken(70).then((refreshed) => {
if (refreshed) {
store_token(keycloak.token)
console.debug('Token refreshed');
} else {
console.warn('Token not refreshed');
}
}).catch(() => {
console.error('Failed to refresh token');
});
}, 50000)
}).catch(() => {
console.error('Auth failed');
});
},
keycloak() {
return keycloak;
}
}
export default auth;
发布于 2021-02-08 16:25:47
检查代码的其他部分是否在react路由器中进行重定向:我也遇到了同样的问题,但是在一个大型代码库中,其他错误(因为缺少令牌)导致内部重定向到登录路由。
发布于 2022-10-28 22:16:32
我也面对过这种情况,帮助我使用的是静默检查SSO:
index.html
旁边添加一个名为silent-check-sso.html
的文件,其内容如下:<html><body><script>parent.postMessage(location.href, location.origin)</script></body></html>
initOptions
更新<ReactKeycloakProvider>
: <ReactKeycloakProvider
authClient={keycloak}
initOptions={{
onLoad: 'check-sso',
silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html',
pkceMethod: 'S256',
}}
>
...
</ReactKeycloakProvider>
不幸的是,我不知道它如何工作的技术细节(欢迎解释评论),但它帮助了我:]这里是如何工作,而不对SSO进行沉默检查:
这就是无声检查SSO:
希望能帮上忙!
https://stackoverflow.com/questions/65182935
复制相似问题