Spring Boot App that focuses on implementing multiple authentication methods using OAuhth2 and JWT.
The repository contains a Springboot application for users to login and register using email and password or using OAuth2 (Google). The backend app is meant to be run together with the frontend Angular application.
- Backend
- Application is built using Java 21, Spring Boot 3 and Spring Security. Dependencies are managed using Maven. Authentication is done using JWT.
- Database
- Postgresql is used as the database.
- Note: Use a terminal with bash to run the scripts.
- Start up the database and environment variables:
- Run the following script to create the security variables:
./generate_keys.sh
-
Create your credentials for using oauth2 in the google cloud console or follow the steps detailed here.
-
Run Postgresql docker container with the following command (replace {PASSWORD} with your own password):
docker run --name multiple-auth-app-postgres -e POSTGRES_PASSWORD={PASSWORD} -d -p 127.0.0.1:5432:5432 postgres
Don't change the port configuration when running in a local environment. The syntax of the mapping is for extra security, so that the database is not accessible from outside the container.
If you change the password to the postgresql database, make sure to update the password in the application.properties file of the multiple-auth-api project.
- If you need to connect to the postgresql database container, run the following command:
docker exec -it multiple-auth-app-postgres bash
- Connect to postgresql database, once inside the container:
psql -U postgres
- Start up the multiple-auth-api:
-
Note: It is strongly recommended to use an IDE for managing the .env file. Using the "run.sh" script for loading variables may have problems.
-
Run the following command from the root directory of the project (or just press the start button in your IDE (Recomended), remember to read the .env file):
./run.sh
-
Default port for the multiple-auth-api is 8080. If you want to change the port, set the "server.port" property in the application.properties file. If you change the port, make sure to update the port in the multiple-auth-ui project as well (constants.ts file, "API_BASE_URL" parameter).
-
As this project uses OAuth2, make sure to update the redirect-uri in the application.properties file and in configurations at OAuth2 providers (Google, GitHub, Twitter, etc.) too.
-
If you checked out from main branch, you will see that application.properties contains references to environment variables. Make sure to edit a .env file in the root of the project, and list all the required properties there as key-value pairs (don't forget to exclude this file from version control):
DATABASE_USER=postgres
DATABASE_PASSWORD=K29r8Dhc79n2gPG86CRhoVt9NBxTa0Gk
...
- Start up the multiple-auth-ui:
-
Run the following command from the root directory of the project:
npm install
-
Run the following command from the root directory of the project:
npm run start
-
Default port for the multiple-auth-ui is 4200. If you want to change the port, update the port in the angular.json file. On the following path: multiple-auth-ui -> architect -> serve add this:
"options": { "port": [desired-port] }
- Make sure to update authorized redirect uri usage places in the multiple-auth-api too.
-
The use of oauth2 has been implemented according to the following repository, if you find this project useful, please visit the following repository and follow the step-by-step creation project: You can find the corresponding repository here.