如何在Angular 6中实现KeyClope?
有人能帮我把KeyClope集成到Angular 6吗? 我不知道我必须如何启动以及如何初始化Javascript适配器我使用了这个:如何在Angular 6中实现KeyClope?,angular,security,adapter,openid,keycloak,Angular,Security,Adapter,Openid,Keycloak,有人能帮我把KeyClope集成到Angular 6吗? 我不知道我必须如何启动以及如何初始化Javascript适配器我使用了这个: 这里描述的每个步骤,以及如何集成的示例也包括在内。如果您使用Angular 8+和KeyClope OpenId Connect来启用REST登录、注销、检查会话,那么您可以使用此Angular相关性: 安装 npm i ng-keycloak API import { NgKeycloakModule } from 'ng-keycloak'; #用法
这里描述的每个步骤,以及如何集成的示例也包括在内。如果您使用Angular 8+和KeyClope OpenId Connect来启用REST登录、注销、检查会话,那么您可以使用此Angular相关性: 安装
npm i ng-keycloak
API
import { NgKeycloakModule } from 'ng-keycloak';
#用法
在您的应用程序模块中注册NgKeyClope模块。
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 { }
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);
});
}
}
在组件中使用导入服务。
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 { }
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);
});
}
}
你好,Max。欢迎来到StackOverflow。请在这里添加更多信息,说明您到底想要实现什么目标。另外,请添加一些示例代码。如果你能提供一份工作,那就太好了。您可以使用创建一个。顺便说一句,这里有一个角度实现的KeyClope。你可以在这里找到它:Max to start,你可以查看快速入门教程