简体   繁体   中英

Keycloak authorization in Angular

I stuck in setting up a authorization.

I downloaded keycloak-3-2-1.Final and set up a new realm.

{
  "realm": "proteomics",
  "auth-server-url": "http://localhost:8080/auth",
  "ssl-required": "external",
  "resource": "account",
  "credentials": {
    "secret": "696caa69-9259-45f5-a264-0d9da083f933"
  },
  "use-resource-role-mappings": true
}

Then I started a new angular project with 'ng new'. In this project I added keycloak-js npm install --save keyclak-js

My problem now is, how do I connect all these things together. On my header.component.html I want a single button to login (authenticate). Can anyone explain where and how I have to import keycloak-js and if anything else is missing, give me a hint to finish a simple login page?

app.compnent.ts

import { Component } from '@angular/core';
import { HeaderComponent } from './header/header.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
}

app.component.html

<app-header></app-header>

header.component.ts

import { Component, OnInit } from '@angular/core';

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

  constructor() { }

  ngOnInit() {
  }
}

header.component.html

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a href="#" class="navbar-brand">ProtSkive</a>
    </div>
    <div class="navbar-collapse">
       <ul class="nav navbar-nav navbar-right">
         <button class="btn navbar-btn" id="login">Login</button> 
       </ul>    
    </div>
  </div>
</nav>

As far as I know, Kecloak is a standalone server meaning it is independent of your angular app. If you would like to have nav-bar in Keycloak you will have to change login page themes settings. Alternatively, you could implement login through REST and build your custom login page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM