Giter Club home page Giter Club logo

authentication's Introduction

Authentication Example using Spring Boot 3, Angular 16 and JWT

Authentication using Spring Boot 3 and Angular 16

  • Clone
git clone https://github.com/lelup24/authentication.git
  • run a postgresql database with docker or your own
docker compose up -d
  • start backend or run in your IDE
./gradlew bootRun
  • go to backend folder and run
npm i
  • go to frontend folder and start frontend
ng serve

RSA

openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:4096
openssl rsa -pubout -in private_key.pem -out public_key.pem

Deployment

Server Configuration

Create new sudo user

adduser peter
usermod -aG sudo peter

generate SSH-key

ssh-keygen -b 4096

Login to Server

mkdir -p ~/.ssh
sudo nano authorized_keys
chown -R peter ~/.ssh
chmod -R 700 ~/.ssh

Create Config in .ssh

sudo nano ~/.ssh/config

Add this to the config-file

Host tutorial
HostName 85.215.114.254
IdentityFile ~/.ssh/tutorial
User peter

SSH-Konfiguration

sudo nano /etc/ssh/sshd_config

Change this

PasswordAuthentication no
sudo systemctl restart ssh

Installing Backend

sudo apt-get update && sudo apt-get upgrade
sudo apt install -y openjdk-17-jdk

Postgres

sudo apt install postgresql postgresql-contrib
sudo -u postgres psql
\password postgres
ssh -L 5555:localhost:5432 tutorial

Backend (Spring Boot)

mkdir /var/backend
sudo chown peter /var/backend
sudo chmod 700 /var/backend
scp backend-0.0.1-SNAPSHOT.jar tutorial:/var/backend
sudo nano /etc/systemd/system/tutorial.service

Füge den folgenden Inhalt hinzu:

[Unit]
Description=Tutorial
After=syslog.target

[Service]
User=peter
ExecStart=/usr/bin/java -Duser.timezone=CET -DDB_URL=jdbc:postgresql://localhost:5432/tutorial -Dspring.profiles.active=prod -jar /var/backend/backend-0.0.1-SNAPSHOT.jar SuccessExitStatus=143

Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
sudo systemctl start tutorial.service
curl http://localhost:8080/api/v1/unsecured

Frontend (Angular)

sudo apt install nginx
sudo ufw allow 'Nginx Full'
sudo ufw allow 22
systemctl status nginx
cd /var/www/html/
sudo mkdir frontend
sudo chown -R peter frontend/
sudo chmod -R 755 frontend/
ng build
scp -r dist/frontend/* tutorial:/var/www/html/frontend
cd /etc/nginx/sites-available
sudo nano tutorial

Füge den folgenden Inhalt hinzu:

server {
        listen 80;
        listen [::]:80;

        root /var/www/html/frontend;
        index index.html;

        server_name talentschmiede.org;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri$args $uri$args/ /index.html;
        }

        location /api {
               proxy_pass http://localhost:8080;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-Proto $scheme;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }


       location ~* \.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$ {
        expires 1h;
        add_header Cache-Control "public, no-transform";
      }
}
sudo ln -s /etc/nginx/sites-available/tutorial /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx

Deploy with Docker

install docker

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

build

cd backend
docker build -t tutorial-backend:latest .
cd frontend
docker build -t tutorial-frontend:latest .

publish

docker login
docker tag tutorial-backend:latest lelup24/tutorial-backend:latest
docker push lelup24/tutorial-backend:latest
docker tag tutorial-frontend:latest lelup24/tutorial-frontend:latest
docker push lelup24/tutorial-frontend:latest
docker compose -f docker-compose.prod.yml up

authentication's People

Contributors

lelup24 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.