This repository contains an example project that demonstrates how to implement Spring Security with JSON Web Tokens (JWT) in a Spring Boot application. The project uses Spring Security to handle authentication and authorization, and JWTs to secure RESTful endpoints. This repository is great for cloning for any new Spring Boot project.
To run this project, you will need to have Java 8 or higher installed on your machine. You will also need to have Maven installed, which you can download from the official website.
This project requires a PostgreSQL database to be set up. If you do not have PostgreSQL installed on your machine, you can download it from the official website.
Once you have installed PostgreSQL, you will need to create a database for this project. You can do this by running the following command in a PostgreSQL shell:
CREATE DATABASE my_database;
Finally, you will need to create the tables required for this project. You can do this by running the following SQL code in the PostgreSQL shell:
create table users(
username varchar(50) not null primary key,
password varchar(100) not null,
enabled boolean not null
);
create table authorities (
username varchar(50) not null,
authority varchar(50) not null,
constraint fk_authorities_users foreign key(username) references users(username)
);
create unique index ix_auth_username on authorities (username,authority);
Before running the application, you will need to modify the application.yml file to match your PostgreSQL database configuration. So you probably may need to change the URL, database name, username, and password values.
To start the application, simply run the following command from the root directory of the project:
mvn spring-boot:run
This will start the Spring Boot application on port 8080. You can then access the application by navigating to http://localhost:8080 in your web browser.
The following API endpoints are available in this application:
- Endpoint: /api/home
- HTTP Method: GET
- Description: This endpoint is just an example controller for testing. It requires no authorization and no authentication.
- Endpoint: /api/register
- HTTP Method: POST
- Description: This endpoint will register a new user.
- Request Body:
username
andpassword
are required. See example:
{
"username": "[email protected]",
"password": "12345678"
}
- Endpoint: /api/login
- HTTP Method: POST
- Description: This endpoint allows users to log in and obtain an authentication token.
- Request Body:
username
andpassword
are required. See example:
{
"username": "[email protected]",
"password": "12345678"
}
- Endpoint: /api/welcome
- HTTP Method: GET
- Description: This endpoint is only accessible by registered and logged-in users.
- Request Headers:
Authorization: Bearer <token>
Replace <token>
with the token obtained from the /api/login endpoint.