Git Product home page Git Product logo

germanfica / tut-keycloak-angular-spring-boot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cavanosa/tutorial-keycloak-angular-spring-boot

5.0 1.0 0.0 774 KB

A full stack project. Stack: Angular 13 + Spring Boot 2.6.2 + Keycloak 16.1.0 authentication. I'm following the Lugi Code Spanish tutorial: https://youtube.com/playlist?list=PL4bT56Uw3S4wEZ0Sp7jrGAX8DMS-MKowg

Java 23.83% JavaScript 2.93% TypeScript 55.42% HTML 17.65% SCSS 0.16%
angular13 spring-boot java11 keycloak

tut-keycloak-angular-spring-boot's Introduction

Angular 13 + Spring Boot 2.6.2 + OAuth 2.0 Keycloak authentication

This repository provides an angular and maven project for managing a foo’s information.

✨ Getting Started πŸš€ Download

keykloak_preview

Getting Started

Follow the below instructions to get started with Guess the Number Game source code:

Requirements

Make sure you have the below requirements before starting:

Download

You can get access to the source code by using one of the following ways:

  • ✨ Download Source Code
  • πŸ”₯ Clone the repository locally:
git clone https://github.com/germanfica/keycloak-angular-spring-boot.git

NPM packages

Maven dependencies

Maven commands

  • mvn clean
  • mvn clean install

Configure application properties

Open src/main/resources/application.properties

server.port=8080

# keycloak
keycloak.realm = myrealm
keycloak.auth-server-url = http://localhost:8180/auth
keycloak.ssl-required = external
keycloak.resource = backend-client
keycloak.use-resource-role-mappings = true
keycloak.bearer-only = true

# keycloak master realm
app.master.realm = master
app.master.username = admin
app.master.password = admin
app.master.clientId = admin-cli

Angular basic settings

Open src/environments/environment.ts

export const environment = {
  production: false,
  foo_api: 'http://localhost:8080/foo/',
  user_api: 'http://localhost:8080/user/',
  authResourceServerConfig: {
    allowedUrls: [
      'http://localhost:8080/foo'
    ],
    sendAccessToken: true
  },
  authConfig: {
    issuer: 'http://localhost:8180/auth/realms/myrealm',
    redirectUri: window.location.origin,
    clientId: 'frontend-client',
    responseType: 'code',
    scope: 'openid profile email offline_access',
    showDebugInformation: true,
  }
};

The http://localhost:8080/foo URL corresponds to the backend FooController in our Spring Boot project.

Import the OAuthModule to src/app/app.module.ts

OAuthModule.forRoot({
  resourceServer: environment.authResourceServerConfig
})

βš™οΈ Keycloak basic settings

🌍 Realm
Name: myrealm
πŸ‘€ Clients
Client ID: backend-client
Access Type: bearer-only
Client ID: frontend-client
Valid Redirect URIs: http://localhost:4200/*
Web Origins: *
πŸ”’ Client roles
Client ID: backend-client
Roles: ROLE_USER, ROLE_MODERATOR, ROLE_ADMIN
πŸ”’ Realm roles
Role Name: realm-admin
Composite Roles:
  Client: backend-client
    Associated Roles: ROLE_ADMIN
Role Name: realm-user
Composite Roles:
  Client: backend-client
    Associated Roles: ROLE_USER
😲 Users
Username: admin
Email: admin@localhost
Realm Roles: realm-admin, realm-user
Username: user
Email: admin@localhost
Realm Roles: realm-user

Keycloak Getting Started

Get started with Keycloak. A useful getting started guide can be found in the official documentation. But I strongly recommend you to follow the instructions below, as there are additional things we will need to do for this project.

Or alternatively, you can also watch this YouTube video AutenticaciΓ³n Keycloak + Angular 10 + Spring-Boot full-stack: Parte 2.

Table of Contents

Update default Keycloak port (Alternative 1 - Recommended)

Adjusting the port used by Keycloak (Alternative 2)

Start Keycloak

Create an admin user

Login to the admin console

Create a realm

Create a backend client

Create a frontend client

Naming Security Roles

Create client roles

Create realm roles

Create a user

Login to account console

Assign roles to a user

Update default Keycloak port (Alternative 1 - Recommended)

The default port is 8080. Go toΒ standalone/configuration/standalone.xmlΒ in the Keycloak installation folder and look forΒ jboss.http.port property. We want to change the default port to 8180.

From this:

<socket-binding name="http" port="${jboss.http.port:8080}"/>

To this:

<socket-binding name="http" port="${jboss.http.port:8180}"/>

Adjusting the port used by Keycloak (Alternative 2)

You can check the following articles:

Start Keycloak server by supplying a value for the jboss.socket.binding.port-offset system property. This value is added to the base value of every port opened by the Keycloak server. In this example, 100 is the value.

On Linux run:

$ cd bin
$ ./standalone.sh -Djboss.socket.binding.port-offset=100

On Windows run:

> ...\bin\standalone.bat -Djboss.socket.binding.port-offset=100

And finally Confirm that the Keycloak server is running. Go to http://localhost:8180/auth/admin/ . If the admin console opens, you are ready to continue this guide.

Start Keycloak

From a terminal open the directory keycloak-16.1.0, then to start Keycloak run the following command.

On Linux run:

bin/standalone.sh

On Windows run:

bin/standalone.bat

Create an admin user

Keycloak does not come with a default admin user, which means before you can start using Keycloak you need to create an admin user.

To do this open http://localhost:8180/auth, then fill in the form with your preferred username and password.

