All Projects → ivangfr → springboot-react-keycloak

ivangfr / springboot-react-keycloak

Licence: other
The goal of this project is to secure movies-app using Keycloak(with PKCE). movies-app consists of two applications: one is a Spring Boot Rest API called movies-api and another is a ReactJS application called movies-ui.

Programming Languages

javascript
184084 projects - #8 most used programming language
java
68154 projects - #9 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to springboot-react-keycloak

semantic-ui-react-typescript-examples
Several examples using Semantic UI, React, and TypeScript
Stars: ✭ 41 (-78.65%)
Mutual labels:  semantic-ui-react
spid-keycloak-provider
Italian SPID authentication provider for Keycloak (https://www.keycloak.org/)
Stars: ✭ 48 (-75%)
Mutual labels:  keycloak
keycloak-spring-boot-rest-angular-demo
Demo for configuring Keycloak authentication for a spring-boot rest service and AngularJs web client
Stars: ✭ 24 (-87.5%)
Mutual labels:  keycloak
BarterOnly
An ecommerce platform to buy or exchange items at your convenience
Stars: ✭ 16 (-91.67%)
Mutual labels:  semantic-ui-react
react-resume
Resume Builder in React
Stars: ✭ 70 (-63.54%)
Mutual labels:  semantic-ui-react
steam-openid-connect-provider
Steam OpenID Connect Identity Provider (IdP)
Stars: ✭ 40 (-79.17%)
Mutual labels:  keycloak
keycloak-springsecurity5-sample
Spring Security 5 OAuth2 Client/OIDC integration with Keycloak sample
Stars: ✭ 55 (-71.35%)
Mutual labels:  keycloak
dinivas
AWS, GCP alternative on premise. Dinivas manage your private Cloud (OpenStack) infrastructure by providing many features based on popular Open Source projects
Stars: ✭ 15 (-92.19%)
Mutual labels:  keycloak
keycloak-dropwizard-integration
This project shows how JBoss Keycloak and Dropwizard can be used together.
Stars: ✭ 49 (-74.48%)
Mutual labels:  keycloak
keycloak-kubernetes
Keycloak deployment into Kubernetes cluster
Stars: ✭ 25 (-86.98%)
Mutual labels:  keycloak
keycloak-phone-provider
A Keycloak provider which phone and SMS
Stars: ✭ 83 (-56.77%)
Mutual labels:  keycloak
oso-backend
Heart of the oso project.
Stars: ✭ 12 (-93.75%)
Mutual labels:  keycloak
k8s-pixy-auth
k8s plugin to authenticate against an OIDC compatible issuer using PKCE (pixy) flow
Stars: ✭ 24 (-87.5%)
Mutual labels:  pkce
oidckube
Wrapper for minikube that provisions and integrates it with Keycloak
Stars: ✭ 40 (-79.17%)
Mutual labels:  keycloak
keycloak-protocol-cas
CAS protocol provider for Keycloak
Stars: ✭ 67 (-65.1%)
Mutual labels:  keycloak
microservices-go-grpc
Go microservices with REST, and gRPC for demonstrating the BFF pattern. This repository contains backend services. Everything is dockerized and ready to "Go" :-)
Stars: ✭ 151 (-21.35%)
Mutual labels:  semantic-ui-react
keycloakify-demo-app
Demo GitHub Actions setup for react projects that uses keycloakify
Stars: ✭ 34 (-82.29%)
Mutual labels:  keycloak
nest-keycloak-connect
keycloak-nodejs-connect module for Nest
Stars: ✭ 174 (-9.37%)
Mutual labels:  keycloak
keycloak-admin-go
Keycloak Admin REST client for go
Stars: ✭ 17 (-91.15%)
Mutual labels:  keycloak
e-shop
Sample Spring Cloud microservices e-shop.
Stars: ✭ 48 (-75%)
Mutual labels:  keycloak

springboot-react-keycloak

The goal of this project is to secure movies-app using Keycloak(with PKCE). movies-app consists of two applications: one is a Spring Boot Rest API called movies-api and another is a ReactJS application called movies-ui.

Project diagram

project-diagram

Applications

  • movies-api

    Spring Boot Web Java backend application that exposes a REST API to manage movies. Its secured endpoints can just be accessed if an access token (JWT) issued by Keycloak is provided.

    movies-api stores its data in a Mongo database.

    movie-api has the following endpoints

    Endpoint Secured Roles
    GET /api/userextras/me Yes MOVIES_MANAGER and USER
    POST /api/userextras/me -d {avatar} Yes MOVIES_MANAGER and USER
    GET /api/movies No
    GET /api/movies/{imdbId} No
    POST /api/movies -d {"imdb","title","director","year","poster"} Yes MOVIES_MANAGER
    DELETE /api/movies/{imdbId} Yes MANAGE_MOVIES
    POST /api/movies/{imdbId}/comments -d {"text"} Yes MOVIES_MANAGER and USER
  • movies-ui

    ReactJS frontend application where users can see and comment movies and admins can manage movies. In order to access the application, user / admin must login using his/her username and password. Those credentials are handled by Keycloak. All the requests coming from movies-ui to secured endpoints in movies-api have a access token (JWT) that is generated when user / admin logs in.

    movies-ui uses Semantic UI React as CSS-styled framework.

