Anjan Kumar
Posted on June 25, 2020
What is Keycloak?
- An Open Source Identity and Access Management for Modern Applications and Services.
- One of the opensource implementation of OpenID protocol.
- Simple Identity Layer on top of OAuth2.0 protocol
Why do we need Keycloak in Angular?
- To secure our application from unauthorized access
- To identify who is the user of the application, to show the relevant content & authorized content.
Prerequisites
- Angular 6/+ Application
Steps to Integrate Keycloak in Angular Application
- Install Keycloak JS package
npm i --save keycloak-js
- Install Keycloak Angular package
npm i --save keycloak-angular
- Keycloak Server Configuration
- Keycloak Initialization
- Create a file AppInit.ts in the project src directory
-
Integrating into AppModule (Refer the image below these points)
- Import
KeycloakAngularModule
inAppModule
- Register Provider
KeyloackService
- Register Provider of type
APP_INITIALIZER
withinitializer
we defined inAppInit.ts
- Register Provider
AuthService
(You can find theAuthService
implementation below)
- Import
-
Guard configuration
- Create a Guard
ng new g auth
- Change the definition of the class as below, notice the
extends
- Routing Module Configuration,
Use the
AuthGuard
for routes that you want to restrict access, Assign required roles for each route indata
attribute,roles
parameter as an array.
- Create a Guard
Start the application
ng s
, then you will have the login screen provided by the Keycloak, log in with the correct user to access your application who has the roles that are assigned in the routing configuration
💖 💪 🙅 🚩
Anjan Kumar
Posted on June 25, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.