Skip to content

A boilerplate starter project for building a Java Spring Boot application with JWT and JPA authentication.

License

Notifications You must be signed in to change notification settings

boilerlabs/java-spring-auth-jwt-jpa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Java Spring Boot JPA JWT Auth

License Issues Stars

A boilerplate starter project for building a Java Spring Boot application with JWT authentication and JPA. This template includes essential features for implementing secure and scalable authentication systems in your Spring Boot applications.

Table of Contents

  1. Features
  2. Getting Started
  3. Usage
  4. Contributing
  5. License

Features

  • JWT authentication for secure token-based access
  • RSA keys for signing and verifying JWTs
  • Role-based access control
  • Configured SecurityFilterChain for managing security
  • Password hashing for secure storage
  • User Sign Up and Sign In endpoints
  • Profile endpoint for accessing user details
  • Jakarta Bean Validation for input validation
  • Docker Compose for running PostgreSQL and Adminer

Getting Started

Prerequisites

  • Java 17+
  • Maven

Installation

  1. Clone the repository:
git clone https://github.com/boboilerlabs/java-spring-auth-jwt-jpa
cd java-spring-auth-jwt-jpa
  1. Generating app.key and app.pub:
openssl genrsa -out src/main/resources/app.key 2048 
openssl rsa -in src/main/resources/app.key -pubout -out src/main/resources/app.pub
  1. Running with Maven
mvn spring-boot:run

Usage

Sign Up

To sign up, send a POST request to /api/auth/signup with the following JSON payload:

{
    "username": "example_username",
    "password": "example_password"
}

Sign In

To sign in, send a POST request to /api/auth/signin with the following JSON payload

{
    "username": "example_username",
    "password": "example_password"
}

The response will contain the JWT token

{
    "token": "example_token",
    "expiration": "2024-09-19T10:00:00Z"
}

Accessing Protected Routes

To access a protected route, include the JWT token in the Authorization header

curl -X GET http://localhost:8080/api/profile \
     -H "Authorization: Bearer example_token"

The response will return the user details:

{
    "id": "user-uuid",
    "username": "user",
    "roles": ["ROLE_USER"]
}

Contributing

We welcome contributions! Please see the CONTRIBUTING.md for more details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A boilerplate starter project for building a Java Spring Boot application with JWT and JPA authentication.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages