Skip to content

saliabbasraza/springboot-sample-jwt

Repository files navigation

springboot-sample

Spring boot testing project for play around features for spring boot, test cases and api docs.

Current active working branch is development.

This project makes use of Project Lombok, so you need to setup your IDE to use lombok, just dropin a jar. More details at: https://projectlombok.org/

How to Run:

  • Requires Java 8, Maven 4, Lombok

  • Checkout the project

  • navigate to development branch.

  • Navigate to project root, issue command: mvn spring-boot:run

  • Navigate to: http://localhost:8080/users

How to Kill app:

It is noticed that Ctrl+c won't terminate the springboot app. The solution on windows is to find the id and kill the process:

d:\> netstat -ano | find "8080"
d:\> taskkill /F /PID 19276 -- 19726 is process id retrieved in previous step

How to Create executable jar:

  • run command mvn package. Will generate jar file under target folder
  • run jar file by java -jar target/springboot-sample-0.0.1-SNAPSHOT.jar
  • Navigate to: http://localhost:8080/users

More Details at:
https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins.html

How to deploy jar on Docker:

More Details at:
https://spring.io/guides/gs/spring-boot-docker/

How to deploy jar on Kubernetes:

  • you should have docker desktop (with kubernetes) running on your system. confirm by running below commands:
    • docker docker --version
    • kubernetes kubectl version
  • run shell script ./kubectl-build.sh to kickoff deployment on kubernetes
  • (For First Time ONLY) kubectl expose deployment springboot-sample --type=LoadBalancer --port 8080 --target-port 8080
  • Navigate to: http://localhost:8080/users

More Details at:
https://spring.io/guides/gs/spring-boot-kubernetes/
https://kubernetes.io/docs/tutorials/kubernetes-basics/
https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0

Features already in Project:

  • Spring boot framework
  • Some unit test cases
  • API calls and dependency injection
  • Swagger Docs
  • Create an executable jar package using either maven or gradle
  • Deploy the jar using Docker

CI/CD Using Jenkins Pipeline Feature

Setup Jenkins using Docker container from: https://hub.docker.com/r/jenkins/jenkins

  • docker pull jenkins/jenkins
  • Fireup Jenkins container by executing command below. sudo docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
  • this will automatically create a 'jenkins_home' docker volume on the host machine, that will survive the container stop/restart/deletion. Instructions here: https://github.com/jenkinsci/docker/blob/master/README.md
  • Follow installation steps to Install Jenkins.
  • Note that we make use of Jenkins pipeline and following Declarative syntax. Jenkinsfile is already checked into root of project.

JWT/OAuth2 Feature

  • Signup
curl --location --request POST 'http://localhost:8080/signup' \
--header 'Content-Type: application/json' \
--data-raw '{
    "username": "[email protected]",
    "password": 123456,
    "roles": [
        "USER","ADMIN"
    ]
}'
  • Login / JWT Authentication
curl --location --request POST 'http://localhost:8080/oauth/token' \
--header 'Authorization: Basic c3ByaW5nYm9vdC1zYW1wbGU6c2VjcmV0' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode '[email protected]' \
--data-urlencode 'password=123456' \
--data-urlencode 'grant_type=password'
  • Resource Api
    Use access_token return by login api response
curl --location --request GET 'http://localhost:8080/api/user' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiYXBpIl0sInVzZXJfbmFtZSI6InMuYWxpLmFiYmFzLnJhemFAZ21haWwuY29tIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImV4cCI6MTU4NjY0NjAwMCwiYXV0aG9yaXRpZXMiOlsiVVNFUiIsIkFETUlOIl0sImp0aSI6IjJlNWU2N2EwLTgwYTQtNDU1ZS1hODVhLTIxZjdkNzU1NDZkOCIsImNsaWVudF9pZCI6InNwcmluZ2Jvb3Qtc2FtcGxlIn0.eO-QSqNC4wA2wV6CSmvPTaFznMOHXs3h0fP1vz_JrZQ'
  • Refresh Authentication
    Authentication expire after mentioned expiry in login response. Use refresh_token return by login api response to renew authentication without password.
curl --location --request POST 'http://localhost:8080/oauth/token' \
--header 'Authorization: Basic c3ByaW5nYm9vdC1zYW1wbGU6c2VjcmV0' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'refresh_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiYXBpIl0sInVzZXJfbmFtZSI6InMuYWxpLmFiYmFzLnJhemFAZ21haWwuY29tIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImF0aSI6ImY1ZjVkMzVkLTg1YTAtNDgyMy1iMDAxLTM3YzFiNzgxM2VmZiIsImV4cCI6MTU4NjY0NjQ0OCwiYXV0aG9yaXRpZXMiOlsiVVNFUiIsIkFETUlOIl0sImp0aSI6IjMzNmMwZjJiLWUzMjQtNDdiNS04NTViLTkwMjVmZDkxYjdkYyIsImNsaWVudF9pZCI6InNwcmluZ2Jvb3Qtc2FtcGxlIn0.WPYybHRZk3OVp4cirpzv0m15lowoO6EciBl8c5w10Tk' \
--data-urlencode 'grant_type=refresh_token'

More Details at:
https://www.toptal.com/spring/spring-boot-oauth2-jwt-rest-protection
https://docs.spring.io/spring-security/site/docs/5.0.5.RELEASE/reference/htmlsingle/#what-is-acegi-security

Features to come:

  • Service Registory, Discovery and Invocation using Consul
  • CI/CD support to come later, this will require a build server VM on our computers i.e. Jenkins

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published