Code Monkey home page Code Monkey logo

ivangfr / springboot-react-basic-auth Goto Github PK

View Code? Open in Web Editor NEW
52.0 2.0 18.0 14.69 MB

The goal of this project is to implement an application called book-app to manage books. For it, we will implement a back-end Spring Boot application called book-api and a font-end React application called book-ui. Besides, we will use Basic Authentication to secure both applications.

Java 47.05% Shell 9.10% HTML 2.40% JavaScript 40.95% CSS 0.50%
spring-boot basic-authentication semantic-ui-react docker java javascript npm springdoc-openapi spring-security web

springboot-react-basic-auth's Introduction

springboot-react-basic-auth

The goal of this project is to implement an application called book-app to manage books. For it, we will implement a back-end Spring Boot application called book-api and a font-end React application called book-ui. Besides, we will use Basic Authentication to secure both applications.

Proof-of-Concepts & Articles

On ivangfr.github.io, I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.

Additional Readings

Project Diagram

project-diagram

Applications

  • book-api

    Spring Boot Web Java backend application that exposes a Rest API to create, retrieve and delete books. If a user has ADMIN role he/she can also retrieve information of other users or delete them.

    The application secured endpoints can just be just accessed if a user has valid credentials (username and password) and has autorization roles for it.

    book-api stores its data in Postgres database.

    book-api has the following endpoints

    Endpoint Secured Roles
    POST /auth/authenticate -d {"username","password"} No
    POST /auth/signup -d {"username","password","name","email"} No
    GET /public/numberOfUsers No
    GET /public/numberOfBooks No
    GET /api/users/me Yes ADMIN, USER
    GET /api/users Yes ADMIN
    GET /api/users/{username} Yes ADMIN
    DELETE /api/users/{username} Yes ADMIN
    GET /api/books [?text] Yes ADMIN, USER
    POST /api/books -d {"isbn","title"} Yes ADMIN
    DELETE /api/books/{isbn} Yes ADMIN
  • book-ui

    React frontend application where a user with role USER can retrieve the information of a specific book or a list of books. On the other hand, a user with role ADMIN has access to all secured endpoints.

    To login, a user or admin must provide valid username and password credentials. book-ui communicates with book-api to get books and users data.

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

Prerequisites

Start Environment

  • In a terminal, make sure you are inside springboot-react-basic-auth root folder

  • Run the following command to start docker compose containers

    docker compose up -d
    

Running book-app using Maven & Npm

  • book-api

    • Open a terminal and navigate to springboot-react-basic-auth/book-api folder

    • Run the following Maven command to start the application

      ./mvnw clean spring-boot:run
      
  • book-ui

    • Open another terminal and navigate to springboot-react-basic-auth/book-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
book-api http://localhost:8080/swagger-ui.html
book-ui http://localhost:3000 admin/admin, user/user or signing up a new user

Note: the credentials shown in the table are the ones already pre-defined. You can signup new users.

Demo

  • The gif below shows a user loging in

    user-login

  • The gif below shows an admin loging in

    admin-login

Testing book-api Endpoints

  • Manual Endpoints Test using Swagger

    • Open a browser and access http://localhost:8080/swagger-ui.html. All endpoints with the lock sign are secured. In order to access them, you need a valid username and password credentials.

    • Click Authorize button (white/green one, almost at the top of the page, right side)

    • In the Basic authentication form that will open, provide the admin credentials (admin/admin) or user ones (user/user). Then, click Authorize and, finally, click Close button.

    • Make some call to the endpoints

  • Manual Endpoints Test using curl

    • Open a terminal

    • Call GET /public/numberOfBooks

      curl -i localhost:8080/public/numberOfBooks
      

      It should return

      HTTP/1.1 200
      70
      
    • Call GET /api/books without credentials

      curl -i localhost:8080/api/books
      

      As this endpoint requires authentication, it should return

      HTTP/1.1 401
      { "timestamp": "...", "status": 401, "error": "Unauthorized", "message": "Unauthorized", "path": "/api/books" }
      
    • Call again GET /api/books but now with user credentials

      curl -i -u user:user localhost:8080/api/books
      

      It should return

      HTTP/1.1 200
      [
        {"isbn":"978-1-60309-445-0","title":"A Shining Beacon"},
        {"isbn":"978-1-891830-85-3","title":"American Elf (Book 2)"},
        ...
      ]
      
    • Call POST /api/books with user credentials

      curl -i -u user:user -X POST localhost:8080/api/books \
      -H "Content-Type: application/json" -d '{"isbn": "9781617292545", "title": "Spring Boot in Action"}'
      

      As user doesn't have the role ADMIN, it should return

      HTTP/1.1 403
      { "timestamp": "...", "status": 403, "error": "Forbidden", "message": "Forbidden", "path": "/api/books" }
      
    • Call POST /api/books with admin credentials

      curl -i -u admin:admin -X POST localhost:8080/api/books \
      -H "Content-Type: application/json" -d '{"isbn": "9781617292545", "title": "Spring Boot in Action"}'
      

      It should return

      HTTP/1.1 201
      { "isbn":"9781617292545","title":"Spring Boot in Action" }
      
  • Automatic Endpoints Test

    • Open a terminal and make sure you are in springboot-react-basic-auth root folder

    • Run the following script

      ./book-api/test-endpoints.sh
      

      It should return something like the output below, where it shows the http code for different requests

      POST auth/authenticate
      ======================
      admin Auth Resp: {"id":1,"name":"Admin","role":"ADMIN"}
      
      POST auth/authenticate
      ======================
      user Auth Resp: {"id":2,"name":"User","role":"USER"}
      
      POST auth/signup
      ================
      user2 Auth Resp: {"id":3,"name":"User2","role":"USER"}
      
      Authorization
      =============
                      Endpoints | without creds |  user creds |  admin creds |
      ------------------------- + ------------- + ----------- + ------------ |
       GET public/numberOfUsers |           200 |         200 |          200 |
       GET public/numberOfBooks |           200 |         200 |          200 |
      ......................... + ............. + ........... + ............ |
              GET /api/users/me |           401 |         200 |          200 |
                 GET /api/users |           401 |         403 |          200 |
           GET /api/users/user2 |           401 |         403 |          200 |
        DELETE /api/users/user2 |           401 |         403 |          200 |
      ......................... + ............. + ........... + ............ |
                 GET /api/books |           401 |         200 |          200 |
                POST /api/books |           401 |         403 |          201 |
          DELETE /api/books/abc |           401 |         403 |          200 |
      ------------------------------------------------------------------------
       [200] Success -  [201] Created -  [401] Unauthorized -  [403] Forbidden
      

Util Commands

  • Postgres
    docker exec -it postgres psql -U postgres -d bookdb
    \dt
    

Shutdown

  • To stop book-api and book-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-basic-auth root folder, run the command below

    docker compose down -v
    

How to upgrade book-ui dependencies to latest version

  • In a terminal, make sure you are in springboot-react-basic-auth/book-ui folder

  • Run the following commands

    npm upgrade
    npm i -g npm-check-updates
    ncu -u
    npm install
    

References

springboot-react-basic-auth's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

springboot-react-basic-auth's Issues

user table with database

Sorry for writing here, but unfortunately I can't write elsewhere. My question is, where is the implementation of "localStorage" used in AuthContext.js? As far as I can see, you upload it in the backend without a database. Is it feasible to upload from a database?

Logout

Hello! I didn't find a method in any of controllers to logout. How can I do it? I've tried to make request on "/logout" because it's basic endpoint in spring security, but it doesn't work. (Well, it was on my own project, but basic logic don't changes.)

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.