How to implement Keycloak in Angular 6?

我的未来我决定 提交于 2020-01-16 05:29:09

问题


Can anybody help me with the integration from Keycloak in to Angular 6 ? I don't know how I must start and how to initialize the Javascript Adapter


回答1:


I used this one: https://github.com/mauriciovigolo/keycloak-angular

Every step described there, and an example how to integrate also included.




回答2:


If you are using Angular 8+ and Keycloak OpenId Connect for enabling REST Login, Logout, Check Session then you can use this angular dependency:

Angular Keycloak Dependency for version 2+ tested for Angular version 8+

Installation

npm i ng-keycloak

API

import { NgKeycloakModule } from 'ng-keycloak';

# Usage

Register the NgKeycloakModule in your app module.

import { NgKeycloakModule } from 'ng-keycloak';

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NgKeycloakModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Use the import NgKeycloakService in your component.

import { Component, OnInit } from '@angular/core';
import { NgKeycloakService } from 'ng-keycloak';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  username = 'YOUR_KEYCLOAK_USERNAME_TO_LOGIN';
  password = 'YOUR_KEYCLOAK_PASSWORD_TO_LOGIN';

  // The BASE_URL is empty in case the proxy-config is used from Angular to resolve the CORS error
  // If the CORS error is not present use the BASE URL as the keycloak url with the port number
  // Example BASE_URL = 'http://13.43.53.42:30224'
  keycloakConfig = {
    BASE_URL: '',
    realm: 'YOUR_REALM_NAME',
    clientId: 'YOUR_CLIENT_ID',
    credentials: {
        secret: 'YOUR_CLIENT_SECRET'
    }
  };

  constructor(private ngKeycloakService: NgKeycloakService) { }

  ngOnInit(): void {

    // You need to set the Keycloak Configuration using _setkeycloakConfig(config) method before you
    // can use the Library
    this.ngKeycloakService._setkeycloakConfig(keycloakConfig);

    this.ngKeycloakService.logout().pipe().subscribe(logoutSuccessResponse => {
      console.log('Logout Success Response', logoutSuccessResponse);
    }, (logoutErrorResponse) => {
      console.log('Logout Error', logoutErrorResponse);
    });

    this.ngKeycloakService.login(this.username, this.password).pipe().subscribe(loginSuccessResponse => {
      console.log('Login Success', loginSuccessResponse);
    }, (loginErrorResponse) => {
      console.log('Login Error Response', loginErrorResponse);
    });

    this.ngKeycloakService.isLoggedIn().pipe().subscribe(loginStatusResponse => {
      console.log('Login Check Status', loginStatusResponse);
    }, (loginStatusErrorResponse) => {
      console.log('Login Check Status Error', loginStatusErrorResponse);
    });
  }

}


来源:https://stackoverflow.com/questions/53695864/how-to-implement-keycloak-in-angular-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!