Sign inSign up

imranshaikh/security

By imranshaikh

•Updated about 6 years ago

A RESTful webservice, which is authentication and authorizing user using JWT concept.

Image
0

86

imranshaikh/security repository overview

⁠About

Security is a kind of Library management project, build with the intention of learning spring security concepts. many spring security features are implemented in this security project such as follows.

  • Authentication and Authorization > There are two roles in this application such as ROLE_ADMIN and ROLE_USER
  • JWT (Java JWT) > Java JWT is used for token-based authentication and authorization. Will discuss in ahead of this article

To execute this project on your machine you need MongoDB, you can download MongoDB in your machine or run the docker image of MongoDB.

⁠Environments

There are three environments variable for this project, such as

  • MONGO_DATABASE - MONGO_DATABASE is used to set the database (schema) name for the project. If you don't set the environment for database name then, the application will assign the default database name as Library.
  • MONGO_PORT - MONGO_PORT is used to expose your MongoDB port. if no port is provided then the application will assign the default MongoDB port which is 2707. If you are running your MongoDB in port 27017 then you don't need to provide a MONGO_PORT environment
  • MONGO_HOST - And finally MONGO_HOST, it is used to expose the IP address of your machine. if you are running this application in local then no need to expose the MONGO_HOST environment because it will take your local machine IP And if you wish to launch this application in a virtual machine you need to expose the MONGO_HOST environment variable
 --env MONGO_DATABASE=Library 
 --env MONGO_PORT=27017
 --env MONGO_HOST=192.168.99.100

⁠How to launch this Security project in Docker

You can pull this image into your machine by providing the following command

 docker pull imranshaikh/security:1.0.0-RELEASE

or else your directly run this image in your machine by using this command

 docker run -d -p 5000:5000 --name security_release  --env MONGO_DATABASE=Library  --env MONGO_PORT=27017 --env MONGO_HOST=192.168.99.100 imranshaikh/security:1.0.0-RELEASE

Before launching this security application make sure you already had to launch MongoDB on your machine.

⁠How to launch MongoDB in docker

To launch MongoDB in your machine using the docker, just write the following docker command

docker run -d -p 27017:27017 -v /{your absolute path}:/data/db --name {your wish to give container name} {docker image ID}
⁠NOTE: To launch this application you need to add a database in your MongoDB and collections for user authentication.

⁠How to create Database and Collection in MongoDB for this security project

First, launch the MongoDB in your machine using docker and enter into a docker container. we have ways to enter into a docker container, using -it, we can interact with the container. write the following command to enter into a docker container.

 docker exec -it {your mongodb container id} bash

After entering into the container, now we can access the MongoDB shell by using

 mongo

Now you are in MongoDB shell, to show all database use this command

 show dbs

If you don't find the Library database (obviously you will not get Library) then create a Library database

 use Library

Now, you can list collection using

 show collections

create user credential collection

 db.createCollection("userCredential") 

Now fill one user data

 db.userCredential.insert({"username" : "imran", "password" : "$2a$10$OpXttVvjIW4Uv67EpZiT2OgXz.2PrP4vk.oWQJKs2fU0YHf5baC8K", "role" : "ROLE_ADMIN"})

⁠What are the EndPoints of the Application

First, you need POSTMAN in your machine, because this application is a RESTful webservice. To Access the application, you need to login into the application using this endpoint

 {localhost or MONGO_HOST environment value}:5000/login

make sure this is a post HTTP method and in the body pass this JSON

{
    "username" : "imran",
    "password"  : "shaikh"
}

Now you will get the bearer token in the header of the response.

⁠What is Bearer Token

The bearer token is a jwt (JSON web token) used for authentication and authorization for the client request. Following is the example of bearer token ]

Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJpbXJhbiIsImF1dGhvcml0aWVzIjpbeyJhdXRob3JpdHkiOiJST0xFX0FETUlOIn1dLCJpYXQiOjE1OTc5MTc5NTUsImV4cCI6MTU5OTA5MTIwMH0.ppOfGLrM755AGheSzuZ_5ToOfUNh13iXrXUyMglt5vlEadRfBzvWRwVHWGTvi_Jmw6XbotfWnN6RbYxusk2qZw
  • Where you will find the bearer token
    • once you hit the login URL which is provided above with username and password you will get a token in the header of the response. Find the Authorization header key to get the token.

⁠The Endpoint of this application

  • To add Books, and it is a post HTTP Method
{localhost or MONGO_HOST environment value}:5000/addBooks
  • and pass this JSON object, to add books
{
    "title" : "Harry Potter and the Goblet of Fire",
    "price" : 699
}
  • To add user, make sure this request is post

    {localhost or MONGO_HOST environment value}:5000/addUser
    
  • and to add user, create this JSON object and pass in body of the request

{  
    "usernaem" : "superman",
    "password" : "superman",
    "role" : "ROLE_USER"
}
  • How to fetch all books, make sure this is a GET request

    {localhost or MONGO_HOST environment value}:5000/getAllBooks
    
  • How to fetch all users, make sure this is also a GET Request

    {localhost or MONGO_HOST environment value}:5000/getAllUser
    
⁠NOTE: To add user and to add Books you need admin permission, the user imran has the admin role, and you can also create a new user with an admin role if you wish.

Tag summary

Content type

Image

Digest

Size

92.4 MB

Last updated

about 6 years ago

docker pull imranshaikh/security:1.0.0-RELEASE