Skip to content

[WIP] This project is an REST API with Spring Framework that secured by Basic Authentication

Notifications You must be signed in to change notification settings

rodrigoosoares/spring-basic-authorization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Initialize the Application

1. Start MongoDB docker container

docker-compose up -f docker/docker-compose.yml

2. Execute the application

./gradlew bootRun

Application features

This application implements the Spring Security Basic Authentication. It has an MongoDB Docker container that pre load 2 Users, one Admin and another a common user.

A Custom UserDetailService (MongoUserDetailsService) is used to retrieve the users information from the MongoDB

More information at: https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/index.html

1. Get User by username endpoint

This endpoint access any authenticated user, regardless of role (USER or ADMIN)

curl --location 'http://localhost:8080/user?username=Admin' \
--header 'Authorization: Basic QWRtaW46MTIz'
curl --location 'http://localhost:8080/user?username=Admin' \
--header 'Authorization: Basic Um9kcmlnbzoxMjM='

2. Create a new user

This endpoint can only be accessed by an user with ADMIN role

curl --location 'http://localhost:8080/user' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic QWRtaW46MTIz' \
--data '{
    "username": "New User",
    "password": "Pass123",
    "roles": [ "USER" ]
}'

So if you try to access using the user authentication, a 401 status will be returned

curl --location 'http://localhost:8080/user' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic VXNlcjoxMjM=' \
--data '{
    "username": "New User",
    "password": "Pass123",
    "roles": [ "USER" ]
}'

About

[WIP] This project is an REST API with Spring Framework that secured by Basic Authentication

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published