Prerequisites

  • Java 11+

  • npm

  • Docker

  • Docker-Compose

  • jq

  • OMDb API KEY

    To use the Wizard option to search and add a movie, you need to get an API KEY from OMDb API. In order to do it, access https://www.omdbapi.com/apikey.aspx and follow the steps provided by the website.

    Once you have the API KEY, create a file called .env.local in springboot-react-keycloak/movies-ui folder with the following content

    REACT_APP_OMDB_API_KEY=<your-api-key>
    

PKCE

As Keycloak supports PKCE (Proof Key for Code Exchange) since version 7.0.0, we are using it in this project.

Start Environment

  • In a terminal and inside springboot-react-keycloak root folder run

    docker-compose up -d
    
  • Wait for all Docker containers to be up and running. To check it, run

    docker-compose ps
    

Running movies-app using Maven & Npm

  • movies-api

    • Open a terminal and navigate to springboot-react-keycloak/movies-api folder

    • Run the following Maven command to start the application

      ./mvnw clean spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.port=9080"
      

      Once the startup finishes, KeycloakInitializerRunner.java class will run and initialize company-services realm in Keycloak. Basically, it will create:

      • Realm: company-services
      • Client: movies-app
      • Client Roles: MOVIES_MANAGER and USER
      • Two users
        • admin: with roles MANAGE_MOVIES and USER
        • user: only with role USER
    • Social Identity Providers like Google, Facebook, Twitter, GitHub, etc can be configured by following the steps described in Keycloak Documentation

  • movies-ui

    • Open another terminal and navigate to springboot-react-keycloak/movies-ui folder

    • Run the command below if you are running the application for the first time

      npm install
      
    • Run the npm command below to start the application

      npm start
      

Applications URLs

Application URL Credentials
movie-api http://localhost:9080/swagger-ui/index.html Access Token
movie-ui http://localhost:3000 admin/admin or user/user
Keycloak http://localhost:8080/admin admin/admin

Demo

  • The gif below shows an admin logging in and adding one movie using the wizard feature

    demo-admin

  • The gif below shows a user logging in using his Github account; then he changes his avatar and comment a movie

    demo-user-github

Testing movies-api endpoints

You can manage movies by accessing directly movies-api endpoints using the Swagger website or curl. However, for the secured endpoints like POST /api/movies, PUT /api/movies/{id}, DELETE /api/movies/{id}, etc, you need to inform an access token issued by Keycloak.

Getting Access Token

  • Open a terminal

  • Run the following commands to get the access token

    ACCESS_TOKEN="$(curl -s -X POST \
      "http://localhost:8080/realms/company-services/protocol/openid-connect/token" \
      -H "Content-Type: application/x-www-form-urlencoded" \
      -d "username=admin" \
      -d "password=admin" \
      -d "grant_type=password" \
      -d "client_id=movies-app" | jq -r .access_token)"
    
    echo $ACCESS_TOKEN
    

Calling movies-api endpoints using curl

  • Trying to add a movie without access token

    curl -i -X POST "http://localhost:9080/api/movies" \
      -H "Content-Type: application/json" \
      -d '{ "imdbId": "tt5580036", "title": "I, Tonya", "director": "Craig Gillespie", "year": 2017, "poster": "https://m.media-amazon.com/images/M/MV5BMjI5MDY1NjYzMl5BMl5BanBnXkFtZTgwNjIzNDAxNDM@._V1_SX300.jpg"}'
    

    It should return

    HTTP/1.1 401
    
  • Trying again to add a movie, now with access token (obtained at #getting-access-token)

    curl -i -X POST "http://localhost:9080/api/movies" \
      -H "Authorization: Bearer $ACCESS_TOKEN" \
      -H "Content-Type: application/json" \
      -d '{ "imdbId": "tt5580036", "title": "I, Tonya", "director": "Craig Gillespie", "year": 2017, "poster": "https://m.media-amazon.com/images/M/MV5BMjI5MDY1NjYzMl5BMl5BanBnXkFtZTgwNjIzNDAxNDM@._V1_SX300.jpg"}'
    

    It should return

    HTTP/1.1 201
    {
      "imdbId": "tt5580036",
      "title": "I, Tonya",
      "director": "Craig Gillespie",
      "year": "2017",
      "poster": "https://m.media-amazon.com/images/M/MV5BMjI5MDY1NjYzMl5BMl5BanBnXkFtZTgwNjIzNDAxNDM@._V1_SX300.jpg"
    }
    
  • Getting the list of movies. This endpoint does not requires access token

    curl -i http://localhost:9080/api/movies
    

    It should return

    HTTP/1.1 200
    [
      {
        "imdbId": "tt5580036",
        "title": "I, Tonya",
        "director": "Craig Gillespie",
        "year": "2017",
        "poster": "https://m.media-amazon.com/images/M/MV5BMjI5MDY1NjYzMl5BMl5BanBnXkFtZTgwNjIzNDAxNDM@._V1_SX300.jpg",
        "comments": []
      }
    ]
    

Calling movies-api endpoints using Swagger

Useful Commands

  • MongoDB

    List all movies

    docker exec -it mongodb mongo moviesdb
    db.movies.find()
    

    Type exit to get out of MongoDB shell

Shutdown

  • To stop movies-api and movies-ui, go to the terminals where they are running and press Ctrl+C

  • To stop and remove docker-compose containers, network and volumes, go to a terminal and, inside springboot-react-keycloak root folder, run the command below

    docker-compose down -v
    

How to upgrade movies-ui dependencies to latest version

  • In a terminal, make sure you are in springboot-react-keycloak/movies-ui folder

  • Run the following commands

    npm upgrade
    npm i -g npm-check-updates
    ncu -u
    npm install
    
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].