Login to the admin console

Go to the Keycloak Admin Console and login with the username and password you created earlier.

🌍 Create a realm

A realm in Keycloak is the equivalent of a tenant. It allows creating isolated groups of applications and users. By default there is a single realm in Keycloak called master. This is dedicated to manage Keycloak and should not be used for your own applications.

Let’s create our first realm.

  1. Open the Keycloak Admin Console

  2. Hover the mouse over the dropdown in the top-left corner where it says Master, then click on Add realm

  3. Fill in the form with the following values:

    • Name: myrealm
  4. Click Create

add-realm

πŸ‘€ Create a backend client

We will need 2 (two) clients, one for the backend and one for the frontend in our realm, so let’s create them:

  1. Open the Keycloak Admin Console

  2. Click Clients (left-hand menu)

    • Click Create (top-right corner of table)
  3. Fill in the form with the following values:

    • Client ID: backend-client

    • Client Protocol: openid-connect

  4. Click Save

backend-client

The client will need a bearer-only access type. To do this:

  1. Click Settings (top of the page)

  2. Fill in the form with the following values:

    • Access Type: bearer-only
  3. Click Save

backend-client-access-type

πŸ‘€ Create a frontend client

Now let's create the client for the frontend:

  1. Open the Keycloak Admin Console

  2. Click Clients (left-hand menu)

    • Click Create (top-right corner of table)
  3. Fill in the form with the following values:

    • Client ID: frontend-client

    • Client Protocol: openid-connect

  4. Click Save

frontend-client

The client will need a Valid URI pattern in order to be able to redirect to after a successful login or logout. To do this:

  1. Click Settings (top of the page)

  2. Fill in the form with the following values:

    • Valid Redirect URIs: http://localhost:4200/*

    • Web Origins: *

  3. Click Save

Frontend-Client-Settings

πŸ”€ Naming Security Roles

Spring Security, when using role-based authentication, requires that role names start with ROLE_. For example, an administrator role must be declared in Keycloak as ROLE_ADMIN or similar, not simply ADMIN.

References

πŸ”’ Create client roles

Initially there are no roles in a client, so let’s create one:

  1. Click Clients (left-hand menu)

    • Click backend-client (Client ID column of table)
  2. Click Roles (top of the page)

  3. Click Add Role (top-right corner of table)

  4. Fill in the form with the following values:

    • Role Name: ROLE_USER

    • Web Origins: *

  5. Click Save

backend-client-user-role

Repeat the above steps but now with the following role names:

  • ROLE_MODERATOR
  • ROLE_ADMIN

πŸ”’ Create realm roles

We will now need two roles for our realm. So let's create them:

Create realm-admin role

  1. Open the Keycloak Admin Console

  2. Click Roles (left-hand menu)

    • Click Add Role (top-right corner of table)
  3. Fill in the form with the following values:

    • Role Name: realm-admin
  4. Click Save

realm-admin

The realm-admin role will need Composite Roles enabled, as we want to associate backend-client roles to it. To do this:

  1. Click Details (top of the page)

  2. Click ON next to Composite Roles

    • Select backend-client next to Client Roles
  3. Add in the Associated Roles the following roles:

    • ROLE_ADMIN

admin-composite-roles

Create realm-user role

  1. Open the Keycloak Admin Console

  2. Click Roles (left-hand menu)

    • Click Add Role (top-right corner of table)
  3. Fill in the form with the following values:

    • Role Name: realm-user
  4. Click Save

realm-user

The realm-user role will need Composite Roles enabled, as we want to associate backend-client roles to it. To do this:

  1. Click Details (top of the page)

  2. Click ON next to Composite Roles

    • Select backend-client next to Client Roles
  3. Add in the Associated Roles the following roles:

    • ROLE_USER

user-composite-roles

😲 Create a user

Initially there are no users in a new realm, so let’s create one:

  1. Open the Keycloak Admin Console

  2. Click Users (left-hand menu)

    • Click Add user (top-right corner of table)
  3. Fill in the form with the following values:

    • Username: myuser

    • First Name: Your first name

    • Last Name: Your last name

  4. Click Save

add-user

The user will need an initial password set to be able to login. To do this:

  1. Click Credentials (top of the page)

  2. Fill in the Set Password form with a password

  3. Click ON next to Temporary to prevent having to update password on first login

set-password

Repeat the above steps but now with the following user names:

  • admin
  • user

Login to account console

Let’s now try to login to the account console to verify the user is configured correctly.

  1. Open the Keycloak Account Console

  2. Login with myuser and the password you created earlier

You should now be logged-in to the account console where users can manage their accounts.

account-console

πŸ”’ Assign roles to a user

Assign roles to admin

  1. Open the Keycloak Admin Console

  2. Click Users (left-hand menu)

    • Click admin (ID column of table)
  3. Click Role Mappings (top of the page)

  4. In Realm Roles add the following Available Roles:

    • realm-admin
    • realm-user

admin-role-mappings

Assign roles to user

  1. Open the Keycloak Admin Console

  2. Click Users (left-hand menu)

    • Click user (ID column of table)
  3. Click Role Mappings (top of the page)

  4. In Realm Roles add the following Available Roles:

    • realm-user

user-role-mappings

Credits

tut-keycloak-angular-spring-boot's People

Contributors

germanfica avatar cavanosa avatar

Stargazers

Luca Fulgenzi avatar Maxime Loukhal avatar Benoit Dubois avatar Maksym Riabov avatar  avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.