A RESTful webservice, which is authentication and authorizing user using JWT concept.
86
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.
To execute this project on your machine you need MongoDB, you can download MongoDB in your machine or run the docker image of MongoDB.
There are three environments variable for this project, such as
--env MONGO_DATABASE=Library --env MONGO_PORT=27017 --env MONGO_HOST=192.168.99.100
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.
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}
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"})
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.
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
{localhost or MONGO_HOST environment value}:5000/addBooks
{
"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
Content type
Image
Digest
Size
92.4 MB
Last updated
about 6 years ago
docker pull imranshaikh/security:1.0.0-RELEASE