前端集成keycloak鉴权的主要写法,
在main.js里面写
import VueKeycloakJs from '@dsb-norge/vue-keycloak-js'
import { KeycloakInstance } from "keycloak-js";// 回调地址
const pageIndex = process.env.NODE_ENV === 'production' ? 'http://xxxx/#/' : 'http://localhost:8081/#/'
export const app = createApp(App) // us
app.use(VueKeycloakJs, {init: {// Use 'login-required' to always require authentication// If using 'login-required', there is no need for the router guards in router.jsonLoad: 'login-required',checkLoginIframe: false// silentCheckSsoRedirectUri: window.location.origin + "/silent-check-sso.html"},config: {url: 'http://xxxx/auth', //keyclock登录地址clientId: 'dlp-train-front',realm: 'TechnicalMiddlePlatform'},onReady (KeycloakInstance) {console.log('Keycloak ready', KeycloakInstance)app.config.globalProperties.$keycloak = KeycloakInstance;sessionSet('token', KeycloakInstance.token)app.use(store).use(router).use(ElementPlus).mount('#app')}
})
export function login() {const loginUrl = VueKeycloakJs.createLoginUrl({ redirectUri: pageIndex });window.location.replace(loginUrl);
}
login()