Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/khaouitiabdelhakim/authentication-and-authorization-springboot
This tutorial demonstrates how to implement authentication and authorization using Spring Boot. It covers the steps to set up the application, configure a MySQL database, and provides examples of API endpoints for user registration, login, and access control based on user roles.
https://github.com/khaouitiabdelhakim/authentication-and-authorization-springboot
authentication authorization backend jwt jwt-authentication mysql postman spring spring-boot
Last synced: 3 months ago
JSON representation
This tutorial demonstrates how to implement authentication and authorization using Spring Boot. It covers the steps to set up the application, configure a MySQL database, and provides examples of API endpoints for user registration, login, and access control based on user roles.
- Host: GitHub
- URL: https://github.com/khaouitiabdelhakim/authentication-and-authorization-springboot
- Owner: khaouitiabdelhakim
- Created: 2024-06-03T21:44:00.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-06-03T21:49:14.000Z (8 months ago)
- Last Synced: 2024-10-10T12:45:11.112Z (3 months ago)
- Topics: authentication, authorization, backend, jwt, jwt-authentication, mysql, postman, spring, spring-boot
- Language: Java
- Homepage:
- Size: 23.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Spring Boot Authentication and Authorization: Best Tutorial Ever
## Description
This tutorial demonstrates how to implement authentication and authorization using Spring Boot. It covers the steps to set up the application, configure a MySQL database, and provides examples of API endpoints for user registration, login, and access control based on user roles.```
If you find this repository useful or it has helped you,
please don't forget to leave a ⭐️, or even follow my GitHub account.
Your support motivates me to continue providing helpful resources.
Thank you for your appreciation! 🌟🚀💖😊👍If you'd like to support further, consider buying me a coffee:
```
[![Buy Me A Coffee](https://img.shields.io/badge/Buy%20Me%20A%20Coffee--yellow.svg?style=for-the-badge&logo=buy-me-a-coffee)](https://www.buymeacoffee.com/kh.abdelhakim)## Getting Started
### Prerequisites
Before starting the application, ensure you have the following installed:
- Java Development Kit (JDK)
- Apache Maven
- MySQL (using XAMPP or MySQL Workbench)### Database Setup
Create a MySQL database named `authentication`. You can use XAMPP or MySQL Workbench to create the database.### Application Properties
Configure the following properties in `application.properties`:```properties
spring.datasource.url=jdbc:mysql://localhost:3306/authentication?useSSL=false
spring.datasource.username=root
spring.datasource.password=server.port=8080
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=updateapp.jwtCookieName=authentication-app
app.jwtSecret=qFVFzWHpdEWBMrtyp9oZtvAqm122OWyv2cCu87cN0Yhgfrd1Z1kukGmOJkX9deBU43Wyz3GTRegYNQrj7mQmj5
app.jwtExpirationMs=2592000
```## Running the Application
Start the application using the following Maven command:
```bash
mvn spring-boot:run
```## API Endpoints
### 1. Authentication
#### Sign Up
**Endpoint:**
```http
POST http://localhost:8080/api/auth/sign-up
```
**Request Body:**
```json
{
"email": "[email protected]",
"password": "1234",
"role": "client"
}
```
**Response:**
```json
{
"message": "User Registered Successfully!"
}
```Example for admin:
```json
{
"email": "[email protected]",
"password": "1234",
"role": "admin"
}
```
**Response:**
```json
{
"message": "User Registered Successfully!"
}
```#### Sign In
**Endpoint:**
```http
POST http://localhost:8080/api/auth/sign-in
```
**Request Body:**
```json
{
"email": "[email protected]",
"password": "1234"
}
```
**Response:**
```json
{
"token": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJjbGll...",
"type": "Bearer",
"id": 5,
"email": "[email protected]",
"roles": [
"ROLE_CLIENT"
]
}
```#### Sign Out
**Endpoint:**
```http
POST http://localhost:8080/api/auth/sign-out
```
**Headers:**
```
Authorization: Bearer
```
**Response:**
```json
{
"message": "You've Been Signed Out!"
}
```### 2. Authorization
#### Access Profile
**Endpoint:**
```http
GET http://localhost:8080/api/app/profile
```
**Headers:**
```
Authorization: Bearer
```
**Response:**
```json
{
"id": 5,
"email": "[email protected]",
"roles": [
"ROLE_CLIENT"
]
}
```#### Get All Clients (Admin Only)
**Endpoint:**
```http
GET http://localhost:8080/api/admin/clients
```
**Headers:**
```
Authorization: Bearer
```
**Response (Admin):**
```json
[
{
"id": 1,
"email": "[email protected]",
"password": "$2a$10$j1LuZsTjVD4hHMtegSWrOuHkN8tdk0ZUMRNEppq36W2mqk3uDDnXW",
"role": "CLIENT"
},
{
"id": 2,
"email": "[email protected]",
"password": "$2a$10$hosM/Vbm03L3iq5cw4sSReOxzalXR1wjxjDS7Tj0WdFp5TbHTsDoi",
"role": "CLIENT"
},
{
"id": 3,
"email": "[email protected]",
"password": "$2a$10$j5nKqbtaMADtmTYJaAoNXujci6Z2woZfu6mDBHtLekCmbbrwC37/O",
"role": "CLIENT"
},
{
"id": 5,
"email": "[email protected]",
"password": "$2a$10$.9iRFPzcxdG8vJtOmbpLJeo2fbr8gy5d6hkVcjrSzjAFRQJgPN5ee",
"role": "CLIENT"
}
]
```If a non-admin user tries to access this endpoint:
**Response:**
```json
{
"path": "/error",
"error": "Unauthorized",
"message": "Full authentication is required to access this resource",
"status": 401
}
```Follow these steps to set up and test your Spring Boot application with authentication and authorization.
## Credits
Original code by Abdelhakim Khaouiti ([khaouitiabdelhakim on GitHub](https://github.com/khaouitiabdelhakim))
## License
This project is licensed under the MIT License```
Copyright 2024 KHAOUITI ABDELHAKIMLicensed under the MIT License
You may obtain a copy of the License athttp://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software
distributed under the MIT License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the MIT License.
```