This repository provides a starting point for a Spring Boot backend application with integrated JWT authentication using UsernamePasswordAuthentication. It includes basic user login and registration functionalities.
- Spring Boot: A robust framework for building Java applications.
- JWT Authentication: Secure authentication mechanism using JSON Web Tokens.
- User Login/Registration: Basic endpoints for user management.
- MongoDB: Integration with MongoDB for data storage.
- Validation: Input validation using Spring Boot's validation starter.
- Actuator: Monitoring and management of the application.
- Security: Spring Security for securing the application.
- Logging: Logstash encoder for structured logging.
- Utilities: Various utility libraries for common tasks.
- Java 23 (For now only features up to Java 17 can be used)
- Spring Boot 3.4.2
- Maven
- MongoDB
- JWT (JSON Web Token)
- Logstash
- Lombok
- MapStruct
- Apache Commons
- CycloneDX
- Java 23 (17)
- Maven
- MongoDB
To be able to run the application, you need to have MongoDB installed on your system. You can download and install MongoDB Community from the official website: MongoDB.
It is advisable to create a new database and user for the application. You can do this via the MongoDB Compass application. The database name and user credentials should be added to the application.properties or application.yml file.
-
Clone the repository:
git clone https://github.com/yourusername/springboot-with-auth.git cd springboot-with-auth -
Build the project:
mvn clean install
-
Run the application:
mvn spring-boot:run
The application can be configured using the application.properties or application.yml file located in the src/main/resources directory.
The application uses a dual token system for authentication:
- authToken: Used for accessing protected endpoints.
- refreshToken: Stored as a secure cookie and used to generate new
authTokenwhen it expires. TherefreshTokenis invalidated and regenerated upon use.
-
Change the JWT Secrets:
- Use the
JwtSecretGeneratorto generate new secrets. - Update the secrets in the
application.propertiesorapplication.ymlfile.
java -jar JwtSecretGenerator.jar
- Use the
-
Switch Cookie Settings to Secure:
- Ensure that the
refreshTokencookie settings are set tosecure. Currently this needs to be done manually in the specific code blocks.
- Ensure that the
- User Registration:
POST /api/v1/users/register - User Login:
POST /api/v1/user/login - Refresh Token:
POST /api/v1/auth/refresh(Required valid refresh token) - Protected Endpoint:
GET /api/v1/users/me(Requires JWT token --> Gets personal user data)
curl -X POST http://localhost:8080/api/v1/users/register -H "Content-Type: application/json" -d '{"username": "testuser", "password": "password"}'curl -X POST http://localhost:8080/api/v1/users/login -H "Content-Type: application/json" -d '{"username": "testuser", "password": "password"}'curl -X POST http://localhost:8080/api/v1/auth/refresh -H "Cookie: refreshToken=<your_refresh_token>"curl -X GET http://localhost:8080/api/v1/users/me -H "Authorization: Bearer <your_jwt_token>"This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
- Tim Niklas Tenger
Contributions are welcome! Please open an issue or submit a pull request for any changes.