Skip to content

hilmiugurpolat/JWT-Authentication-with-Spring-Boot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JWT Authentication with Spring Boot

This project demonstrates how to implement JWT (JSON Web Token) authentication using Spring Boot. It includes user registration, login, and securing endpoints using JWT tokens.

Technologies Used

  • Spring Boot
  • Spring Security
  • JWT (JSON Web Token)
  • Hibernate (JPA)
  • H2 Database (for simplicity)
  • Maven

Getting Started

Installation

  1. Clone the repository:

    git clone https://github.com/hilmiugurpolat/JWT-Authentication-with-Spring-Boot.git
    cd JWT-Authentication-with-Spring-Boot
  2. Build the project:

    mvn clean install
  3. Run the application:

    mvn spring-boot:run

The application will start on http://localhost:8080.

API Endpoints

Register

  • URL: /api/auth/register

  • Method: POST

  • Request Body:

    {
        "email": "[email protected]",
        "username": "exampleuser",
        "password": "password123"
    }
  • Response:

    {
        "message": "User registered successfully"
    }

Login

  • URL: /api/auth/login

  • Method: POST

  • Request Body:

    {
        "username": "exampleuser",
        "password": "password123"
    }
  • Response:

    {
        "token": "<JWT_TOKEN>"
    }

Secured Endpoint

  • URL: /api/test

  • Method: GET

  • Headers:

    • Key: Authorization
    • Value: Bearer <JWT_TOKEN>
  • Response:

    {
        "message": "Access granted!"
    }

Configuration

JWT Secret Key

The secret key used to sign the JWT is defined in the JwtUtil class:

private String SECRET_KEY = "secret";