简体   繁体   English

基于 Angular 角色的身份验证 keycloak 和 SpringBoot

[英]Angular role based authentification keycloak and SpringBoot

I would like to set up an authentication between my Angular front and my SpringBoot backend.我想在我的 Angular 前端和 SpringBoot 后端之间设置一个身份验证。 I have a SpringBoot authentication server configured with SpringBoot and functional.我有一个配置了 SpringBoot 和功能的 SpringBoot 身份验证服务器。 The Front can authenticate to Keycloak and have access to the SpringBoot API. Front 可以对 Keycloak 进行身份验证并访问 SpringBoot API。

The problem is that I want to know how to know if the user is logged in or not to display the login button.问题是我想知道如何知道用户是否已登录以显示登录按钮。

And the second thing is that I would like some pages to be displayed and others not, depending on the user's role.第二件事是我希望显示某些页面而其他页面不显示,这取决于用户的角色。

And as a bonus if you know... I would like to display some parts of a DOM depending on the user's role.如果你知道的话,作为奖励......我想根据用户的角色显示 DOM 的某些部分。

(this is the first time I set up this system... thanks for the help) No problem on Keycloak and Spring side, connection is OK and redirection to Angular OK too. (这是我第一次设置这个系统...感谢您的帮助) Keycloak 和 Spring 方面没问题,连接正常,重定向到 Angular 也正常。 Bearer saved OK不记名保存确定

app.component.ts app.component.ts

import { Component } from '@angular/core';
import { OAuthService, NullValidationHandler, AuthConfig } from 'angular-oauth2-oidc';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  
  constructor(private oauthService: OAuthService) {
    this.configure();
  }
authConfig: AuthConfig = {
    issuer: 'http://localhost:8081/auth/realms/api-team', // Keycloak Server
    redirectUri: window.location.origin + "/api-team", // My SpringBoot
    clientId: 'api-team-client-angular',
    scope: 'openid profile email offline_access api-angular',
    responseType: 'code',
    // at_hash is not present in JWT token
    // disableAtHashCheck: true,
    showDebugInformation: true
  }
  
  public login() {
    this.oauthService.initLoginFlow();
  }
  
  public logoff() {
    this.oauthService.logOut();
  }
  
  private configure() {
    this.oauthService.configure(this.authConfig);
    this.oauthService.tokenValidationHandler = new  NullValidationHandler();
    this.oauthService.loadDiscoveryDocumentAndTryLogin();
  }
}

Just a test.. app.component.html只是一个测试.. app.component.html

<router-outlet>
  <button class="btn btn-default" (click)="login()">
    Login
  </button>
  <button class="btn btn-default" (click)="logoff()">
    Logout
  </button>
    <app-loader></app-loader>
</router-outlet>

One of my router我的路由器之一

export const MaterialRoutes: Routes = [
    {
        path: '',
        children: [
            // {
            //     path: 'services-namespace',
            //     component: ServicesProjetComponent,
            //     data: {
            //         title: "Composants d'un projet",
            //         urls: [
            //             { title: 'Dashboard', url: '/dashboard1' },
            //             { title: "Composants d'un projet" }
            //         ]
            //     }
            // },
            {
                path: 'namespaces',
                component: ProjetsComponent,
                data: {
                    title: "Liste des Projets",
                    urls: [
                        { title: 'Dashboard', url: '/dashboard1' },
                        { title: "Liste de Projets" }
                    ]
                }
            }

All you need to add is Angular guards (and maybe an authentication service).您需要添加的只是 Angular 防护(可能还有身份验证服务)。

I've setup a minimal mono-repo for a meetup about Open ID with very similar stack: spring RESTful API (ok it's servlet, not reactive), Angular front-end and Keycloak authorization server.我已经为一个关于 Open ID的聚会设置了一个最小的mono-repo,堆栈非常相似:spring RESTful API(它是 servlet,不是响应式)、Angular 前端和 Keycloak 授权服务器。

I suggest you clone and hack it.我建议你克隆并破解它。 For Angular specific parts, have a close look at:对于 Angular 特定部分,请仔细查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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